MessageBox With Timeout

If a program or some part of a program can execute in the unattended
mode, it may be useful to have a message box which would close
on timeout, thus letting the program to continue. I would like to
present here a function that does exactly this. It has the following
declaration:

int MsgBoxWithTimeout(HWND hWnd, LPCTSTR lpText, LPCTSTR lpCaption,
UINT uType, UINT uElapse, UINT uBlinkingTime);

The first 4 parameters are the same as in MessageBox. The message box will be
closed in uElapse milliseconds unless there is any activity from the user. If the
user does not close the message box, but either presses a key when it is in the
foreground, or does some mouse action over its client or non-client area, the
message box will wait for another uElapse milliseconds. uBlinkingTime milliseconds
before closing, the message box caption will start blinking to attract user attention.

To be able to close the message box MsgBoxWithTimeout function spawns another thread.
This new thread sleeps for a while to give the message box time to appear and then
tries to find its window handle. To this end it uses EnumThreadWindows API function
looking for the window with “#32770” class name. Then the function waits either for
timeout, or the user to close the message box using WaitForSingleObject API. On timeout
the function calls EndDialog API to close the message box.

Download demo project – 14 KB

More by Author

Must Read