Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在MFC/Visual C++;_C++_Visual C++_Mfc - Fatal编程技术网

C++ 在MFC/Visual C++;

C++ 在MFC/Visual C++;,c++,visual-c++,mfc,C++,Visual C++,Mfc,我有一个模型对话框。我通过按ESC按钮覆盖了OnCancel函数以停止意外退出。但这带来了一个新问题。我无法使用关闭(x)按钮关闭应用程序。我的职能是: void MyDlg::OnCancel() { } 关闭对话框实际上会取消它。由于在OnCancel处理程序中禁用了close,因此不会发生任何事情 要仅禁用ESC,您需要预处理键盘消息: BOOL CYourDlg::PreTranslateMessage(MSG* pMsg) { switch ( pMsg->message

我有一个模型对话框。我通过按ESC按钮覆盖了OnCancel函数以停止意外退出。但这带来了一个新问题。我无法使用关闭(x)按钮关闭应用程序。我的职能是:

void MyDlg::OnCancel()
{
}

关闭对话框实际上会取消它。由于在OnCancel处理程序中禁用了close,因此不会发生任何事情

要仅禁用ESC,您需要预处理键盘消息:

BOOL CYourDlg::PreTranslateMessage(MSG* pMsg)
{
   switch ( pMsg->message )
   {
       case WM_KEYDOWN:

           switch( pMsg->wParam )
           {
               case VK_ESCAPE:
               case VK_CANCEL: return true;
           }
           break;
   }

   return CDialog::PreTranslateMessage(pMsg);
}

OnCancel是通过按x按钮或按escape来调用的,在函数中无法判断。您可以添加一个消息对话框,询问用户是否确定要退出,或者专门查看捕获escape键。我实现了您的解决方案。它很好用。。。但我还有一个问题,当按下ESC按钮时如何停止退出?我认为最好用另一种方式:“typedef CDialog base_class”,然后调用“base_class::OnCancel();”而不是使用VC特定的超级关键字。@krish只需调用base OnCancel(),然后您就需要退出对话框了。例如,使用“如果”语句。@GazTheDestroyer感谢您提供更好的解决方案,它现在正在工作。@Raxillan我同意GazTheDestroyer的解决方案,谢谢您的回答