简体   繁体   English

可以在 MFC Button 控件上检测到鼠标右键单击事件吗?

[英]Can Right-mouse click events be detected on a MFC Button control?

I am trying to create a dialog with multiple buttons, which change color on left and right clicks respectively.我正在尝试创建一个带有多个按钮的对话框,它们分别在左键和右键单击时改变颜色。
So, how can I handle the right click event for specific buttons ??那么,如何处理特定按钮的右键单击事件?

ON_RBUTTONDOWN doesn't work for specific buttons. ON_RBUTTONDOWN不适用于特定按钮。

As MFC does not allow to trap all the events on the CButton control but there some commonly used events like BN_CLICKED and BN_DOUBLECLICKED. 由于MFC不允许捕获CButton控件上的所有事件,但是存在一些常用事件,例如BN_CLICKED和BN_DOUBLECLICKED。 So to trap a right mouse button events on a CButton MFC you need to derive a new class from the CButton. 因此,要在CButton MFC上捕获鼠标右键事件,您需要从CButton派生一个新类。

MyButton.h MyButton.h

#if !defined(AFX_MYBUTTON_H__46A1ECCC_0FAD_485A_B6B8_C21B6538148E__INCLUDED_)
#define AFX_MYBUTTON_H__46A1ECCC_0FAD_485A_B6B8_C21B6538148E__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
// MyButton.h : header file
// CMyButton window

class CMyButton : public CButton  //CMyButton  =>derive from the CButton.
{
// Construction
public:
    CMyButton();

// Attributes
public:

// Operations
public:

// Overrides
    // ClassWizard generated virtual function overrides
    //{{AFX_VIRTUAL(CMyButton)
    //}}AFX_VIRTUAL

// Implementation
public:
    virtual ~CMyButton();

    // Generated message map functions
protected:
    //{{AFX_MSG(CMyButton)
    afx_msg void OnRButtonUp(UINT nFlags, CPoint point);
    //}}AFX_MSG

    DECLARE_MESSAGE_MAP()
};

#endif // !defined(AFX_MYBUTTON_H__46A1ECCC_0FAD_485A_B6B8_C21B6538148E__INCLUDED_)

MyButton.cpp MyButton.cpp

#include "stdafx.h"
#include "MyButton.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyButton

CMyButton::CMyButton()
{
}

CMyButton::~CMyButton()
{
}

BEGIN_MESSAGE_MAP(CMyButton, CButton)
    //{{AFX_MSG_MAP(CMyButton)
    ON_WM_RBUTTONUP()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyButton message handlers

void CMyButton::OnRButtonUp(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    NMHDR hdr;
    hdr.code = NM_RCLICK;
    hdr.hwndFrom = this->GetSafeHwnd();
    hdr.idFrom = GetDlgCtrlID();
    TRACE("OnRButtonUp");
    this->GetParent()->SendMessage(WM_NOTIFY, (WPARAM)hdr.idFrom, (LPARAM)&hdr);
}

Now in your Dialog class you need to trap the Message that your CMyButton passes. 现在,在Dialog类中,您需要捕获CMyButton传递的消息。 The message passed is NM_RCLICK and you capture it as 传递的消息是NM_RCLICK,您将其捕获为

ON_NOTIFY(NM_RCLICK, IDC_BUTTON1, OnRClicked)

Your member function must be declared with the following prototype: 您的成员函数必须使用以下原型进行声明:

afx_msg void OnRClicked( NMHDR * pNotifyStruct, LRESULT * result );

For more details you can go through link mouse-button-event-handler 有关更多详细信息,您可以通过链接mouse-button-event-handler

While the accepted answer works, I find it much easier to trap WM_CONTEXT_MENU, which is possible for any control with no new classes required, and many fewer lines of added code.虽然接受的答案有效,但我发现捕获 WM_CONTEXT_MENU 更容易,这对于任何不需要新类的控件都是可能的,并且添加的代码行更少。 Here's one example of how to do it:这是如何做到这一点的一个例子:

https://stackoverflow.com/a/63633383/9655696 https://stackoverflow.com/a/63633383/9655696

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

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