MFC上下浮動與漸入漸出消息提示框實現 2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀 睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接 類似QQ與360軟件,消息提示有兩種。上下浮動、漸入漸出。 1、上下浮動提示框實現 機制,定時器響應上下浮動消息。 主要API:MoveWindow。 源碼如下UpDownTipDlg.h、UpDownTipDlg.cpp。 UpDownTipDlg.h [cpp] view plaincopy /* *@brief 上下浮動提示框 *@date 2012-8-9 */ #pragma once // CUpDownTipDlg dialog class CUpDownTipDlg : public CDialog { DECLARE_DYNAMIC(CUpDownTipDlg) public: CUpDownTipDlg(CWnd* pParent = NULL); // standard constructor virtual ~CUpDownTipDlg(); // Dialog Data enum { IDD = IDD_MCMSG_DLG }; void ShowMsgWindow(CWnd* pParent, const CString& strTipInfo); protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support virtual BOOL OnInitDialog(); //響應關閉消息,刪除對象 virtual void OnCancel(); virtual void PostNcDestroy(); afx_msg void OnTimer(UINT_PTR nIDEvent); afx_msg void OnBnClickedOk(); afx_msg void OnBnClickedCancel(); DECLARE_MESSAGE_MAP() private: void InitDlgPosition(); private: CString m_strTipInfo; }; UpDownTipDlg.cpp [cpp] view plaincopy // MCMsgTipDlg.cpp : implementation file // #include "stdafx.h" #include "mcmsgtip_demo.h" #include "UpDownTipDlg.h" const UINT_PTR POP_WINDOW = 1; const UINT_PTR DISPLAY_DELAY = 2; const UINT_PTR CLOSE_WINDOW = 3; const UINT POP_ELAPSE = 1; const UINT DELAY_ELAPSE = 5000; const UINT CLOSE_ELAPSE = 1; //上下浮動跨度 const UINT FLOAT_SPAN = 2; // CUpDownTipDlg dialog IMPLEMENT_DYNAMIC(CUpDownTipDlg, CDialog) CUpDownTipDlg::CUpDownTipDlg(CWnd* pParent /*=NULL*/) : CDialog(CUpDownTipDlg::IDD, pParent) , m_strTipInfo(_T("")) { } CUpDownTipDlg::~CUpDownTipDlg() { } void CUpDownTipDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CUpDownTipDlg, CDialog) ON_WM_TIMER() ON_BN_CLICKED(IDOK, &CUpDownTipDlg::OnBnClickedOk) ON_BN_CLICKED(IDCANCEL, &CUpDownTipDlg::OnBnClickedCancel) END_MESSAGE_MAP() // CUpDownTipDlg message handlers void CUpDownTipDlg::ShowMsgWindow(CWnd* pParent, const CString& strTipInfo) { m_strTipInfo = strTipInfo; Create(IDD, pParent); ShowWindow(SW_SHOW); } BOOL CUpDownTipDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here SetDlgItemText(IDC_TIP_INFO, m_strTipInfo); InitDlgPosition(); //消息彈出效果 SetTimer(POP_WINDOW, POP_ELAPSE, NULL); return TRUE; } void CUpDownTipDlg::InitDlgPosition() { CRect rectInit; GetWindowRect(&rectInit); RECT rect; SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0); int cy = rect.bottom-rect.top; int cx = rect.right-rect.left; int nx = rect.right - rectInit.Width(); int ny = cy; rectInit.MoveToXY(nx, ny); MoveWindow(rectInit); } void CUpDownTipDlg::OnTimer(UINT_PTR nIDEvent) { RECT rect; SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0); int cy = rect.bottom-rect.top; int cx = rect.right-rect.left; CRect rectTip; GetWindowRect(&rectTip); switch (nIDEvent) { case POP_WINDOW: { if (rectTip.bottom > cy) { rectTip.MoveToY(rectTip.top - FLOAT_SPAN); MoveWindow(rectTip); } else { KillTimer(POP_WINDOW); SetTimer(DISPLAY_DELAY, DELAY_ELAPSE, NULL); } break; } case DISPLAY_DELAY: { KillTimer(DISPLAY_DELAY); SetTimer(CLOSE_WINDOW, CLOSE_ELAPSE, NULL); break; } case CLOSE_WINDOW: { if (rectTip.top <= cy) { rectTip.MoveToY(rectTip.top + FLOAT_SPAN); MoveWindow(rectTip); } else { KillTimer(CLOSE_WINDOW); PostMessage(WM_CLOSE); } break; } } CDialog::OnTimer(nIDEvent); } void CUpDownTipDlg::OnCancel() { DestroyWindow(); } void CUpDownTipDlg::PostNcDestroy() { CDialog::PostNcDestroy(); //窗口銷毀時,刪除該對象 delete this; } void CUpDownTipDlg::OnBnClickedOk() { OnOK(); ::MessageBox(AfxGetMainWnd()->GetSafeHwnd(), _T("提示框的反饋-是"), _T("提示"), MB_OK); } void CUpDownTipDlg::OnBnClickedCancel() { OnCancel(); } 2、漸入漸出提示框實現 機制,定時器響應淡入淡出消息。 主要API:AnimateWindow。 源碼如下InOutTipDlg.h、InOutTipDlg.cpp。 InOutTipDlg.h [cpp] view plaincopy /* *@brief 淡入淡出提示框 *@date 2012-8-9 */ #pragma once // CInOutTipDlg dialog class CInOutTipDlg : public CDialog { DECLARE_DYNAMIC(CInOutTipDlg) public: CInOutTipDlg(CWnd* pParent = NULL); // standard constructor virtual ~CInOutTipDlg(); // Dialog Data enum { IDD = IDD_MCMSG_DLG }; void ShowMsgWindow(CWnd* pParent, const CString& strTipInfo); protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support virtual BOOL OnInitDialog(); //響應關閉消息,刪除對象 virtual void OnCancel(); virtual void PostNcDestroy(); afx_msg void OnTimer(UINT_PTR nIDEvent); afx_msg void OnBnClickedOk(); afx_msg void OnBnClickedCancel(); DECLARE_MESSAGE_MAP() private: void InitDlgPosition(); private: CString m_strTipInfo; }; InOutTipDlg.cpp [cpp] view plaincopy // MCMsgTipDlg.cpp : implementation file // #include "stdafx.h" #include "mcmsgtip_demo.h" #include "InOutTipDlg.h" const UINT_PTR BLAND_IN = 4; const UINT_PTR BLAND_OUT = 5; const UINT IN_ELAPSE = 1; const UINT OUT_ELAPSE = 5000; // CInOutTipDlg dialog IMPLEMENT_DYNAMIC(CInOutTipDlg, CDialog) CInOutTipDlg::CInOutTipDlg(CWnd* pParent /*=NULL*/) : CDialog(CInOutTipDlg::IDD, pParent) , m_strTipInfo(_T("")) { } CInOutTipDlg::~CInOutTipDlg() { } void CInOutTipDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); } BEGIN_MESSAGE_MAP(CInOutTipDlg, CDialog) ON_WM_TIMER() ON_BN_CLICKED(IDOK, &CInOutTipDlg::OnBnClickedOk) ON_BN_CLICKED(IDCANCEL, &CInOutTipDlg::OnBnClickedCancel) END_MESSAGE_MAP() // CInOutTipDlg message handlers void CInOutTipDlg::ShowMsgWindow(CWnd* pParent, const CString& strTipInfo) { m_strTipInfo = strTipInfo; Create(IDD, pParent); ShowWindow(SW_HIDE); } BOOL CInOutTipDlg::OnInitDialog() { CDialog::OnInitDialog(); // TODO: Add extra initialization here SetDlgItemText(IDC_TIP_INFO, m_strTipInfo); InitDlgPosition(); //消息漸入漸出效果 SetTimer(BLAND_IN, IN_ELAPSE, NULL); return TRUE; } void CInOutTipDlg::InitDlgPosition() { CRect rectInit; GetWindowRect(&rectInit); RECT rect; SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0); int cy = rect.bottom-rect.top; int cx = rect.right-rect.left; int nx = rect.right - rectInit.Width(); int ny = cy - rectInit.Height(); rectInit.MoveToXY(nx, ny); MoveWindow(rectInit); } void CInOutTipDlg::OnTimer(UINT_PTR nIDEvent) { RECT rect; SystemParametersInfo(SPI_GETWORKAREA,0,&rect,0); int cy = rect.bottom-rect.top; int cx = rect.right-rect.left; CRect rectTip; GetWindowRect(&rectTip); switch (nIDEvent) { case BLAND_IN: { KillTimer(BLAND_IN); AnimateWindow(1000, AW_BLEND); SetTimer(BLAND_OUT, OUT_ELAPSE, NULL); break; } case BLAND_OUT: { KillTimer(BLAND_OUT); AnimateWindow(1000, AW_BLEND|AW_HIDE); PostMessage(WM_CLOSE); break; } } CDialog::OnTimer(nIDEvent); } void CInOutTipDlg::OnCancel() { DestroyWindow(); } void CInOutTipDlg::PostNcDestroy() { CDialog::PostNcDestroy(); //窗口銷毀時,刪除該對象 delete this; } void CInOutTipDlg::OnBnClickedOk() { OnOK(); ::MessageBox(AfxGetMainWnd()->GetSafeHwnd(), _T("提示框的反饋-是"), _T("提示"), MB_OK); } void CInOutTipDlg::OnBnClickedCancel() { OnCancel(); } 3、兩種消息框調用 [cpp] view plaincopy void ShowTipWindow(const CString& strTipInfo) { CButton* pCheckBtn = (CButton*)GetDlgItem(IDC_CHECK_IO); if (BST_CHECKED == pCheckBtn->GetCheck()) { //漸入漸出效果彈框 CInOutTipDlg* pMsgWindow=new CInOutTipDlg(); pMsgWindow->ShowMsgWindow(this, strTipInfo); } else { //上下浮動方式彈框 CUpDownTipDlg* pMsgWindow=new CUpDownTipDlg(); pMsgWindow->ShowMsgWindow(this, strTipInfo); } } 兩個消息提示框,都封裝了ShowMsgWindow接口,傳入父窗口和待提示信息就可以了。 ^_^這個調用方式有內存泄露現象,具體實現的時候,可以在對話框銷毀時(virtual void PostNcDestroy()),提供一個回調刪除接口。 from:http://blog.csdn.net/segen_jaa/article/details/7848598RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成