简体   繁体   English

如何在编辑控件上获得左键单击通知?

[英]How to get left click notification on an edit control?

I want to track event of single left-click on edit control. 我想跟踪单击编辑控件的事件。 I override PretranslateMessage function as below: 我重写PretranslateMessage函数,如下所示:

BOOL CMyClass::PreTranslateMessage(Msg* pMsg)
    {
       switch(pMsg->message)

       case WM_LBUTTONDOWN:
       {
          CWnd* pWnd = GetFocus();
          if (pWnd->GetDlgCtrlID == MY_EDIT_CTRL_ID)
             {
                //Do some thing
             }
          break;
       }
    }

The problem is that when I click on the edit control, all other control become disabled (for example buttons don't respond to clicks etc.) 问题是,当我单击编辑控件时,所有其他控件都被禁用(例如,按钮不响应单击等)。

How can I fix this problem? 我该如何解决这个问题? Or how can I track click notificationN on edit box? 或如何在编辑框中跟踪单击通知N?

You need this: 你需要这个:

BOOL CMyClass::PreTranslateMessage(MSG* pMsg)
{
  switch(pMsg->message)
  {
    case WM_LBUTTONDOWN:
    {
      CWnd* pWnd = GetFocus();
      if (pWnd->GetDlgCtrlID() == MY_EDIT_CTRL_ID)  // << typo corrected here
      {
         //Do some thing
      }
      break;
    }
  } 

  return __super::PreTranslateMessage(pMsg);  //<< added
}

BTW its a bit awkword to use a switch statement here. 顺便说一句,在这里使用switch语句有点措辞。 Following code is cleaner IMO, unless you want to add morecases than only WM_LBUTTONDOWN: 以下代码是更干净的IMO,除非您要添加的案例比仅WM_LBUTTONDOWN多:

BOOL CMyClass::PreTranslateMessage(MSG* pMsg)
{
  if (pMsg->message == WM_LBUTTONDOWN)
  {
    CWnd* pWnd = GetFocus();

    if (pWnd->GetDlgCtrlID() == MY_EDIT_CTRL_ID)
    {
       //Do some thing
    }
  } 

  return __super::PreTranslateMessage(pMsg);  //<< added
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM