优信二手车:“优”无品质 “信”失归途
2025-11-07
int AFXAPI AfxMessageBox(LPCTSTR lpszText, UINT nType, UINT nIDHelp)
{
CWinApp* pApp = AfxGetApp();
if (pApp != NULL)
return pApp->DoMessageBox(lpszText, nType, nIDHelp);
else
return pApp->CWinApp::DoMessageBox(lpszText, nType, nIDHelp);
}
重载 DoMessageBox 后我们得到了什么呢? int COwnAfxMessageBoxApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
{
return CWinApp::DoMessageBox(lpszPrompt, nType, nIDPrompt);
}
其中 CWinApp::DoMessageBox 就是对 Windows API 中的 ::MessageBox 的封装,再此不多叙。 int COwnAfxMessageBoxApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
{
OwnMessageBox(lpszPrompt, nType, nIDPrompt);
// return CWinApp::DoMessageBox(lpszPrompt, nType, nIDPrompt);
}
这样的写法没有问题,但也许有的时候仍然需要弹出标准的 MessageBox 需要用户确认,怎么设计才更加合理呢?AfxMessageBox 的第二个参数 nType 是指定 MessageBox 的类型,在 Winuser.h 中定义了一些标准的类型,请注意 nType 是 UINT 类型的,而标准类型的定义才不到10个,你完全可以添加自己的 MessageBox 类型!在 OwnAfxMessageBoxApp.h 中定义:#define MB_USERDEFINE 0x10000000你的 DoMessageBox 处理函数:
int COwnAfxMessageBoxApp::DoMessageBox(LPCTSTR lpszPrompt, UINT nType, UINT nIDPrompt)
{
if (MB_USERDEFINE == nType)
{
OwnMessageBox(lpszPrompt, nType, nIDPrompt);
return TRUE;
}
return CWinApp::DoMessageBox(lpszPrompt, nType, nIDPrompt);
}
你的调用代码:void COwnAfxMessageBoxDlg::OnOK()
{
::AfxMessageBox("我是标准的 AfxMessageBox!");
::AfxMessageBox("我是被重载的 AfxMessageBox!", MB_USERDEFINE);
// CDialog::OnOK();
}
到这里原理部分已经讲完了,具体的实现方法请查看代码。感谢 CSDN 的 bongny (金辉)提供了思路。 评论 {{userinfo.comments}}
{{child.content}}



{{question.question}}
提交