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:
- 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).
Class CBrightness : public CCustDialogBox
{
}
- Include the header file CustDialogBox.h in the
CBrightness
dialog’s header file.
- Override the
CreateCustDialog
function as shown below:
- Use the
PreInitCtrl()
function to provide customized parameters.
- 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.
- If dialog is created successfully, then create the OK and Cancel buttons.
Class CBrightness : public CCustDialogBox
{
..
HWND CreateCustDialog;
..
}
HWND CBrightness::CreateCustDialog()
{
CRect rect;
HWND hwndDialog = NULL;
CString strFunctionName = _T("CBrightness::OnInitDialog");
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);
if(Create(_T(""),WS_POPUP,CRect(180,75,640,340),AfxGetMainWnd(),IDD_BRIGHTNESS,0))
{
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);
hwndDialog = this->m_hWnd;
}
else
{
TRACE1("Error in %s(): DialogBox failed to be created!\r\n",strFunctionName);
}
return hwndDialog;
}
- 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.
BEGIN_MESSAGE_MAP(CBrightness, CCustDialogBox)
ON_BN_CLICKED(IDC_OK_BUTTON,&CBrightness::OnClickOk)
ON_BN_CLICKED(IDC_CANCEL_BUTTON,&CBrightness::OnClickCancel)
END_MESSAGE_MAP()
void CBrightness::OnClickOk()
{
CCustDialogBox::OnOK();
}
void CBrightness::OnClickCancel()
{
CCustDialogBox::OnCancel();
}
- 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.
class CTestModalDialogDlg : public CDialog
{
..
protected:
CBrightness m_objBrightness;
..
};
BEGIN_MESSAGE_MAP(CTestModalDialogDlg, CDialog)
...
ON_BN_CLICKED(IDC_BUTTON1,&CTestModalDialogDlg::OnBnClickedButton1)
END_MESSAGE_MAP()
void CTestModalDialogDlg::OnBnClickedButton1()
{
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!"),
CString strInfo = _T("Dialog Info here!"),
UINT nWarningImgID = 0,
UINT nTitleBarHeight = 65,
COLORREF clrBkgnd = RGB(255,0,0),
COLORREF clrInfoArea = RGB(255,255,255),
COLORREF clrInfoFont = RGB(0,0,0),
COLORREF clrTitleFont = RGB(255,255,255),
int nTitleFontSize = 20,
CString strFont = TEXT("Arial"),
DIALOGBOXCORNER eDialogBoxCorner = ROUNDED_CORNE);
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. |