|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThis 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. BackgroundModal Dialogs in MFC can be created using either the default templates provided by the wizard or the memory defined templates using the Using the CodeThe step-by-step usage of the code is explained below:
Class CBrightness : public CCustDialogBox
{
}
//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; } 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();
}
//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 Functionvoid 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 InterestThis code was developed after debugging how modal dialogs are created by the MFC Framework. A comparison of both the methods is given below:
|
||||||||||||||||||||||||||||||||||||||||||||||||||