1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Windows Message Handling

Discussion in 'Win32' started by coderzone, Dec 11, 2008.

  1. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0

    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_WM_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.
     
    Scripting and slymart like this.
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
  3. rajeev_rsd

    rajeev_rsd New Member

    Joined:
    Sep 8, 2009
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Good Explaination, add some code example.
     
  4. slymart

    slymart New Member

    Joined:
    Apr 25, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    message are precised, context to a beginer. Thanks alot man.
     
  5. JECOB

    JECOB New Member

    Joined:
    Jun 7, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hi evryone,


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

    slymart New Member

    Joined:
    Apr 25, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    Thanks obaid for the good explanation.
     
  7. slymart

    slymart New Member

    Joined:
    Apr 25, 2011
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    What can one do to solve this window 32 message; "dll file missing"?
     
  8. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    The most well written article i have read in this section! Hats off.
     

Share This Page