MFC的编辑框响应双击事件的三种方法
一. PreTranslateMessage结合位置信息法
在类中重写下面函数:
virtual BOOL PreTranslateMessage(MSG* pMsg) override
{
if (pMsg->message == WM_LBUTTONDBLCLK)
{
CEdit* ptr = (CEdit*)GetDlgItem(IDC_Edit);
CRect rect; ptr->GetWindowRect(&rect);
if (rect.PtInRect(pMsg->pt))
{
//具体事务
}
}
return CDialog::PreTranslateMessage(pMsg);
}
二. 子类继承法
创建CMyEdit类, 并创建一个其变量, 且与使用的编辑框进行DDX关联, 接着写双击事件的代码, 如下:
class CMyEdit : public CEdit
{
public:
DECLARE_MESSAGE_MAP()
afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
};
BEGIN_MESSAGE_MAP(CMyEdit, CEdit)
ON_WM_LBUTTONDBLCLK()
END_MESSAGE_MAP()
void CMyEdit::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO:在此添加消息处理程序代码和/或调用默认值
CEdit::OnLButtonDblClk(nFlags, point);
}
三. 子类发消息法
class CMyEdit : public CEdit
{
protected:
virtual BOOL PreTranslateMessage(MSG * pMsg) override
{
if (pMsg->message == WM_LBUTTONDBLCLK)
{
//AfxWndProc接收BN_DOUBLECLICKED函数,并处理
AfxWndProc(
GetParent()->GetSafeHwnd(),
WM_COMMAND,
(WPARAM)((BN_DOUBLECLICKED << 16) | GetDlgCtrlID()),
(LPARAM)GetSafeHwnd());
}
return __super::PreTranslateMessage(pMsg);
}
};或者使用:
//PostMessage发送系统消息BN_DOUBLECLICKED并处理
::PostMessage(
GetParent()->GetSafeHwnd(),
WM_COMMAND,
(BN_DOUBLECLICKED << 16) | GetDlgCtrlID(),
(LPARAM)GetSafeHwnd());
或者使用:
protected:
virtual BOOL PreTranslateMessage(MSG * pMsg) override
{
//此PostMessage发送控件原生消息BN_DOUBLECLICKED并处理
if (pMsg->message == WM_LBUTTONDBLCLK)
{
if (m_hWnd)
{
::PostMessage(
::GetParent(m_hWnd),
WM_COMMAND,
(WPARAM)((EM_LBUTTONDBLCLK() << 16) | (WPARAM)::GetDlgCtrlID(m_hWnd)),
(LPARAM)GetSafeHwnd());
}
}
return __super::PreTranslateMessage(pMsg);
}
可以使用下面的反射进行验证: 反射消息将先被编辑框捕获, 然后再传到其父类中去;
protected:
BOOL On_EM_LBUTTONDBLCLK() { return 0; }
protected:
DEFINE_MESSAGE_MAP_BGN()
{
WM_COMMAND + WM_REFLECT_BASE, (WORD)CEdit_ResponseLButtonDblClick::EM_LBUTTONDBLCLK(), 0, 0, AfxSigCmd_b,
(AFX_PMSG)(static_cast<BOOL(AFX_MSG_CALL CCmdTarget::*)(void)> (&CEdit_ResponseLButtonDblClick::On_EM_LBUTTONDBLCLK))
},
页:
[1]