Let my MFC dialog receive keystroke events before its controls (MFC/Win32 equivalent of WinForms “KeyPreview”)

强颜欢笑 提交于 2019-12-24 08:46:58

问题


I have an MFC dialog containing a dozen or so buttons, radio buttons and readonly edit controls.

I'd like to know when the user hits Ctrl+V in that dialog, regardless of which control has the focus.

If this were C#, I could set the KeyPreview proprety and my form would receive all the keystrokes before the individual controls - but how do I do that in my MFC dialog?


回答1:


JTeagle is right. You should override PreTranslateMessage().

// Example
BOOL CDlgFoo::PreTranslateMessage( MSG* pMsg )
{
  // Add your specialized code here and/or call the base class
  if ( pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN )
  {
    int idCtrl= this->GetFocus()->GetDlgCtrlID();
    if ( idCtrl == IDC_MY_EDIT ) {
      // do something <--------------------
      return TRUE; // eat the message
    }
  }

  return CDialog::PreTranslateMessage( pMsg );
}



回答2:


Add a handler to override PreTranslateMessage() in the dialog class, and check the details of the MSG struct received there. Be sure to call the base class to get the right return value, unless you want to eat the keystroke to prevent it going further.



来源:https://stackoverflow.com/questions/1372202/let-my-mfc-dialog-receive-keystroke-events-before-its-controls-mfc-win32-equiva

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!