让我的MFC对话框在其控件之前接收按键事件(MFC / Win32等效于WinForms“KeyPreview”) [英] Let my MFC dialog receive keystroke events before its controls (MFC/Win32 equivalent of WinForms "KeyPreview")

查看:223
本文介绍了让我的MFC对话框在其控件之前接收按键事件(MFC / Win32等效于WinForms“KeyPreview”)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含十几个按钮,单选按钮和只读编辑控件的MFC对话框。



我想知道用户什么时候点击Ctrl + V在该对话框中,无论哪个控件都有重点。



如果这是C#,我可以设置 KeyPreview proprety和我的表单将接收个人控制之前的所有按键 - 但是如何在MFC对话框中执行此操作?

解决方案

JTeagle是对的您应该覆盖 PreTranslateMessage()

  //示例
BOOL CDlgFoo :: PreTranslateMessage(MSG * pMsg)
{
//在此处添加您的专用代码和/或调用基类
if(pMsg-> message == WM_KEYDOWN& & pMsg-> wParam == VK_RETURN)
{
int idCtrl = this-> GetFocus() - > GetDlgCtrlID();
if(idCtrl == IDC_MY_EDIT){
// do something< --------------------
return TRUE; //吃消息
}
}

return CDialog :: PreTranslateMessage(pMsg);
}


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?

解决方案

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 );
}

这篇关于让我的MFC对话框在其控件之前接收按键事件(MFC / Win32等效于WinForms“KeyPreview”)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