Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Delay MessageBox with auto-close option

, , 13 Aug 2002 Ms-PL
This message box delays its dismissal by disabling the OK button for a delay interval. It also has an optional auto-close feature. There are two versions, one uses a WH_CBT hook and some basic window tricks like sub-classing to achieve its goal. The other one is more MFC-ied.
#include "stdafx.h"
#include "DelayMessageBox2.h"
#include <afxpriv.h>

CDelayMessageBox2::CDelayMessageBox2(CWnd* pParent)
	m_hWndParent = pParent->GetSafeHwnd();    // can be NULL
	m_autoclose = NULL;
	m_count = 0;

BEGIN_MESSAGE_MAP(CDelayMessageBox2, CWnd)

// Purpose: Unhook window creation
int CDelayMessageBox2::OnCreate(LPCREATESTRUCT lpCreateStruct)
	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 )
	return lRet;

// Purpose: display running countdown, close when finished.
void CDelayMessageBox2::OnTimer(UINT nIDEvent)
	if (nIDEvent == 100)
		if (m_count>0)

		if (m_count == 0)
			CWnd* pOk = GetDlgItem(IDCANCEL);
			if ( NULL != pOk )
			if (m_autoclose)

// 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;
	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.


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Authors

Software Developer
United States United States

Nish Nishant

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150327.1 | Last Updated 14 Aug 2002
Article Copyright 2002 by Shog9, Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid