|
#include "stdafx.h"
#include "DelayMessageBox.h"
CMapPtrToPtr CDelayMessageBox::m_map;
IMPLEMENT_DYNAMIC(CDelayMessageBox, CWnd)
CDelayMessageBox::CDelayMessageBox(CWnd* pParent)
{
m_hHook = NULL;
m_hMsgBoxWnd = NULL;
m_hOK = NULL;
m_autoclose = NULL;
m_OkayWnd.m_hWnd = NULL;
Create(NULL,"{8B32A21C-C853-4785-BE20-A4E575EE578A}",WS_OVERLAPPED, CRect(0,0,0,0),
pParent,1000);
m_map[m_hWnd] = this;
}
CDelayMessageBox::~CDelayMessageBox()
{
m_map.RemoveKey(m_hWnd);
DestroyWindow();
}
BEGIN_MESSAGE_MAP(CDelayMessageBox, CWnd)
ON_WM_TIMER()
END_MESSAGE_MAP()
BOOL CALLBACK CDelayMessageBox::EnumChildProc( HWND hwnd, LPARAM lParam )
{
CDelayMessageBox *pthis = static_cast<CDelayMessageBox*>((LPVOID)lParam);
char str[256];
::GetWindowText(hwnd,str,255);
if(strcmp(str,"OK") == 0)
{
pthis->m_hOK = hwnd;
if(pthis->m_count>0)
{
::EnableWindow(pthis->m_hOK,FALSE);
}
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK CDelayMessageBox::CBTProc(int nCode,WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_ACTIVATE )
{
void* p;
m_map.Lookup(::FindWindowEx(::GetParent((HWND)wParam),NULL,NULL,
"{8B32A21C-C853-4785-BE20-A4E575EE578A}"),p);
CDelayMessageBox* pthis = (CDelayMessageBox*)p;
pthis->m_hMsgBoxWnd = (HWND)wParam;
EnumChildWindows(pthis->m_hMsgBoxWnd,EnumChildProc,(LPARAM)pthis);
UnhookWindowsHookEx(pthis->m_hHook);
if(pthis->m_count>0)
pthis->m_OkayWnd.SubclassWindow(pthis->m_hMsgBoxWnd);
pthis->m_hHook = NULL;
}
return FALSE;
}
void CDelayMessageBox::OnTimer(UINT nIDEvent)
{
if(nIDEvent == 100 && m_hMsgBoxWnd )
{
if(m_count>0)
m_OkayWnd.SetWindowText(FormTitle(--m_count));
if(m_count == 0)
{
if(m_OkayWnd.m_hWnd)
{
m_OkayWnd.UnsubclassWindow();
m_OkayWnd.m_hWnd = NULL;
}
::EnableWindow(m_hOK,TRUE);
KillTimer(100);
m_hOK = NULL;
if(m_autoclose)
::PostMessage(m_hMsgBoxWnd,WM_CLOSE,0,0);
m_hMsgBoxWnd = NULL;
}
}
CWnd::OnTimer(nIDEvent);
}
int CDelayMessageBox::MessageBox(LPCTSTR lpszText, int count, bool bclose,MBIcon icon)
{
m_autoclose = bclose;
m_hHook = SetWindowsHookEx(WH_CBT,CBTProc,
AfxGetApp()->m_hInstance,
AfxGetApp()->m_nThreadID);
m_count = count;
SetTimer(100,1000,NULL);
CWnd::MessageBox(lpszText,FormTitle(m_count),icon);
return IDOK;
}
CString CDelayMessageBox::FormTitle(int num)
{
CString s;
s.Format("%d seconds remaining",num);
return s;
}
// COkayWnd
IMPLEMENT_DYNAMIC(COkayWnd, CWnd)
COkayWnd::COkayWnd()
{
}
COkayWnd::~COkayWnd()
{
}
BEGIN_MESSAGE_MAP(COkayWnd, CWnd)
END_MESSAGE_MAP()
// COkayWnd message handlers
LRESULT COkayWnd::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: Add your specialized code here and/or call the base class
if(message == WM_COMMAND)
{
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