Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / MFC
Article

Customised Modal Dialog

Rate me:
Please Sign up or sign in to vote.
3.33/5 (5 votes)
23 Apr 2008CPOL2 min read 43.6K   1.5K   26   2
Modal dialog with a user-defined look and feel.

Image 1

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

C++
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.

License

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


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

Comments and Discussions

 
GeneralMy vote of 1 Pin
Syed J Hashmi24-Jan-10 0:41
Syed J Hashmi24-Jan-10 0:41 
Generalbug in PreInitCtrl Pin
m@ge24-Apr-08 16:02
m@ge24-Apr-08 16: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.