Right mouse button event handler

shabbir's Avatar author of Right mouse button event handler
This is an article on Right mouse button event handler in MFC.
MFC does not allow to trap all the events on the CButton control but some commonly used events like BN_CLICKED and BN_DOUBLECLICKED and so to trap a right mouse button events on a CButton MFC you need to derive a new class from the CButton.

MyButton.h
Code: CPP
class CMyButton : public 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()
};
MyButton.cpp
Code: CPP
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. The message passed is NM_RCLICK and you capture it as
Code:
  ON_NOTIFY(NM_RCLICK, IDC_BUTTON1, OnRClicked)
  ON_NOTIFY(NM_RCLICK, IDC_BUTTON2, OnRClicked)
Your member function must be declared with the following prototype:

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

afx_msg void OnRClicked( NMHDR * pNotifyStruct, LRESULT * result )
Attached Files
File Type: zip MyButton.zip (1.2 KB, 96 views)
Newbie Member
31Jul2006,15:59   #2
naresha1's Avatar
iam getting undeclared error in _beginthread
process.h included
this has been done
"Project|Settings|C/C++|Code generation|Use runtime library: (Debug)
> multithreaded"
getting unresolved error in linking:_fun()@1

solution plz
Go4Expert Founder
31Jul2006,16:36   #3
shabbir's Avatar
Did you follow all the steps correctly. You probably have a call to a function _fun which you have not declared.
Go4Expert Founder
31Jul2006,21:15   #4
shabbir's Avatar
naresha1 Please refer to Error Undeclared identifier error _beginthread.
Ambitious contributor
6Mar2008,15:02   #5
parvez.yu's Avatar
i m also getting an error
Go4Expert Founder
6Mar2008,16:03   #6
shabbir's Avatar
Quote:
Originally Posted by parvez.yu
i m also getting an error
What is the error
Ambitious contributor
6Mar2008,16:47   #7
parvez.yu's Avatar
now its done actually there was some declaration problem
Contributor
12Jun2009,11:20   #8
Panarchy's Avatar
So... yeah.

I downloaded the files, your files, but can't compile them.

Are they complete files in there own, or what?
Go4Expert Founder
12Jun2009,15:44   #9
shabbir's Avatar
Yes
Contributor
14Jun2009,07:41   #10
Panarchy's Avatar
Well it didn't compile for me...

Visual Studio 2008 Professional

Last edited by Panarchy; 14Jun2009 at 08:03..