Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Send & Receive a WM_USER Message? 1

Status
Not open for further replies.

panioti

Programmer
Jan 5, 2005
15
US
I want to send a user-defined message, and process it in the destination Class.

(CWnd*) pDest->SendMessage(WM_USER,0,0);

This compiles with no error and will presumably send the msg.

I don't know how to map the msg at the receiving class.
Class Wizard doesn't list WM_USER as a message ID, so I'll have to code it myself.
 
>Class Wizard doesn't list WM_USER as a message ID, so I'll have to code it myself.

Indeed.

First of all, I'd recommend to base your user defined messages on WM_APP rather that WM_USER because MFC uses WM_USER stuff. To be sure you have unique message id's use WM_APP.

Anyway...

In the .h file
Code:
class CMyWnd  // ... etc
{
    //{{AFX_MSG(CMyWnd)
    //  ...
    afx_msg LRESULT OnCallSomeMethod(WPARAM, LPARAM);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()

In the .cpp file:
Code:
BEGIN_MESSAGE_MAP(CMyWnd, CWnd)
    //{{AFX_MSG_MAP(CMyWnd)
    ...
    ON_MESSAGE(MYMESS_CALL_SOME_METHOD, OnCallSomeMethod)
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

LRESULT CMyWnd::OnCallSomeMethod(WPARAM wp, LPARAM lp)
{
    // Whatever...
    return 0;
}

Somewhere accessible:
Code:
#define MYMESS_CALL_SOME_METHOD (WM_APP + 1)

// or make it a const unsigned int if you want to avoid macros

Call it
Code:
theWnd->SendMessage(MYMESS_CALL_SOME_METHOD , 0, 0):

(i prefer typed constants rather than macros, hence the static const)

/Per
[sub]
www.perfnurt.se[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top