Click here to Skip to main content
15,121,230 members
Articles / Desktop Programming / MFC
Posted 27 Feb 2004


125 bookmarked

Enhanced MFC Message Boxes

Rate me:
Please Sign up or sign in to vote.
4.81/5 (36 votes)
22 Oct 2010CPOL8 min read
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:

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,

    // 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:

int nResult = AfxMessageBox(IDS_ARE_YOU_SURE_TO_QUIT, 

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

int nResult = AfxMessageBox(IDS_ARE_YOU_SURE_TO_QUIT, 

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.


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)


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


About the Author

Michael P. Mehl
Web Developer
Germany Germany
No Biography provided

Comments and Discussions

QuestionDestroyIcon probably a bad idea Pin
MordachaiWolf10-Feb-14 8:09
MemberMordachaiWolf10-Feb-14 8:09 
QuestionApplication crashes if Multithreaded Pin
Member 1032260019-Oct-13 2:36
MemberMember 1032260019-Oct-13 2:36 
Questionwin32 messagebox Pin
Amit R. Sharma19-Mar-13 0:27
MemberAmit R. Sharma19-Mar-13 0:27 
QuestionMy vote of 5 && Is it possible to show no buttons at all? Pin
Ron Anders18-Feb-13 4:25
MemberRon Anders18-Feb-13 4:25 
GeneralMy vote of 5 Pin
gndnet4-Nov-12 22:30
Membergndnet4-Nov-12 22:30 
QuestionWhere are the new MB_??? flags defined. Pin
bralecou24-Sep-12 2:06
Memberbralecou24-Sep-12 2:06 
QuestionNo button? Pin
spyhunter8828-Nov-11 19:58
Memberspyhunter8828-Nov-11 19:58 
AnswerRe: No button? Pin
Rolf Kristensen2-Jul-12 13:04
MemberRolf Kristensen2-Jul-12 13:04 
QuestionWithout resource dialog Pin
Joxemi15-Feb-11 6:32
MemberJoxemi15-Feb-11 6:32 
AnswerRe: Without resource dialog Pin
pj220_200630-May-13 16:46
Memberpj220_200630-May-13 16:46 
QuestionHow about C# variant? Pin
Nick Alexeev22-Oct-10 14:58
professionalNick Alexeev22-Oct-10 14:58 
Questionmaryam Pin
wearth23-Jul-10 7:00
Memberwearth23-Jul-10 7:00 
QuestionA much simpler version of CMessageBoxDialog ? Pin
Rolf Kristensen12-Sep-09 14:51
MemberRolf Kristensen12-Sep-09 14:51 
AnswerRe: A much simpler version of CMessageBoxDialog ? Pin
Rolf Kristensen1-Nov-10 3:08
MemberRolf Kristensen1-Nov-10 3:08 
GeneralNice implementation, but... Pin
SoftwareGeek5-Sep-07 6:42
MemberSoftwareGeek5-Sep-07 6:42 
Question"Yes To all" is not displayed. Why ? Pin
zmau23-Aug-07 3:42
Memberzmau23-Aug-07 3:42 
AnswerRe: "Yes To all" is not displayed. Why ? Pin
zmau27-Aug-07 6:19
Memberzmau27-Aug-07 6:19 
GeneralRe: "Yes To all" is not displayed. Why ? Pin
zmau27-Aug-07 20:14
Memberzmau27-Aug-07 20:14 
GeneralStrange box Pin
morganrg29-Jun-07 15:14
Membermorganrg29-Jun-07 15:14 
On my message dialog I get a strange box that comes from the message title and goes off the end of the message box. How did it get there and how do I get rid of it? I am using Microsoft Visual Studio 2005
Version 8.0.50727.42 (RTM.050727-4200)
Microsoft .NET Framework
Version 2.0.50727

Installed Edition: Professional

Microsoft Visual C++ 2005 77626-009-0000007-41701
Microsoft Visual C++ 2005

QuestionHow to change font for message and caption ? Pin
ana_v12316-Apr-07 3:12
Memberana_v12316-Apr-07 3:12 
GeneralNice Pin
ddmcr19-May-06 10:25
Memberddmcr19-May-06 10:25 
GeneralWinXP Cleartype Drawing support Pin
2-Jul-04 7:27
suss2-Jul-04 7:27 
GeneralRe: WinXP Cleartype Drawing support Pin
Skyhawk20-Feb-07 1:00
MemberSkyhawk20-Feb-07 1:00 
AnswerRe: WinXP Cleartype Drawing support Pin
BritGeek26-Feb-07 10:38
MemberBritGeek26-Feb-07 10:38 
GeneralDialog Box Pin
MHillary18-Jun-04 6:08
MemberMHillary18-Jun-04 6:08 

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.