Wednesday, August 01, 2007

User Defined message creation

The user defined message can be created by the user and used in MFC application :

ForExample if we want to post our own message, we have to create our own message .

Each and every message have a unique number. Using this number, windows identifies the message.



Normally some messages are predefined. Ex : WM_LBUTTONDOWN



it is having the unique ID . using this ID, the windows takes appropriate action for left button down event.





if we are creating our own message, that will not be mingle with the existing ID.



Assume that WM_LBUTTONDOWN message ID number is 0x0008 .



if we are creating our own message with this ID, the windows will takes appropriate action for the LButtonDown.



To overcome this we have to use it as follows :



#define WM_USERMSG WM_APP + 1

#define WM_USERMSG2 WM_APP + 2



and so on.



How can we handle our own message in an application ?





For Adding handler to the user defined message , MFC has ON_COMMAND() message handler.





Here is the testing of custom message :



#define WM_USERMSG WM_APP + 1



How to send custom message to the application ?...



::SendMessage(

HWND, // Parent window

WM_USERMSG, // User defined message

1, // wparam value...

2 // lparam value



) ;

the user can pass their own data through wparam and lparam parameter.





in win32 application we can handle this message in the window procedure as follows :



switch( uMsg)

{

case WM_USERMSG :

int i = (int) wparam; // value will be 1

int j = (int) lparam; // value will be 2 for the above program...

break;



}



In MFC we can add handler to the user defined message using ON_COMMAND() macro..



afx_msg LRESULT OnUserMessage(WPRAM wp, LPARAM lp);



ON_COMMAND( WM_USERMSG, OnUserMessage)





LRESULT CMFCDlg:: OnUserMessage(WPRAM wp, LPARAM lp)

{

int i = (int)wp; // value will be 1

int j = (int) lp; // value will be 2 // if we used the SendMessage(hwnd,WM_USERMSG, 1,2)



retrurn 0;

}

No comments: