📄 automessagebox.cpp
字号:
// AutoMessageBox.cpp : implementation file
//
#include "stdafx.h"
#include "AutoMessageBox.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//
int AutoMessageBox( int nInterval, const CString& strMessage, const CString& strTitle )
{
CAutoMessageBox msgbox( nInterval );
if( !strMessage.IsEmpty() )
{
msgbox.SetMessageString( strMessage );
}
if( !strTitle.IsEmpty() )
{
msgbox.SetTitleString( strTitle );
}
return msgbox.DoModal();
}
//
/////////////////////////////////////////////////////////////////////////////
// CAutoMessageBox dialog
CAutoMessageBox::CAutoMessageBox( int nInterval /*= 30*/ )
: CDialog(CAutoMessageBox::IDD, NULL)
{
//{{AFX_DATA_INIT(CAutoMessageBox)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_nInterval = nInterval;//对话框显示时间,以秒为单位默认为60秒!
m_nTimerID = 0;//定时器ID,0表示没有设置定时器
//m_strMessage = "对话框将在 %d 秒之后关闭...";//默认显示的消息
//m_strTitle = "剩余时间: %d 秒";//默认显示的标题
m_strMessage.LoadString( IDS_MESSAGE );
m_strTitle.LoadString( IDS_TITLE );
}
CAutoMessageBox::~CAutoMessageBox()
{
}
void CAutoMessageBox::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAutoMessageBox)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAutoMessageBox, CDialog)
//{{AFX_MSG_MAP(CAutoMessageBox)
ON_WM_TIMER()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAutoMessageBox message handlers
BOOL CAutoMessageBox::OnInitDialog()
{
CDialog::OnInitDialog();
//
// TODO: Add extra initialization here
if( m_nInterval < 1 )
{
m_nInterval = 0;
}
CString strTitle;
strTitle.Format( m_strTitle, m_nInterval );
SetWindowText( strTitle );
CWnd* pMessageWnd = GetDlgItem( IDC_MESSAGE );
if( pMessageWnd != NULL )
{
CString strMessage;
strMessage.Format( m_strMessage, m_nInterval );
pMessageWnd->SetWindowText( strMessage );
}
//
if( m_nInterval > 0 )
{
m_nTimerID = SetTimer( 1, 1000, NULL );
}
else
{
OnOK();
}
//
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CAutoMessageBox::OnTimer(UINT nIDEvent)
{
if( nIDEvent != m_nTimerID )
{
CDialog::OnTimer(nIDEvent);
return;
}
//
if( m_nInterval <= 1 )
{
OnOK();
}
m_nInterval--;
//
CString strTitle;
strTitle.Format( m_strTitle, m_nInterval );
SetWindowText( strTitle );
CWnd* pMessageWnd = GetDlgItem( IDC_MESSAGE );
if( pMessageWnd != NULL )
{
CString strMessage;
strMessage.Format( m_strMessage, m_nInterval );
pMessageWnd->SetWindowText( strMessage );
}
}
void CAutoMessageBox::OnDestroy()
{
CDialog::OnDestroy();
// TODO: Add your message handler code here
if( m_nTimerID > 0 )
{
KillTimer( m_nTimerID );
}
}
//
void CAutoMessageBox::SetInterval( int nInterval )
{
//设置对话框显示时间
m_nInterval = nInterval;
}
int CAutoMessageBox::GetInterval()const
{
//读取对话框显示时间
return m_nInterval;
}
void CAutoMessageBox::SetMessageString( const CString& strMessage )
{
//设置显示的消息(带格式)
m_strMessage = strMessage;
}
CString CAutoMessageBox::GetMessageString()const
{
//读取显示的消息
return m_strMessage;
}
void CAutoMessageBox::SetTitleString( const CString& strTitle )
{
//设置显示的标题
m_strTitle = strTitle;
}
CString CAutoMessageBox::GetTitleString()const
{
//读取标题
return m_strTitle;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -