|
#include "stdafx.h"
#include "DelayMessageBox2.h"
#include <afxpriv.h>
IMPLEMENT_DYNAMIC(CDelayMessageBox2, CWnd)
CDelayMessageBox2::CDelayMessageBox2(CWnd* pParent)
{
m_hWndParent = pParent->GetSafeHwnd(); // can be NULL
m_autoclose = NULL;
m_count = 0;
}
BEGIN_MESSAGE_MAP(CDelayMessageBox2, CWnd)
ON_WM_TIMER()
ON_WM_CREATE()
ON_MESSAGE(WM_INITDIALOG, OnSubclassedInit)
END_MESSAGE_MAP()
// Purpose: Unhook window creation
int CDelayMessageBox2::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
AfxUnhookWindowCreate();
return CWnd::OnCreate(lpCreateStruct);
}
// Purpose: Disable OK button, start timer
LRESULT CDelayMessageBox2::OnSubclassedInit(WPARAM wParam, LPARAM lParam)
{
LRESULT lRet = Default();
CWnd* pOk = GetDlgItem(IDCANCEL);
if ( NULL != pOk )
pOk->EnableWindow(FALSE);
SetTimer(100,1000,NULL);
return lRet;
}
// Purpose: display running countdown, close when finished.
void CDelayMessageBox2::OnTimer(UINT nIDEvent)
{
if (nIDEvent == 100)
{
if (m_count>0)
SetWindowText(FormTitle(--m_count));
if (m_count == 0)
{
CWnd* pOk = GetDlgItem(IDCANCEL);
if ( NULL != pOk )
{
pOk->EnableWindow(TRUE);
pOk->SetFocus();
}
KillTimer(100);
if (m_autoclose)
PostMessage(WM_CLOSE,0,0);
}
}
}
// Purpose: Display a message box, hooking it to do stuff
int CDelayMessageBox2::MessageBox(LPCTSTR lpszText, int count, bool bclose,MBIcon icon)
{
m_autoclose = bclose;
m_count = count;
AfxHookWindowCreate(this);
return ::MessageBox(m_hWndParent, lpszText, FormTitle(m_count), icon);
}
// Purpose: compose a title for the dialog based on the # of seconds left to disable it
CString CDelayMessageBox2::FormTitle(int num)
{
CString s;
s.Format("%d seconds remaining",num);
return s;
}
// Purpose: prevent dialog from closing before it has timed out
LRESULT CDelayMessageBox2::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_COMMAND && m_count > 0)
{
if(HIWORD(wParam) == BN_CLICKED )
return 0;
}
return CWnd::DefWindowProc(message, wParam, lParam);
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.
Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.
Nish's Technology Blog :
voidnish.wordpress.com