Introduction

This article describes a simple solution for automatically closing a message box. I have looked at many similar articles in the Inetrnet which are quite complex, so I thought I would make this one a little more simple.

Understanding the CMsgBox class

CMsgBox is a class which implements auto-close functionality. This class is derived from the CWnd class. It exposes a method called "MessageBox()", which in turn would call CWnd::MessageBox() to display a message box.

Getting the auto-close functionality implemented is very simple. In the CMsgBox::MessageBox() method, we start a timer (SetTimer() method) just before we call the CWnd::MessageBox API. In the OnTimer method, we try to find the message box window by using its window name (caption). Once found, we post a WM_CLOSE message to close the message box. That's it !

Share

About the Author

Sandeep completed his masters in Computer Applications in 2003 and has been hooked onto programming since then. His first project involved communicating with hardware devices which actually got him more interested in device programming.

It would be nice if you could provide a way to retrieve the return value from the internal MessageBox() call. In its current form, the class is useful only for message boxes displayed using MB_OK, where the return value is always IDOK.

The caller should also be able to specify the default return value that will be used if the message box autocloses.