Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Customised Modal Dialog

0.00/5 (No votes)
23 Apr 2008 1  
Modal dialog with a user-defined look and feel.

Introduction

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.

Background

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, 
                    RGB(254,149,16),RGB(255,255,255),RGB(0,0,0),RGB(0,0,0),20,_T("Arial"),
                    CCustDialogBox::ROUNDED_CORNER);
    
        //Create Dialog with these user inputs
        if(Create(_T(""),WS_POPUP,CRect(180,75,640,340),AfxGetMainWnd(),IDD_BRIGHTNESS,0))
        {
            //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)
         ON_BN_CLICKED(IDC_OK_BUTTON,&CBrightness::OnClickOk)
         ON_BN_CLICKED(IDC_CANCEL_BUTTON,&CBrightness::OnClickCancel)
    END_MESSAGE_MAP()
    
    //Called when OK button is pressed on modal dialog
    void CBrightness::OnClickOk()
    {
        //Call base class OnOK
        CCustDialogBox::OnOK();
    }
    
    //Called when Cancel button is pressed on modal dialog
    void CBrightness::OnClickCancel()
    {
        //Call base class OnCancel
        CCustDialogBox::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
    {
        ..
        protected:
          CBrightness m_objBrightness;
        ..
    };
    
    //In source file
    BEGIN_MESSAGE_MAP(CTestModalDialogDlg, CDialog)
       ...
       ON_BN_CLICKED(IDC_BUTTON1,&CTestModalDialogDlg::OnBnClickedButton1)
    END_MESSAGE_MAP()
    
    void CTestModalDialogDlg::OnBnClickedButton1()
    {
        // TODO: Add your control notification handler code here
        INT_PTR ret = m_objBrightness.DoModal();
        switch(ret)
        {
        case IDOK:
            AfxMessageBox(_T("OK is pressed!"),0,0);
            break;
        case IDCANCEL:
            AfxMessageBox(_T("Cancel is pressed!"),0,0);        
               break;
        default:
            AfxMessageBox(_T("Dialog creation failed!"),0,0);
            break;
        }
    }

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 Framework Customized Code
Template is used to create dialog The CreateCustDialog function is called to take user input and create a dialog using the Create() function.
WM_INITDIALOG message sent WM_INITDIALOG not sent. Rather, OK and Cancel buttons are created in the overridden code.
Continuous modal loop is started Continuous modal loop is started
Modal loop exits on pressing of the OK or Cancel buttons OnOK and OnCancel are overridden, which internally calls EndDialog to exit the modal loop.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here