如何拦截对话框ESC键的处理?


用VC编写的基于对话框的应用程序,当用户按下ESC按键的时候,系统默认的处理是关闭对话框,如何编程实现截获ESC按键消息?

VC windows

玖·月神殿 11 years, 6 months ago

有几种方法:

  1. 把OnCancel()(ON_COMMAND(IDCANCEL,OnCancel))重载掉,去掉对基类CDialog::OnCancel的调用。
  2. mfc提供了一个PreTranslateMessage()方法,就可以直接屏蔽掉某些消息。
  3. WTL中处理IDCANCEL消息。
   
  BOOL   CMyDlg::PreTranslateMessage(MSG*   pMsg)
  
{
if (pMsg->message ==WM_KEYDOWN)
{
int nVirtKey = (int) wParam;
if (nVirtKey==VK_RETURN)
{
//如果是回车在这里做你要做的事情,或者什么也不作
return TRUE;
}
if (nVirtKey==VK_ESCAPE)
{
//如果是ESC在这里做你要做的事情,或者什么也不作
return TRUE;
}

}
return CDialog::PreTranslateMessage(pMsg);
}

arorua answered 11 years, 6 months ago

Your Answer