c++mfceditcontrol

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:

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?


Solution

  • 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. Following code is cleaner IMO, unless you want to add morecases than only 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
    }