Delay MessageBox with auto-close option

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.

Overview

A delay message box will not allow the user to dismiss it until the delay
interval has run out. It does this by disabling the OK button on the message box
till the specified time interval expires. One example of a situation where this
might be useful is a shareware program that has expired its trial period. Say you
want to show a message box to the user and you want to make sure it stays there
for at least 10 seconds. Anyway Nish started writing this class with a totally
different idea. He wanted to center his message box on it's parent window. That's
when he found out that, message boxes do this by default. In his case they were
not doing so because he had made them owned by the desktop. Anyway Nish ended up
writing a delay message box that also has an auto-close option. If the
auto-close option is set to true, then the message box will close on it's own
once the delay period has terminated. This was how the CDelayMessageBox
class was born.

What turned out as a simple attempt to center a message box has ended up in
some rather complicated code with WH_CBT hooks, invisible windows,
a CWnd* to HWND map and a sub-classed message box
window that overrides DefWindowProc of all things to override. It
sure seems like a lot of work for such a simple sounding task. But it is the
write-once-use-multiple-times kind of class and thus Nish hopes his methods are
justified.

That's when Shog got interested in the class. Shog is the type of guy who
hates any kind of code obfuscation and he's always trying to figure out easier
ways of doing things. Anyhow he modified Nish's class so that it was more
MFC-ied. We decided to call it CDelayMessageBox2 because while it
didn't extend the class in anyway, the implementation was thoroughly revamped.
Both the classes are presented in this article as well as in the demo project
and the class source. You'll find the following files to be of interest to you.

DelayMessageBox.cpp - This was the original implementation file and
you can take a look at this one if you are interested in seeing elementary
examples of the use of WH_CBT hooks and window sub-classing.

DelayMessageBox.h - The header file for the original class

DelayMessageBox2.cpp - This is the new implementation file
and you can see some high quality MFC type sub classing here.

DelayMessageBox2.h - The header file for the revised class.

Usage

The CDelayMessageBox and CDelayMessageBox2 classes have only one public method in addition to the
constructor. There is no parameter-less constructor. By the way, in the rest of
the article when you see CDelayMessageBox, it represents both the
classes unless specifically mentioned otherwise.

Constructor

Constructs a CDelayMessageBox object.

CDelayMessageBox(CWnd* pParent);

pParent - This will be the parent window of the eventual message box that
will be displayed. You should not set this to NULL. The parent window must be a
valid CWnd that holds a valid HWND.

MessageBox method

lpszText - Points to a null-terminated string containing the message to be
displayed. You may use a CString here.

count - This is the delay in seconds. You can use any delay from 0 - the
maximum size of an int, but you are advised to keep it under 60 for all
practical purposes.

bclose - If this is set to true, the message box will close on its own after
the delay period, otherwise the OK button is enabled so that the user can
dismiss the message box manually.

icon - This is an MBIcon enumeration which can take one of the following
values.

CDelayMessageBox::MBIcon::MBICONNONE

CDelayMessageBox::MBIcon::MBICONSTOP

CDelayMessageBox::MBIcon::MBICONQUESTION

CDelayMessageBox::MBIcon::MBICONEXCLAMATION

CDelayMessageBox::MBIcon::MBICONINFORMATION

Sample Code

/*
You may use either of the classes.
In behaviour they are identical.
It's in the implementation that they differ.
*///CDelayMessageBox mbox(this);
CDelayMessageBox2 mbox(this);
mbox.MessageBox(m_text,
m_delay,
m_close,(CDelayMessageBox2::MBIcon)mbicon);

Technical details

CDelayMessageBox

CDelayMessageBox is derived from CWnd and it creates a
CWnd object in it's
constructor. The window that is created has a unique title text and is hidden.
The unique text is a GUID. The class has a static CMapPtrToPtr member using
which we maintain an HWND to CWnd* map. This is so that any number of threads
may simultaneously use the CDelayMessageBox class. In other words it's
thread-safe.

When the MessageBox method is called we use SetWindowsHookEx to set a
WH_CBT
hook. We then start a timer at a 1-second interval and use CWnd::MessageBox to
show our message box. In the hook proc we enumerate all the child windows of the
message box window using EnumChildWindows. In the callback for
EnumChildWindows,
we disable the OK button. We also subclass the message box window to a custom
CWnd derived class. And we also unhook the WH_CBT hook. In the timer proc,
we keep decreasing the count and also keep changing the title text of the
message box to reflect the remaining time in seconds. When the count reaches
zero we enable the OK button or if the auto-close option is true we dismiss the
message box using a WM_CLOSE message.

The custom CWnd derived class into which we subclass the message
box was added as a bug fix to a problem reported by Andreas Saurwein where he
found that the message box can be closed using the space bar. This is because a
WM_COMMAND message is sent to the message box window with a BN_CLICKED notification when the space bar is pressed. This has been
handled by overriding DefWindowProc and filtering out this message.

CDelayMessageBox2

Now we don't have a hidden CWnd parent for the message box. We
create the CWnd object using AfxHookWindowCreate when
the call to MessageBox(...) is made. MFC will call SetWindowsHookEx for you. Now the message box has been sub-classed by our
CWnd derived class. We override OnSubclassedInit and
we disable the OK button and start our timer. We also override OnCreate
where we call AfxUnhookWindowCreate as we don't have any further
need for the hook.

The timer proc is similar to the timer proc in the original class and we keep
changing the title text to reflect the remaining time in seconds. Once the delay
interval has elapsed we post a WM_CLOSE to the message box. And
also enable the OK button.

Class source listings

Both the old and new classes are listed here. They both use contrastingly
different techniques to solve the delay message box problem. We thought you
might want to compare them and that might also help to understand the inner
workings better. Now the class has out-valued itself in the sense, it is now a
class with a lot more academic value than utility. Both the implementations
reveal a lot about the inner workings of Windows.

Conclusion

This class started off with one idea and ended up with another. This was also
one of Nish's first proper attempts with using hooks. So he might have made some
erroneous assumptions. But he is counting on the wonderful feedback that is
available through the thousands of CodeProject visitors and regulars.
Shog would also like to see whether there is any way to further simply the
class. Thank you.

Updates and fixes

Aug 14 2002 - Shog has joined Nish as co-author and now there is a more
MFC-ied version of the class available. Both classes have been retained as
they both depict various interesting win32 techniques.

Aug 13 2002 - A bug was reported by Andreas Saurwein where he discovered
that the space bar can close the delayed message box. This has been fixed by
sub-classing the message box window and handling the message that causes this
behaviour.

Share

About the Authors

Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 16 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (14 consecutive awards as of 2015).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously 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 vastly experienced in team management, mentoring teams, and directing all stages of software development.