jav*_*vad 2 c++ mfc editcontrol
我想跟踪单击左键单击编辑控件的事件.我重写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;
}
}
Run Code Online (Sandbox Code Playgroud)
问题是当我点击编辑控件时,所有其他控件都被禁用(例如按钮不响应点击等)
我该如何解决这个问题?或者如何在编辑框中跟踪点击通知?
你需要这个:
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
}
Run Code Online (Sandbox Code Playgroud)
BTW在这里使用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
}
Run Code Online (Sandbox Code Playgroud)