Go4Expert

Go4Expert (http://www.go4expert.com/)
-   Win32 (http://www.go4expert.com/articles/win32-tutorials/)
-   -   Windows Message Handling (http://www.go4expert.com/articles/windows-message-handling-t15467/)

coderzone 11Dec2008 21:42

Windows Message Handling
 

Introduction



Windows communicate with the application by sending messages. There are many Windows messages and each message is represented by a unique integer value.In WINDOWS.H there are standard names for these messages.

There are some common Windows messages like:-
WM_CHAR,WM_PAINT,WM_MOVE,WM_CLOSE,WM_LBUTTONUP,WM_ LBUTTONDOWN,WM_COMMAND,WM_SIZE.

Messages are always accompanied by other values that contain additional,related in formation.This information includes things like cursor or mouse coordinates etc.Windows interacts with your programs through messages.Specifically each time when event occurs a message is sent to program which identifies the event. As there are large number of messages so it not necessary for the program to respond allthe messages so MFC has a default message handler. In a normal Windows program your program's window procedure receives each message and also any additional information which is related to that message .Inside the window procedure,a large switch statement determines what type of message has been received and then processes it accordingly.While using MFC responding messages is much easier.

MFC provides a set of predefined message handler functions that your program may implement. If your program implements one of these handlers then that function will be called whenever its associated message is received.When a message has an additional information associated to it this information will be passed to message handler as an argument.

To respond to a message ,your program must perform these three steps:-

1. The message macro corresponding to the message must be added to your program's message map.

2. The prototype for the message handler must be added to the window class that will process the message.

3. Implement the message handler associated with the message.

Adding Message Macros



MFC message macros have the same name as standard window messages,except that they all begin with ON_ and end with a set of parentheses.There is one exception to this rule: the WM_COMMAND message uses ON_COMMAND for its message macro. This is because WM_COMMAND is a rather special windows message and it requires unique treatement by MFC.

Here are some MFC message macros:-

ON_WM_CHAR,ON_WM_PAINT,ON_WM_MOVE,ON_WM_CLOSE,ON_W M_LBUTTONUP,ON_WM_LBUTTONDOWN,ON_WM_COMMAND,ON_WM_ SIZE. To add a message macro to your message map,simply include it between BEGIN_MESSAGE_MAP and END_MESSAGE_MAP.

Each message that your program responds to must be associated with a message handler. All message handlers are members of the CWND class, and may be overridden by your program. As a general rule name of the message handler is constructed using the name of the message preceded by ON. For example , the handler for WM_CHAR IS OnChar(),the handler for WM_LBUTTONDOWN is OnLButtonDown().

To respond to the message one must add the message handler to the window class that your program defines and that is done by including its prototype in the class declaration.

Implementing Message Handlers

Implementing message handlers depends on two things:-

1.Nature of the message.

2.Meaning of the message for your program.

shabbir 6Jan2009 12:25

Re: Windows Message Handling
 
Nomination for article of the month for December 2008 Started.

rajeev_rsd 16Sep2009 06:46

Re: Windows Message Handling
 
Good Explaination, add some code example.

slymart 26Apr2011 04:48

Re: Windows Message Handling
 
message are precised, context to a beginer. Thanks alot man.

JECOB 10Jun2011 14:25

Re: Windows Message Handling
 
Hi evryone,


Good expriaratins is don't missing text and simple wordings.

slymart 10Jun2011 15:22

Re: Windows Message Handling
 
Thanks obaid for the good explanation.

slymart 10Jun2011 15:32

Re: Windows Message Handling
 
What can one do to solve this window 32 message; "dll file missing"?

Scripting 21Mar2012 00:45

Re: Windows Message Handling
 
The most well written article i have read in this section! Hats off.


All times are GMT +5.5. The time now is 20:10.