Click here to Skip to main content
15,860,943 members
Articles / Desktop Programming / MFC

Enhanced MFC Message Boxes

Rate me:
Please Sign up or sign in to vote.
4.82/5 (37 votes)
22 Oct 2010CPOL8 min read 268K   7.5K   126   56
How to replace the standard MFC message box with an enhanced version

Last update: 2010-10-20

Enhanced MFC message boxes

The Problem

Windows applications often use message boxes for asking the user for some actions or for displaying information messages. From week to week, more applications are getting published, which contain nice message boxes with checkboxes like "Do not display this message again" or "Do not ask this question" again, with which the user is able to customize the application behavior and to get rid of message boxes, which he always wants to answer in the same way.

I also wanted to offer this to the users of my applications. What features should be supported?

  • The message boxes should support some new style (the MB_??? flags used in a call of AfxMessageBox).
  • Such message boxes should be entirely written in MFC to enable other MFC developers to make quick and easy customization without having to know much about Windows API calls, just by using the standard MFC classes.
  • The new message boxes should support the checkboxes mentioned above and should manage automatically to save the state of the checkbox and the answer of the user and not to display the message box again, if the user didn't want it to appear again.
  • The message boxes should also be easy to integrate into existing applications. In existing applications, I didn't want to change every line of code containing a call to AfxMessageBox to something else. I'd just like to add a few lines of code and all messages boxes should appear in the new way.

Existing Solutions

At CodeProject, there are currently two different interesting solutions for modifying and enhancing the standard Windows message boxes, which can be invoked from MFC based applications by using the AfxMessageBox method:

Although these solutions work fine, there were a few problems, because of which I was forced to offer a third solution:

  • Both solutions can be used in MFC based applications, but offer only little or even no support for the standard MFC classes. For example, in one application, all dialogs are not derived from the MFC CDialog class directly, but from another class, which draws a custom skin for the dialog instead of the standard skin. If I want to use the message boxes provided by the two solutions mentioned above in this case, I have to apply many changes in the source code. My solution is entirely based upon MFC classes. It can therefore be easily modified and is for people like me, who enjoy using MFC and do not want to get deep into Windows API calls and is more easy to understand.
  • The XMessageBox class supports "Do not ask again" or "Do not display again" checkboxes in the displayed message boxes. This feature is also supported by my CMessageBoxDialog class. The problem with the XMessageBox class from my point of view has been, that the state of the checkboxes, which has been stored in the registry, was stored there in a way, which is not the standard way by using the WriteProfileInt or GetProfileInt methods of the CWinApp class. This might seem to be only a little problem, but I'd like to get the values stored in the same places as all other profile values are stored automatically by the MFC framework.
  • At last, the layout of the message boxes generated by the two classes mentioned above didn't seem to me quite the way I'd like it to be, therefore I changed it a little bit.

How to Use CMessageBoxDialog

The Simple Way

The simple way to use the new message box is to change nothing in your application. All your calls to AfxMessageBox(...) remain just the way they are already. You just have to overwrite the method DoMessageBox in your application class, which is derived from CWinApp. Use the following code to overwrite the method:

C++
int CYourApplication::DoMessageBox ( LPCTSTR lpszPrompt, UINT nType, 
    UINT nIDPrompt )
{
    // Create a handle to store the parent window of the message box.
    CWnd* pParentWnd = CWnd::GetActiveWindow();

    // Check whether an active window was retrieved successfully.
    if ( pParentWnd == NULL )
    {
        // Try to retrieve a handle to the last active popup.
        pParentWnd = GetMainWnd()->GetLastActivePopup();
    }

    // Create the message box dialog.
    CMessageBoxDialog dlgMessage(pParentWnd, lpszPrompt, _T(""), nType,
        nIDPrompt);

    // Display the message box dialog an return the result.
    return (int)dlgMessage.DoModal();
}

Now every time you use AfxMessageBox anywhere in your application, the new message box will appear. The new feature can simply be used by adding the new message box styles (the MB_??? flags) to your already existing message box flags. Your example, if you do a call like this:

C++
int nResult = AfxMessageBox(IDS_ARE_YOU_SURE_TO_QUIT, 
    MB_YESNO | MB_ICONQUESTION);

To add the checkbox "Don't ask again" to the message box, simply change the call to:

C++
int nResult = AfxMessageBox(IDS_ARE_YOU_SURE_TO_QUIT, 
    MB_YESNO | MB_ICONQUESTION | MB_DONT_ASK_AGAIN);

