Click here to Skip to main content
13,351,050 members (53,899 online)
Click here to Skip to main content
Add your own
alternative version


26 bookmarked
Posted 23 Apr 2008

Customised Modal Dialog

, 23 Apr 2008
Rate this:
Please Sign up or sign in to vote.
Modal dialog with a user-defined look and feel.


This article/source code explains the procedure to create a customized modal dialog. In the project, the complete application has customized dialogs and controls. I needed the dialogs to be customized based on user input and also provide modal dialog like functionality. And the result was this piece of code. The code is written in MFC and is implemented as a class.


Modal Dialogs in MFC can be created using either the default templates provided by the wizard or the memory defined templates using the DLGTEMPLATE structure. But, neither of these options don’t allow creating customized skins.

Using the Code

The step-by-step usage of the code is explained below:

  1. Use the wizard to add a new dialog and the corresponding dialog class (say, CBrightness). It will be derived from CDialog, by default. Replace CDialog with CCustDialogBox in the CBrightness files (source and header).
  2. Class CBrightness : public CCustDialogBox
  3. Include the header file CustDialogBox.h in the CBrightness dialog’s header file.
  4. Override the CreateCustDialog function as shown below:
    1. Use the PreInitCtrl() function to provide customized parameters.
    2. Then, call the Create() function with the WS_POP_UP style, the parent window handler, the dialog ID, and the rectangle defining the dialog size.
    3. If dialog is created successfully, then create the OK and Cancel buttons.
    //In header file
    Class CBrightness : public CCustDialogBox
        HWND CreateCustDialog;
    //In source file, add
    HWND CBrightness::CreateCustDialog()
        CRect rect;    
        HWND hwndDialog = NULL;
        CString strFunctionName = _T("CBrightness::OnInitDialog");
        //User inputs
        PreInitCtrl(_T("Brightness"),_T("DialogBox is displayed!"), IDB_WARNING, 53, 
        //Create Dialog with these user inputs
            //Create Ok and CANCEL button on brightness dialog
            m_objOKButton.Create(_T("OK"), WS_CHILD|WS_VISIBLE, CRect(120, 201, 215,240), 
                                   this, IDC_OK_BUTTON);
            m_objCancelButton.Create(_T("CANCEL"), WS_CHILD|WS_VISIBLE,
                                       CRect(240, 201, 350, 240), this, IDC_CANCEL_BUTTON);
            //Return dialog handler
            hwndDialog = this->m_hWnd;
        else //dialog not created successfully; NULL is returned 
            TRACE1("Error in %s(): DialogBox failed to be created!\r\n",strFunctionName);
        return hwndDialog;
  5. If the OK button is added, add the handler for the OK button. In the handler, call the base class OnOK() function. Similarly, for the Cancel button (if added), add the handler and call the base class OnCancel() function.
  6. BEGIN_MESSAGE_MAP(CBrightness, CCustDialogBox)
    //Called when OK button is pressed on modal dialog
    void CBrightness::OnClickOk()
        //Call base class OnOK
    //Called when Cancel button is pressed on modal dialog
    void CBrightness::OnClickCancel()
        //Call base class OnCancel
  7. In the parent dialog, include the header file for the CBrightness dialog class. In its header file, create an object of the CBrightness class. Create a button, and in its button click handler, call the DoModal() function for the CBrightness object.
  8. //In header file
    class CTestModalDialogDlg : public CDialog
          CBrightness m_objBrightness;
    //In source file
    BEGIN_MESSAGE_MAP(CTestModalDialogDlg, CDialog)
    void CTestModalDialogDlg::OnBnClickedButton1()
        // TODO: Add your control notification handler code here
        INT_PTR ret = m_objBrightness.DoModal();
        case IDOK:
            AfxMessageBox(_T("OK is pressed!"),0,0);
        case IDCANCEL:
            AfxMessageBox(_T("Cancel is pressed!"),0,0);        
            AfxMessageBox(_T("Dialog creation failed!"),0,0);

PreInitCtrl Function

void PreInitCtrl(CString strTitle = _T("Dialog Title!"),     //Dialog Title
                 CString strInfo = _T("Dialog Info here!"),  //Dialog text
                 UINT nWarningImgID = 0,              //Icon
                 UINT nTitleBarHeight = 65,                  //TitleBar height
                 COLORREF clrBkgnd = RGB(255,0,0),           //Title Area color
                 COLORREF clrInfoArea = RGB(255,255,255),    //TextArea color
                 COLORREF clrInfoFont = RGB(0,0,0),          //Text Font color
                 COLORREF clrTitleFont  = RGB(255,255,255),  //Title Font color
                 int nTitleFontSize = 20,                    //Title Font size          
                 CString strFont = TEXT("Arial"),            //Text Font
                 DIALOGBOXCORNER eDialogBoxCorner = ROUNDED_CORNE);  //Rounded or sharp 
                                                                     //cornered dialog

Points of Interest

This code was developed after debugging how modal dialogs are created by the MFC Framework. A comparison of both the methods is given below:

MFC FrameworkCustomized Code
Template is used to create dialogThe CreateCustDialog function is called to take user input and create a dialog using the Create() function.
WM_INITDIALOG message sentWM_INITDIALOG not sent. Rather, OK and Cancel buttons are created in the overridden code.
Continuous modal loop is startedContinuous modal loop is started
Modal loop exits on pressing of the OK or Cancel buttonsOnOK and OnCancel are overridden, which internally calls EndDialog to exit the modal loop.


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


About the Author

Software Developer
India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralMy vote of 1 Pin
Syed Javed24-Jan-10 1:41
memberSyed Javed24-Jan-10 1:41 
Generalbug in PreInitCtrl Pin
Mage24-Apr-08 17:02
memberMage24-Apr-08 17:02 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 24 Apr 2008
Article Copyright 2008 by lata07mahi
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid