2

我想跟踪单击编辑控件的单击事件。我覆盖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?

4

1 回答 1

5

你需要这个:

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 语句有点尴尬。以下代码更简洁 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
}
于 2016-01-28T14:02:54.380 回答