The Other Way

The other way of using the new message box is to directly create a dialog object. For details, please take a look at the header file of CMessageBoxDialog. The constructor will take the same parameters as the AfxMessageBox method. If you create the message box this way, you can also change the displayed icon to some other icon by using the SetMessageIcon method.

It's also required to create the message box this way, if you want to use the timeout feature, which can be activated by calling the SetTimeout method. A timeout is a countdown, which starts, when the message box is displayed. There are two modes for a timeout: The "un-disabled" or "enabled" timeout means, that the user can choose any button, but if he doesn't choose one, the default button will be assumed as being chosen, when the countdown is finished. The other mode, a "disabled" countdown is something like a nag screen. All buttons will be disabled, until the countdown is finished. After that, the user can click any button.

The New Styles and Return Values

Besides the standard MB_??? flags provided by Windows, the CMessageBoxDialog supports the following new flags:

  • MB_CONTINUEABORT: Display two buttons in the message box: "Continue" and "Abort".
  • MB_SKIPSKIPALLCANCEL: Display three buttons in the message box: "Skip", "Skip all" and "Cancel".
  • MB_IGNOREIGNOREALLCANCEL: Display three buttons in the message box "Ignore", "Ignore all" and "Cancel".
  • MB_DONT_DISPLAY_AGAIN: If you set this flag, a checkbox "Don't display this message again" will be displayed in the message box. If the user checks this checkbox, the result of the message box will automatically be stored in the registry. The next time, this dialog will be called through AfxMessageBox or the DoModal method of the CMessageBoxDialog directly, the former result will be returned. You as a developer do not have to care, whether the message box is actually displayed or not. You will receive the result, the user wants you to receive - no matter whether it's a direct input or it's the input stored in the registry, because the user didn't want to be asked again.
  • MB_DONT_ASK_AGAIN: This flag does the same as MB_DONT_DISPLAY_AGAIN, only the text of the checkbox will be another one: "Don't ask this question again" instead of "Don't display this message again".
  • MB_DEFAULT_CHECKED: If a checkbox is displayed, this box is not checked by default. If you add this flag, the checkbox will be marked checked as soon as the message box is displayed.
  • MB_YES_TO_ALL: If this flag is set and your message box contains a "Yes" button, an additional button with the text "Yes to all" will be added to the message box.
  • MB_NO_TO_ALL: This flag does the same as MB_YES_TO_ALL, but it will add the button "No to all", if your message box contains a button named "No".
  • MB_RIGHT_ALIGN: If you specify this flag, the button in the dialog will be aligned on the right side of the message box (e.g. as it is done in the Windows Explorer) and will not get centered in the dialog.
  • MB_NO_SOUND: If you specify this flag, no standard sound will be played, if you open a message box.
  • MB_DEFBUTTON5 and MB_DEFBUTTON6: Because now there can be more than four buttons in a message box, these flags have been added to enable you to select another default button.

Because of the new buttons, there are also some new return values: IDYESTOALL, IDNOTOALL, IDSKIP, IDSKIPALL and IDIGNOREALL. They represent the new buttons and will be returned, if one of these buttons is clicked.

Other Methods

There are also some other public methods for the CMessageBoxDialog class, which are explained in the header file or the source code and should be easy to understand and use.

Just one method might be interesting: ResetMessageBoxes. If you call this method, all message box results, which have been stored in the registry, because the user checked the "Don't display again" or "Don't ask again" checkbox, are removed. Therefore all message boxes will be displayed again.

How to Use It with VC6/MFC6

To use the CMessageBoxDialog with VC6/MFC6, some minor changes need to be applied. Please take a look at this message for more information. Because I do not have VC6/MFC6 installed, I cannot provide a full download for this case, but I think it's very easy to customize and should be no problem.

History

2010-10-20: Version 1.2 lite

  • Added the feature that one can add buttons without needing a static string resource
  • Added the feature that the dialog is now dynamic (instead of having add a custom dialog resource)
  • Removed the feature of using a checkbox to remember choice (required access to settings/registry)
  • Removed the feature of using a timer to automatically choose (too complicated code)
  • Removed the feature of imitating the AfxMessageBox (Messsagebox styles require access to user32 resource to retrieve "Ok", "Cancel", etc.)

2004-03-01: Version 1.1a (Minor update)

  • License: Source code now may be used under the terms of the LGPL license, too
  • License: Added the full licenses to the ZIP archives

2004-02-29: Version 1.1 (Minor update)

  • Bug Fix: Added missing files to the ZIP archive
  • Bug Fix: Added support for the standard Windows flags MB_RIGHT and MB_RTLREADING, which were still missing
  • New Feature: Added the ResetMessageBoxes method to reset the message boxes
  • New Feature: Added the MB_DEFAULT_CHECKED flag
  • New Feature: Added the enabled and disabled timeout for the message boxes

2004-02-28: Version 1.0 (Initial release)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralNice Pin
ddmcr19-May-06 9:25
ddmcr19-May-06 9:25 
GeneralWinXP Cleartype Drawing support Pin
2-Jul-04 6:27
suss2-Jul-04 6:27 
GeneralRe: WinXP Cleartype Drawing support Pin
Skyhawk20-Feb-07 0:00
Skyhawk20-Feb-07 0:00 
AnswerRe: WinXP Cleartype Drawing support Pin
BritGeek26-Feb-07 9:38
BritGeek26-Feb-07 9:38 
GeneralDialog Box Pin
MHillary18-Jun-04 5:08
MHillary18-Jun-04 5:08 
GeneralRe: Dialog Box Pin
Michael P. Mehl21-Jun-04 2:54
Michael P. Mehl21-Jun-04 2:54 
Generalkey used to recognize the messagebox Pin
laoran24-Mar-04 11:18
laoran24-Mar-04 11:18 
GeneralCrashes at start up Pin
Rui Jiang24-Mar-04 7:11
Rui Jiang24-Mar-04 7:11 
This line of code in DoMessageBox needs to be guarded.

// Try to retrieve a handle to the last active popup.
pParentWnd = GetMainWnd()->GetLastActivePopup();

I hit the problem that when I was doing AfxMessageBox before the main window shows up, GetMainWnd() return NULL thus caused crash.

Thanks!
GeneralRe: Crashes at start up Pin
Michael P. Mehl31-Mar-04 6:45
Michael P. Mehl31-Mar-04 6:45 
GeneralRe: Crashes at start up Pin
Rui Jiang31-Mar-04 17:34
Rui Jiang31-Mar-04 17:34 
GeneralCheckbox always check by default Pin
po.coulet2-Mar-04 23:11
po.coulet2-Mar-04 23:11 
GeneralRe: Checkbox always check by default Pin
Michael P. Mehl2-Mar-04 23:14
Michael P. Mehl2-Mar-04 23:14 
GeneralBugfix for Version 1.1a Pin
Michael P. Mehl2-Mar-04 23:22
Michael P. Mehl2-Mar-04 23:22 
GeneralRe: Bugfix for Version 1.1a Pin
Eric Hirst15-Jul-05 7:43
Eric Hirst15-Jul-05 7:43 
QuestionRelease with GPL license ? Pin
laoran29-Feb-04 23:01
laoran29-Feb-04 23:01 
AnswerRe: Release with GPL license ? Pin
Michael P. Mehl29-Feb-04 23:05
Michael P. Mehl29-Feb-04 23:05 
AnswerCMessageBoxDialog now under LGPL, too. Pin
Michael P. Mehl1-Mar-04 1:52
Michael P. Mehl1-Mar-04 1:52 
GeneralRe: CMessageBoxDialog now under LGPL, too. Pin
laoran29-Mar-04 21:25
laoran29-Mar-04 21:25 
GeneralRe: CMessageBoxDialog now under LGPL, too. Pin
Michael P. Mehl31-Mar-04 6:41
Michael P. Mehl31-Mar-04 6:41 
GeneralWorking now with VC 6. Pin
WREY29-Feb-04 9:05
WREY29-Feb-04 9:05 
GeneralXMessageBox Pin
Uwe Keim28-Feb-04 21:53
sitebuilderUwe Keim28-Feb-04 21:53 
GeneralRe: XMessageBox Pin
Michael P. Mehl29-Feb-04 0:56
Michael P. Mehl29-Feb-04 0:56 
GeneralI know you didn't test this under VC 6. Pin
WREY28-Feb-04 4:34
WREY28-Feb-04 4:34 
GeneralRe: I know you didn't test this under VC 6. Pin
Michael P. Mehl28-Feb-04 4:54
Michael P. Mehl28-Feb-04 4:54 
GeneralRe: I know you didn't test this under VC 6. Pin
PJ Arends28-Feb-04 18:25
professionalPJ Arends28-Feb-04 18:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.