Currently viewing: GipsySoft » Front Page» Articles

Project goals

To provide a more flexible MessageBox that is an enhancement of the existing API version. To allow for an auto-kill timer and optionally a check-box to "never show this again".

I did consider (not for long) writing it from scratch. However, the MessageBox API is fairly extensive and allows for a fair degree of flexibility. Instead I decided to coerce the existing API to do what I wanted.

Want to use GSMessageBox in Visual Basic?

Laurent Muller has been kind enough to donate the Visual Basic code and a sample application to enable you to use GSMessageBox in VB. Click here to download it.

Features overview

GSMessageBox provides the following extensions to a standard MessageBox. It extends the MessageBox API rather than replacing it. This allows you to use all of the functionality of a standard MessagBox while at the same time using some very handy extra features:

Auto-kill timer

Have you ever wanted to display a MessageBox for a few seconds and then have the default action taken. Now you can.

Click here to view sample code

Right align the buttons

Not everyone likes having the buttons centered, now you can have them right-aligned just like Windows Explorer MessageBox like dialogs.

Click here to view sample code

Checkbox on the MessageBox

Showing Don't show this again on a MessageBox is a hassle, usually you'll code a unique dialog for it. This is easy with GSMessageBox. It can even show whatever text you like.

Click here to view sample code

Absolute positioning

Sometimes we want the MessageBox to appear exactly where we want it. GSMessageBox allows this and will also correct the position you give it to ensure it doesn't overlap the taskbar or fall off the screen.

Click here to view sample code

Centering of the MessageBox to it's parent.

In MFC this isn't a problem but in plain vanilla API it's a hassle, not with GSMessageBox it isn't.

Click here to view sample code

And finally...

All of the above can be used pretty much at the same time.

The notable exception is centering and absolute position. The function will fail if you set both of these values.

Apart from that can now have an auto-kill MessageBox at an absolute position on screen and can include a checkbox with your right-aligned buttons.

Other minor 'features' of the library

GSMessageBox is written using C++ and the Win32 API, no MFC is needed nor are any external libraries or components.

Size is important, so GSMessageBox comes in it's own DLL and weighs in at a paltry 48kb.

The source code compiles to both Ansi and Unicode, level 4 warnings.

The source code can be statically linked into your application, thus reducing the need for yet another DLL.

Code notes

How is this done?

'Hooking' is the name of the game. Basically we hook window creation, then create the MessageBox in the usual way, when it is created we get notified. Once in our notification we can do all of the magic bits we need. You can see this fairly simple code in GSMessageBox.cpp.

To move the MessageBox buttons and to place the extra check box is a real hassle. Lots of iterating windows, grabbing their window class types and then calculating their positions - very dull but very worthwhile.

All of the real work goes on in HandleMessageBox(...) in HandleMessageBox.cpp. If you wanted to add extra functionality to GSMessageBox then this is where you'd start to add the actual code once you had modified the API structures.

To allow the timer and the check-box to function we need to subclass the dialog. This is begun in HandleMessageBox.cpp with ShouldSubclass(...).

Adding the checkbox consumes a disproportionate amount of code but everything is required. Figuring out the size of the checkbox is taken care of by GetCheckSize(...) and AddCheckbox(...) actually does the business of adding the checkbox, settings it's font and text, and of course moving the pushbuttons around so everything looks good.

Dull bits that projects like GSMessageBox need

Writing this documentation is a chore. When the code is written it's too easy to look at the code and think 'it seems so simple'.

Setting the directory structure so the library will build as a LIB and DLL with both debug and release as well as ansi and unicode...are probably the least exciting bits.

Deleting the readme.txt files that MSDEV puts in. There's nothing worse than having a readme.txt to find it has no valuable information at all.

Get from Source Safe, build it, find it something wrong, fix it, delete everything, get from Source Safe...Does anyone have a better method of getting *only* source code in the ZIP file?


July 24 2002
Fixed bug where timed message box was closing too early in WinXP. This was due to WinXP MessageBox having a timer that I didn't know about.