简体   繁体   English

如何在基于 MFC 对话框的应用程序中为复选框捕获 MouseMove 事件?

[英]How to capture MouseMove event in a MFC Dialog Based application for a checkbox?

My application is a VC6 MFC dialog based application with multiple property pages.我的应用程序是具有多个属性页的基于 VC6 MFC 对话框的应用程序。

I have to capture a mousemove event over a control, for example Checkbox.我必须在控件上捕获 mousemove 事件,例如复选框。

How can I capture the mousemove events over a checkbox in MFC?如何在 MFC 中的复选框上捕获 mousemove 事件?

A checkbox is a button control (eg. CWnd).复选框是一个按钮控件(例如 CWnd)。 Derive your own class from CCheckBox and handle the OnMouseMove event.从 CCheckBox 派生您自己的类并处理 OnMouseMove 事件。

Per request...assuming a class derived from CButton...每个请求......假设一个从 CButton 派生的类......

BEGIN_MESSAGE_MAP(CMyCheckBox, CButton)
    ON_WM_MOUSEMOVE()
END_MESSAGE_MAP()


void CMyCheckBox::OnMouseMove(UINT nFlags, CPoint point)
    {
    // TODO: Add your message handler code here and/or call default

    CButton::OnMouseMove(nFlags, point);
    }

Thanks for your replies.. I found a way to get the mousemove event for my app.感谢您的回复.. 我找到了一种方法来为我的应用程序获取 mousemove 事件。

WM_SETCURSOR windows message gets the mouse move. WM_SETCURSOR windows 消息使鼠标移动。 It returns the Cwnd pointer for a control and the dialog.它返回控件和对话框的 Cwnd 指针。

Find my code below.在下面找到我的代码。

BOOL CMyDialog::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message)
{
CWnd* pWndtooltip = GetDlgItem(IDC_STATIC_TOOLTIP); 

if (pWnd != this)
{
    if  (IDC_SN_START_ON == pWnd->GetDlgCtrlID())
        pWndtooltip->ShowWindow(SW_SHOW);

}
else
    pWndtooltip->ShowWindow(SW_HIDE);   

SetCursor(AfxGetApp()->LoadStandardCursor(IDC_ARROW));


return true;

} }

I found in @raj's OnSetCursor() code, that the associated Member variable for IDC_STATIC_TOOLTIP is that variable to which you assign the desired tool tip text.我在OnSetCursor()OnSetCursor()代码中发现,IDC_STATIC_TOOLTIP 的关联成员变量是您分配所需工具提示文本的变量。 For example, if the associated variable is m_strToolTip, assign the desired text to display during the hovering event as follows:例如,如果关联变量是 m_strToolTip,则分配所需的文本以在悬停事件期间显示,如下所示:

m_strToolTip.Format("%s", "Tool tip text goes here");

I also found that UpdateData() was required upon entry into the event handler and UpdateData(FALSE) was required before the return.我还发现UpdateData()在进入事件处理程序,所以需要UpdateData(FALSE)返回之前需要。 The SetCursor() call seems to have no effect when commented. SetCursor()调用在评论时似乎没有效果。

You can also override CDialog::PreTranslateMessage :您还可以覆盖CDialog::PreTranslateMessage

BOOL CSomeDlg::PreTranslateMessage(MSG* pMsg)
{
  if (pMsg->message == WM_MOUSEMOVE && pMsg->hwnd == m_checkBox->m_hWnd)
  {
    ...
  }

  return CDialog::PreTranslateMessage(pMsg);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM