这里采用重载PreTranslateMessage的方法:
BOOL CStructureExtractionDlg::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if( pMsg->wParam == VK_ESCAPE )
{
// Escape key clicked. Pop a MessageBox.
if(MessageBox(_T("确定要退出吗?"), _T("提示"), MB_DEFBUTTON2|MB_OKCANCEL) == IDOK)
{
// Have clicked the OK button. Application is to exit.
OnCancel();
}
}
}
return FALSE; // Messages should be dispatched.
}
两个if判断语句确定是否按下了Esc键,若是,就弹出MessageBox,风格是MB_OKCANCEL的,当点击OK按钮时(等于IDOK),调用CDialog::OnCancel()退出程序。
注意如果不是按下Esc事件,或者点击了“取消”按钮,那么程序还要保持运行,这时PreTranslateMessage的返回值应为FALSE;如果返回TRUE,会屏蔽所有点击事件,程序窗口无响应。
下面是MSDN上对于该函数返回值的说明:
Return Value:
Nonzero if the message was translated and should not be dispatched; 0 if the message was not translated and should be dispatched.