• 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.   
    7.   
    8. // CUpDownTipDlg dialog  
    9.   
    10. class CUpDownTipDlg : public CDialog  
    11. {  
    12.     DECLARE_DYNAMIC(CUpDownTipDlg)  
    13.   
    14. public:  
    15.     CUpDownTipDlg(CWnd* pParent = NULL);   // standard constructor  
    16.     virtual ~CUpDownTipDlg();  
    17.   
    18.     // Dialog Data  
    19.     enum { IDD = IDD_MCMSG_DLG };  
    20.   
    21.     void ShowMsgWindow(CWnd* pParent, const CString& strTipInfo);  
    22.   
    23. protected:  
    24.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support  
    25.     virtual BOOL OnInitDialog();  
    26.     //响应关闭消息,删除对象  
    27.     virtual void OnCancel();  
    28.     virtual void PostNcDestroy();  
    29.     afx_msg void OnTimer(UINT_PTR nIDEvent);  
    30.     afx_msg void OnBnClickedOk();  
    31.     afx_msg void OnBnClickedCancel();  
    32.   
    33.     DECLARE_MESSAGE_MAP()  
    34.   
    35. private:  
    36.     void InitDlgPosition();  
    37.   
    38. private:  
    39.     CString m_strTipInfo;  
    40. };  

    UpDownTipDlg.cpp

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

    2、渐入渐出提示框实现

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

    主要API:AnimateWindow。


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

    InOutTipDlg.h

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

    InOutTipDlg.cpp

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

    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

  • 相关阅读:
    【Unity学习笔记】Unity网络游戏开发实战(一)---网络编程的开端:Echo程序
    【DX11学习笔记】GerstnerWave波浪模拟(基于GPU计算着色器的实现)
    【DX11学习笔记】粒子系统--爆炸特效
    【设计模式】(二)观察者模式是什么?
    【设计模式】(一)工厂模式是什么?
    【C++笔记】C++中常见智能指针auto_ptr、unique_ptr、shared_ptr和weak_ptr的用法
    【C++笔记】C++关联容器set和map的概述和操作
    【C++笔记】C++函数模版与类模版
    【C++笔记】C++中vector、stack、deque、list的简易实际使用方法
    python之赋值、浅拷贝、深拷贝
  • 原文地址:https://www.cnblogs.com/lidabo/p/3346341.html
Copyright © 2020-2023  润新知