自定义模态对话框






3.33/5 (5投票s)
具有用户定义外观的模式对话框。
引言
本文/源代码解释了创建自定义模式对话框的过程。在该项目中,整个应用程序都具有自定义的对话框和控件。我需要对话框能够基于用户输入进行自定义,并提供类似模式对话框的功能。结果就是这段代码。该代码是用MFC编写的,并实现为一个类。
背景
可以使用向导提供的默认模板或使用DLGTEMPLATE
结构定义的内存模板在MFC中创建模式对话框。但是,这些选项都不允许创建自定义皮肤。
Using the Code
下面解释了代码的逐步用法
- 使用向导添加一个新的对话框和相应的对话框类(例如,
CBrightness
)。默认情况下,它将从CDialog
派生。在CBrightness
文件(源文件和头文件)中将CDialog
替换为CCustDialogBox
。 - 在
CBrightness
对话框的头文件中包含头文件 *CustDialogBox.h*。 - 如下所示,重写
CreateCustDialog
函数 - 使用
PreInitCtrl()
函数提供自定义参数。 - 然后,使用
WS_POP_UP
样式,父窗口句柄,对话框ID和定义对话框大小的矩形调用Create()
函数。 - 如果成功创建了对话框,则创建“确定”和“取消”按钮。
- 如果添加了“确定”按钮,请添加“确定”按钮的处理程序。在处理程序中,调用基类
OnOK()
函数。同样,对于“取消”按钮(如果已添加),添加处理程序并调用基类OnCancel()
函数。 - 在父对话框中,包含
CBrightness
对话框类的头文件。在其头文件中,创建一个CBrightness
类的对象。创建一个按钮,并在其按钮单击处理程序中,为CBrightness
对象调用DoModal()
函数。
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 函数
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
关注点
此代码是在调试MFC框架如何创建模式对话框之后开发的。下面给出了两种方法的比较
MFC框架 | 自定义代码 |
模板用于创建对话框 | 调用CreateCustDialog 函数以获取用户输入,并使用Create() 函数创建对话框。 |
发送WM_INITDIALOG 消息 |
未发送WM_INITDIALOG 。相反,在重写的代码中创建“确定”和“取消”按钮。 |
启动连续模式循环 | 启动连续模式循环 |
按下“确定”或“取消”按钮时,模式循环退出 | OnOK 和OnCancel 被重写,它们在内部调用EndDialog 以退出模式循环。 |