一些VC编程书上举例时常常是用对话框作窗口,然后在上面放一些控件来演示功能。一般要把OK按钮和Cancel按钮去掉。我想问的为什么执行时还能响应Enter键和ESC键动作使窗口关闭从而退出程序?
下面是拷贝的部分代码,我也不知是不是要改动这部分,望赐教,谢谢!!/////////////////////////////////////////////////////////////////////////////
// CSDApp initializationBOOL CSDApp::InitInstance()
{
AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif CSDDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
} // Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
下面是拷贝的部分代码,我也不知是不是要改动这部分,望赐教,谢谢!!/////////////////////////////////////////////////////////////////////////////
// CSDApp initializationBOOL CSDApp::InitInstance()
{
AfxEnableControlContainer(); // Standard initialization
// If you are not using these features and wish to reduce the size
// of your final executable, you should remove from the following
// the specific initialization routines you do not need.#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif CSDDlg dlg;
m_pMainWnd = &dlg;
int nResponse = dlg.DoModal();
if (nResponse == IDOK)
{
// TODO: Place code here to handle when the dialog is
// dismissed with OK
}
else if (nResponse == IDCANCEL)
{
// TODO: Place code here to handle when the dialog is
// dismissed with Cancel
} // Since the dialog has been closed, return FALSE so that we exit the
// application, rather than start the application's message pump.
return FALSE;
}
一种方法:
重载对话框的PreTranslateMessage函数屏蔽调消息
CxxDlg::PreTranslateMessage(MSG* pMsg)
{
//屏蔽esc键和enter键
if((pMsg->message == WM_KEYDOWN &&
pMsg->wParam == VK_ESCAPE))
return TRUE;
return CDialog::PreTranslateMessage(pMsg);
}
重载OnOk或者OnCancel函数,在其中直接返回
{
// TODO: Add your specialized code here and/or call the base class
if(pMsg->message == WM_KEYDOWN){
switch(pMsg->wParam){
case VK_RETURN://截获回车
return TRUE;
case VK_ESCAPE://截获ESC
return TRUE;
break;
}
}
return CDialog::PreTranslateMessage(pMsg);
}
OnOk()
{
if(...)
{
...
}
else
{
...
CDialog::OnOk();
}
}
{
if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_ESCAPE) return TRUE;
if(pMsg->message==WM_KEYDOWN && pMsg->wParam==VK_RETURN) return TRUE;
else
return CDialog::PreTranslateMessage(pMsg);
}