Right mouse button event handler

Discussion in 'MFC' started by shabbir, Jul 9, 2005.

  1. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    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:
    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:
    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:

  2. naresha1

    naresha1 New Member

    Joined:
    Jul 31, 2006
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    _beginthread

    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
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Did you follow all the steps correctly. You probably have a call to a function _fun which you have not declared.
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    naresha1 Please refer to [thread=1096]Error Undeclared identifier error _beginthread[/thread].
     
  5. parvez.yu

    parvez.yu New Member

    Joined:
    Feb 14, 2008
    Messages:
    100
    Likes Received:
    0
    Trophy Points:
    0
    i m also getting an error
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    What is the error
     
  7. parvez.yu

    parvez.yu New Member

    Joined:
    Feb 14, 2008
    Messages:
    100
    Likes Received:
    0
    Trophy Points:
    0
    now its done actually there was some declaration problem
     
  8. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    So... yeah.

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

    Are they complete files in there own, or what?
     
  9. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  10. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Well it didn't compile for me...

    Visual Studio 2008 Professional
     
    Last edited: Jun 14, 2009
  11. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    They compile for me on VS 2005 and I do not have 2008. What error you get ?
     
  12. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Sorry for the late reply;
    Code:
    1>------ Build started: Project: MyButton, Configuration: Debug Win32 ------
    1>Compiling...
    1>MyButton.cpp
    1>c:\mybutton\mybutton\mybutton.h(14) : error C2504: 'CButton' : base class undefined
    1>c:\mybutton\mybutton\mybutton.h(37) : error C2144: syntax error : 'void' should be preceded by ';'
    1>c:\mybutton\mybutton\mybutton.h(37) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\mybutton\mybutton\mybutton.h(37) : error C2061: syntax error : identifier 'CPoint'
    1>c:\mybutton\mybutton\mybutton.h(41) : error C2143: syntax error : missing ';' before '}'
    1>c:\mybutton\mybutton\mybutton.h(41) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\mybutton\mybutton\mybutton.h(41) : warning C4183: 'DECLARE_MESSAGE_MAP': missing return type; assumed to be a member function returning 'int'
    1>c:\mybutton\mybutton\mybutton.cpp(25) : error C2061: syntax error : identifier 'CButton'
    1>c:\mybutton\mybutton\mybutton.cpp(27) : error C3646: 'ON_WM_RBUTTONUP' : unknown override specifier
    1>c:\mybutton\mybutton\mybutton.cpp(29) : error C3646: 'END_MESSAGE_MAP' : unknown override specifier
    1>c:\mybutton\mybutton\mybutton.cpp(29) : error C2091: function returns function
    1>c:\mybutton\mybutton\mybutton.cpp(34) : error C2091: function returns function
    1>c:\mybutton\mybutton\mybutton.cpp(34) : error C2144: syntax error : 'void' should be preceded by ';'
    1>c:\mybutton\mybutton\mybutton.cpp(34) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\mybutton\mybutton\mybutton.cpp(34) : error C2061: syntax error : identifier 'CPoint'
    1>c:\mybutton\mybutton\mybutton.cpp(38) : error C2065: 'NM_RCLICK' : undeclared identifier
    1>c:\mybutton\mybutton\mybutton.cpp(39) : error C2039: 'GetSafeHwnd' : is not a member of 'CMyButton'
    1>        c:\mybutton\mybutton\mybutton.h(13) : see declaration of 'CMyButton'
    1>c:\mybutton\mybutton\mybutton.cpp(40) : error C2660: 'GetDlgCtrlID' : function does not take 0 arguments
    1>c:\mybutton\mybutton\mybutton.cpp(41) : error C3861: 'TRACE': identifier not found
    1>c:\mybutton\mybutton\mybutton.cpp(42) : error C2039: 'GetParent' : is not a member of 'CMyButton'
    1>        c:\mybutton\mybutton\mybutton.h(13) : see declaration of 'CMyButton'
    1>c:\mybutton\mybutton\mybutton.cpp(42) : error C2227: left of '->SendMessageW' must point to class/struct/union/generic type
    1>Build log was saved at "file://c:\MyButton\MyButton\Debug\BuildLog.htm"
    1>MyButton - 20 error(s), 1 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
     
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    IF CButton is not defined I guess you have to make sure you are able to compile MFC Programs
     
  14. naimish

    naimish New Member

    Joined:
    Jun 29, 2009
    Messages:
    1,043
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Engineer
    Location:
    On Earth
    Cool one, need to check it first :) will reply soon
     
  15. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Which project should I be using?

    [​IMG]

    Have been using Windows Forms... or win32 project.
     
  16. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I dont see Windows Form in there but you should be using MFC application because CButton and Win32 does not go together..
     
  17. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Windows Forms Application

    2nd one down 1st column

    Makes it very easy to design a GUI...
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice