对话框处理Enter,Esc键相应问题

在类视图里面选择你要实现的类,右键属性,在属性里面找到函数PreTranslateMessage,然后添加PreranslateMessage的消息函数,在PreTranslateMessage的消息函数中添加你要实现处理按键事件的代码。


  1. /******************************************************** 
  2. * 方法名称: PreTranslateMessage(MSG *pMsg) 
  3. * 描述: 该方法主要是处理传递消息的,任何消息产生的消息都会经过这个方法处理, 
  4. * 然后,消息才会到达相应的应用程序中。 
  5. * 调用模块:CDialogEx::PreTranslateMessage(pMsg)父类的对应方法  
  6. * 输入: MSG类型的消息对象指针 
  7. * 输出: 无 
  8. * 返回值: bool值 
  9. *********************************************************/  
  10. BOOL CSerialDlg::PreTranslateMessage(MSG* pMsg)  
  11. {  
  12.     // TODO: Add your specialized code here and/or call the base class  
  13.     // 把Esc和Enter按键事件消息过滤掉,否则该消息会导致对应应用程序调用OnOK()方法,结束应用程序  
  14.     if (pMsg->message == WM_KEYDOWN)  
  15.     {  
  16.         switch(pMsg->wParam)  
  17.         {  
  18.             case VK_ESCAPE: //Esc按键事件  
  19.                 return true;  
  20.             case VK_RETURN: //Enter按键事件  
  21.                 return true;  
  22.             default:  
  23.                 ;  
  24.         }  
  25.     }  
  26.       
  27.     return CDialogEx::PreTranslateMessage(pMsg);  
  28. }  




原文地址:https://www.cnblogs.com/skyhuangdan/p/5486376.html