The MessageBox API is one of the preferred APIs when it comes to simply displaying a message, but its simplicity comes at a cost.
Formatting options, size control and other drawing capabilities are missing. On the other hand, it has a well-designed behavior that most users are used to.
As you probably know, I recently designed a control that can draw formatted text based on a markup string. The idea behind this new advanced API is to combine the well-behaved MessageBox with new drawing features. This new API is simply called
MsgBox and is parameter compatible the the original MessageBox. This way, you exchange the name of the API and add the markup string.
This approach is not limited to an OOP framework because the drawing code can easily be replaced with your own custom code.
Here are some details of the implementation:
- Get access to the control using a system hook.
- Pass additional information using a custom caption string.
- Intercept WM_NCCALCSIZE, release the hook and replace the dialog WndProc.
- Store the pointer to the additional information in a new window property.
- Intercept WM_INITDLG and setup the control, get information of child controls and reposition them.
- Intercept WM_NOTIFY and WM_HELP and forward them to the dialog parent window.
- Intercept WM_PAINT to perform custom drawing.
- Intercept WM_NCDESTROY to release allocated resources.
Notes:
- Don’t use GWLP_USERDATA to store data. The original API uses it.
- Don’t try to change the parent of the buttons. They will not work anymore.
- Calling DefDlgProc after interception WM_PAINT will draw the grey band behind the buttons on the original position. This call is necessary to paint the remaining controls but make sure that the previous artwork don’t interfere with the actual drawing. In the demo code, the grey band in covered by the TextView control.
- The Help dialog launched from the help button is also a MsgBox call.
- The Tooltip information is obtained using a WM_NOTIFY that is forwarded to the main application.
- I had to extend the TextView code and add some missing procedures in the ObjMem library. All those files are in the attachment.
- The attached code is compiled for 32 bit and tested on Win10, Win8 and Win7. Still, the code may contain some bugs that I would like to hear about.
For this example, I used the following markup string:
"{S.X 420}{S.Y 195}{PADDING 20}", \
"[INDENT 55]",\
"[B,H +6,C #FF4F4F]Warning![~]{P}", \
"[H +2]Critical system failure - ERRID 521 - [~]", \
"[Info 0, Font 'Webdings', H +1]{#69}[~]{P}", \
"[C #5F4FFF]", \
"[I]Read the instruction manual for more information [~]", \
"[Info 1, Font 'Webdings', H +1]{#69}[~]", \
"[~]", \
"[~]"
Have fun,
Biterider