Tag Archives: auto-close

AfxMessageBox with Auto-close – Part 2

In a previous article I showed how to make AfxMessageBox to display a message box with auto-close, by using a CBT hook.
One reader suggested that whould be nice if the message box displays also the time left until auto-closing. I thought that’s a very good idea and promissed an update. Here it is!

AfxMessageBox with Auto-close

AfxMessageBox with Auto-close

Of course, may be more than one solution. I chose one which appends “Time left: xxx sec.” in the bottom of the built-in static text control. For this purpose, I added two private methods to CAutoMessageBox class:

  • CAutoMessageBox::_InitStaticTextCtrl – initializes the static contol: finds the static control, keeps in mind the initial text, makes space for an extra-line of text, and assigns an ID to be used later.
  • CAutoMessageBox::_SetStaticTextCtrl – sets the text which contains time left.

For more implementation details of CAutoMessageBox class, download and have a look in the attached demo project.

See also

Downloads

AfxMessageBox with Auto-close

Introduction

This article describes how to make the MFC function AfxMessageBox to dispay a message box that is automatically dismissed after a given time.
Auto-close message box

For that purpose we can do the following:

  1. Override CWinApp::DoMessageBox virtual function for customzing the default AfxMessageBox.
  2. Set a CBT hook to catch the message box creation.
  3. In the CBT hook procedure, subclass the message box window procedure.
  4. In the message box procedure, handle WM_INITDIALOG message and set a timer.
  5. Handle WM_TIMER to automatically close the message box.

Next are simplified code snippets that show how to implement the steps described above. CAutoMessageBox is a class designed to implement the message box with auto-close (sets CBT hook, subclasses the message box and so on).
More detailed code can be found in the demo application attached at the end of the article.

1. Override CWinApp::DoMessageBox

2. Set the CBT hook

3. Subclass the message box window procedure

4. Handle WM_INITDIALOG and set a timer

5. Handle WM_TIMER and close the message box

Demo application

You can find attached here a simple dialog-based application for demo purpose.

[Demo]AfxMessageBox_with_auto_close

Just choose the options (time out, message box style and so on) then click “Say Hello!” button. If “Auto close” is checked, a message box with time-out will be shown.

Resources

See also

Downloads