MFC上下浮动与渐入渐出消息提示框实现

时间:2023-03-08 21:44:38
MFC上下浮动与渐入渐出消息提示框实现

类似QQ与360软件,消息提示有两种。上下浮动、渐入渐出。

1、上下浮动提示框实现

机制,定时器响应上下浮动消息。

主要API:MoveWindow。

源码如下UpDownTipDlg.h、UpDownTipDlg.cpp。

UpDownTipDlg.h

  1. /*
  2. *@brief 上下浮动提示框
  3. *@date 2012-8-9
  4. */
  5. #pragma once
  6. // CUpDownTipDlg dialog
  7. class CUpDownTipDlg : public CDialog
  8. {
  9. DECLARE_DYNAMIC(CUpDownTipDlg)
  10. public:
  11. CUpDownTipDlg(CWnd* pParent = NULL);   // standard constructor
  12. virtual ~CUpDownTipDlg();
  13. // Dialog Data
  14. enum { IDD = IDD_MCMSG_DLG };
  15. void ShowMsgWindow(CWnd* pParent, const CString& strTipInfo);
  16. protected:
  17. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  18. virtual BOOL OnInitDialog();
  19. //响应关闭消息,删除对象
  20. virtual void OnCancel();
  21. virtual void PostNcDestroy();
  22. afx_msg void OnTimer(UINT_PTR nIDEvent);
  23. afx_msg void OnBnClickedOk();
  24. afx_msg void OnBnClickedCancel();
  25. DECLARE_MESSAGE_MAP()
  26. private:
  27. void InitDlgPosition();
  28. private:
  29. CString m_strTipInfo;
  30. };

UpDownTipDlg.cpp

  1. // MCMsgTipDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "mcmsgtip_demo.h"
  5. #include "UpDownTipDlg.h"
  6. const UINT_PTR POP_WINDOW = 1;
  7. const UINT_PTR DISPLAY_DELAY = 2;
  8. const UINT_PTR CLOSE_WINDOW = 3;
  9. const UINT POP_ELAPSE = 1;
  10. const UINT DELAY_ELAPSE = 5000;
  11. const UINT CLOSE_ELAPSE = 1;
  12. //上下浮动跨度
  13. const UINT FLOAT_SPAN = 2;
  14. // CUpDownTipDlg dialog
  15. IMPLEMENT_DYNAMIC(CUpDownTipDlg, CDialog)
  16. CUpDownTipDlg::CUpDownTipDlg(CWnd* pParent /*=NULL*/)
  17. : CDialog(CUpDownTipDlg::IDD, pParent)
  18. , m_strTipInfo(_T(""))
  19. {
  20. }
  21. CUpDownTipDlg::~CUpDownTipDlg()
  22. {
  23. }
  24. void CUpDownTipDlg::DoDataExchange(CDataExchange* pDX)
  25. {
  26. CDialog::DoDataExchange(pDX);
  27. }
  28. BEGIN_MESSAGE_MAP(CUpDownTipDlg, CDialog)
  29. ON_WM_TIMER()
  30. ON_BN_CLICKED(IDOK, &CUpDownTipDlg::OnBnClickedOk)
  31. ON_BN_CLICKED(IDCANCEL, &CUpDownTipDlg::OnBnClickedCancel)
  32. END_MESSAGE_MAP()
  33. // CUpDownTipDlg message handlers
  34. void CUpDownTipDlg::ShowMsgWindow(CWnd* pParent, const CString& strTipInfo)
  35. {
  36. m_strTipInfo = strTipInfo;
  37. Create(IDD, pParent);
  38. ShowWindow(SW_SHOW);
  39. }
  40. BOOL CUpDownTipDlg::OnInitDialog()
  41. {
  42. CDialog::OnInitDialog();
  43. // TODO:  Add extra initialization here
  44. SetDlgItemText(IDC_TIP_INFO, m_strTipInfo);
  45. InitDlgPosition();
  46. //消息弹出效果
  47. SetTimer(POP_WINDOW, POP_ELAPSE, NULL);
  48. return TRUE;
  49. }
  50. void CUpDownTipDlg::InitDlgPosition()
  51. {
  52. CRect rectInit;
  53. GetWindowRect(&rectInit);
  54. RECT rect;
  55. SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
  56. int cy = rect.bottom-rect.top;
  57. int cx = rect.right-rect.left;
  58. int nx = rect.right - rectInit.Width();
  59. int ny = cy;
  60. rectInit.MoveToXY(nx, ny);
  61. MoveWindow(rectInit);
  62. }
  63. void CUpDownTipDlg::OnTimer(UINT_PTR nIDEvent)
  64. {
  65. RECT rect;
  66. SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);
  67. int cy = rect.bottom-rect.top;
  68. int cx = rect.right-rect.left;
  69. CRect rectTip;
  70. GetWindowRect(&rectTip);
  71. switch (nIDEvent)
  72. {
  73. case POP_WINDOW:
  74. {
  75. if (rectTip.bottom > cy)
  76. {
  77. rectTip.MoveToY(rectTip.top - FLOAT_SPAN);
  78. MoveWindow(rectTip);
  79. }
  80. else
  81. {
  82. KillTimer(POP_WINDOW);
  83. SetTimer(DISPLAY_DELAY, DELAY_ELAPSE, NULL);
  84. }
  85. break;
  86. }
  87. case DISPLAY_DELAY:
  88. {
  89. KillTimer(DISPLAY_DELAY);
  90. SetTimer(CLOSE_WINDOW, CLOSE_ELAPSE, NULL);
  91. break;
  92. }
  93. case CLOSE_WINDOW:
  94. {
  95. if (rectTip.top <= cy)
  96. {
  97. rectTip.MoveToY(rectTip.top + FLOAT_SPAN);
  98. MoveWindow(rectTip);
  99. }
  100. else
  101. {
  102. KillTimer(CLOSE_WINDOW);
  103. PostMessage(WM_CLOSE);
  104. }
  105. break;
  106. }
  107. }
  108. CDialog::OnTimer(nIDEvent);
  109. }
  110. void CUpDownTipDlg::OnCancel()
  111. {
  112. DestroyWindow();
  113. }
  114. void CUpDownTipDlg::PostNcDestroy()
  115. {
  116. CDialog::PostNcDestroy();
  117. //窗口销毁时,删除该对象
  118. delete this;
  119. }
  120. void CUpDownTipDlg::OnBnClickedOk()
  121. {
  122. OnOK();
  123. ::MessageBox(AfxGetMainWnd()->GetSafeHwnd(), _T("提示框的反馈-是"), _T("提示"), MB_OK);
  124. }
  125. void CUpDownTipDlg::OnBnClickedCancel()
  126. {
  127. OnCancel();
  128. }

2、渐入渐出提示框实现

机制,定时器响应淡入淡出消息。

主要API:AnimateWindow。

源码如下InOutTipDlg.h、InOutTipDlg.cpp。

InOutTipDlg.h

  1. /*
  2. *@brief 淡入淡出提示框
  3. *@date 2012-8-9
  4. */
  5. #pragma once
  6. // CInOutTipDlg dialog
  7. class CInOutTipDlg : public CDialog
  8. {
  9. DECLARE_DYNAMIC(CInOutTipDlg)
  10. public:
  11. CInOutTipDlg(CWnd* pParent = NULL);   // standard constructor
  12. virtual ~CInOutTipDlg();
  13. // Dialog Data
  14. enum { IDD = IDD_MCMSG_DLG };
  15. void ShowMsgWindow(CWnd* pParent, const CString& strTipInfo);
  16. protected:
  17. virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  18. virtual BOOL OnInitDialog();
  19. //响应关闭消息,删除对象
  20. virtual void OnCancel();
  21. virtual void PostNcDestroy();
  22. afx_msg void OnTimer(UINT_PTR nIDEvent);
  23. afx_msg void OnBnClickedOk();
  24. afx_msg void OnBnClickedCancel();
  25. DECLARE_MESSAGE_MAP()
  26. private:
  27. void InitDlgPosition();
  28. private:
  29. CString m_strTipInfo;
  30. };

