代码之家  ›  专栏  ›  技术社区  ›  javad

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

  •  2
  • javad  · 技术社区  · 10 年前

    我想跟踪在编辑控件上单击鼠标左键的事件。 我超控 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;
           }
        }
    

    问题是,当我单击编辑控件时,所有其他控件都会被禁用(例如,按钮不会响应单击等)

    我如何解决这个问题?或者如何跟踪编辑框上的单击通知N?

    1 回复  |  直到 10 年前
        1
  •  5
  •   Jabberwocky    10 年前

    你需要这个:

    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
    }
    

    顺便说一句,在这里使用switch语句有点奇怪。以下代码更简洁,除非您想添加比仅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
    }
    
    推荐文章