类似QQ与360软件,消息提示有两种。上下浮动、渐入渐出。
1、上下浮动提示框实现
机制,定时器响应上下浮动消息。
主要API:MoveWindow。
源码如下UpDownTipDlg.h、UpDownTipDlg.cpp。
UpDownTipDlg.h
/*
*@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
// 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
/*
*@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
// 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、两种消息框调用
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()),提供一个回调删除接口。