InOutTipDlg.cpp

  1. // MCMsgTipDlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "mcmsgtip_demo.h"
  5. #include "InOutTipDlg.h"
  6. const UINT_PTR BLAND_IN = 4;
  7. const UINT_PTR BLAND_OUT = 5;
  8. const UINT IN_ELAPSE = 1;
  9. const UINT OUT_ELAPSE = 5000;
  10. // CInOutTipDlg dialog
  11. IMPLEMENT_DYNAMIC(CInOutTipDlg, CDialog)
  12. CInOutTipDlg::CInOutTipDlg(CWnd* pParent /*=NULL*/)
  13. : CDialog(CInOutTipDlg::IDD, pParent)
  14. , m_strTipInfo(_T(""))
  15. {
  16. }
  17. CInOutTipDlg::~CInOutTipDlg()
  18. {
  19. }
  20. void CInOutTipDlg::DoDataExchange(CDataExchange* pDX)
  21. {
  22. CDialog::DoDataExchange(pDX);
  23. }
  24. BEGIN_MESSAGE_MAP(CInOutTipDlg, CDialog)
  25. ON_WM_TIMER()
  26. ON_BN_CLICKED(IDOK, &CInOutTipDlg::OnBnClickedOk)
  27. ON_BN_CLICKED(IDCANCEL, &CInOutTipDlg::OnBnClickedCancel)
  28. END_MESSAGE_MAP()
  29. // CInOutTipDlg message handlers
  30. void CInOutTipDlg::ShowMsgWindow(CWnd* pParent, const CString& strTipInfo)
  31. {
  32. m_strTipInfo = strTipInfo;
  33. Create(IDD, pParent);
  34. ShowWindow(SW_HIDE);
  35. }
  36. BOOL CInOutTipDlg::OnInitDialog()
  37. {
  38. CDialog::OnInitDialog();
  39. // TODO:  Add extra initialization here
  40. SetDlgItemText(IDC_TIP_INFO, m_strTipInfo);
  41. InitDlgPosition();
  42. //消息渐入渐出效果
  43. SetTimer(BLAND_IN, IN_ELAPSE, NULL);
  44. return TRUE;
  45. }
  46. void CInOutTipDlg::InitDlgPosition()
  47. {
  48. CRect rectInit;
  49. GetWindowRect(&rectInit);
  50. RECT rect;
  51. SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
  52. int cy = rect.bottom-rect.top;
  53. int cx = rect.right-rect.left;
  54. int nx = rect.right - rectInit.Width();
  55. int ny = cy - rectInit.Height();
  56. rectInit.MoveToXY(nx, ny);
  57. MoveWindow(rectInit);
  58. }
  59. void CInOutTipDlg::OnTimer(UINT_PTR nIDEvent)
  60. {
  61. RECT rect;
  62. SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0);
  63. int cy = rect.bottom-rect.top;
  64. int cx = rect.right-rect.left;
  65. CRect rectTip;
  66. GetWindowRect(&rectTip);
  67. switch (nIDEvent)
  68. {
  69. case BLAND_IN:
  70. {
  71. KillTimer(BLAND_IN);
  72. AnimateWindow(1000, AW_BLEND);
  73. SetTimer(BLAND_OUT, OUT_ELAPSE, NULL);
  74. break;
  75. }
  76. case BLAND_OUT:
  77. {
  78. KillTimer(BLAND_OUT);
  79. AnimateWindow(1000, AW_BLEND|AW_HIDE);
  80. PostMessage(WM_CLOSE);
  81. break;
  82. }
  83. }
  84. CDialog::OnTimer(nIDEvent);
  85. }
  86. void CInOutTipDlg::OnCancel()
  87. {
  88. DestroyWindow();
  89. }
  90. void CInOutTipDlg::PostNcDestroy()
  91. {
  92. CDialog::PostNcDestroy();
  93. //窗口销毁时,删除该对象
  94. delete this;
  95. }
  96. void CInOutTipDlg::OnBnClickedOk()
  97. {
  98. OnOK();
  99. ::MessageBox(AfxGetMainWnd()->GetSafeHwnd(), _T("提示框的反馈-是"), _T("提示"), MB_OK);
  100. }
  101. void CInOutTipDlg::OnBnClickedCancel()
  102. {
  103. OnCancel();
  104. }

3、两种消息框调用

  1. void ShowTipWindow(const CString& strTipInfo)
  2. {
  3. CButton* pCheckBtn = (CButton*)GetDlgItem(IDC_CHECK_IO);
  4. if (BST_CHECKED == pCheckBtn->GetCheck())
  5. {
  6. //渐入渐出效果弹框
  7. CInOutTipDlg* pMsgWindow=new CInOutTipDlg();
  8. pMsgWindow->ShowMsgWindow(this, strTipInfo);
  9. }
  10. else
  11. {
  12. //上下浮动方式弹框
  13. CUpDownTipDlg* pMsgWindow=new CUpDownTipDlg();
  14. pMsgWindow->ShowMsgWindow(this, strTipInfo);
  15. }
  16. }

两个消息提示框,都封装了ShowMsgWindow接口,传入父窗口和待提示信息就可以了。

^_^这个调用方式有内存泄露现象,具体实现的时候,可以在对话框销毁时(virtual void PostNcDestroy()),提供一个回调删除接口。

from:http://blog.csdn.net/segen_jaa/article/details/7848598