Results 1 to 2 of 2

Thread: Message Handling in MFC Apps

  1. #1

    Thread Starter
    Member
    Join Date
    Jul 2001
    Posts
    46

    Message Handling in MFC Apps

    I have read four tutorials about message handling in codeproject.com . I`m searching for more. Would you please guide me if there are any websites that gives some info and tutorials about message handling in MFC apps?

    Thanks in advance

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Message handling in MFC is always the same.

    Every class that derives from CCmdTarget can receive command messages (CWnd, CDocument, CWinThread and some others).
    Every class that derives from CWnd can receive window messages.

    Message handling uses several macros. The most important are DECLARE_MESSAGE_MAP, BEGIN_MESAGE_MAP and END_MESSAGE_MAP.

    DMM is used in the class declaration, it has no parameters.

    BMM marks the start of the message map and is used in the implementation of the class. It has two parameters: the name of the class and the name of the base class.

    After BMM follow the message map entries. Use the ON_... macros.

    To end the message map, use the EMM macro (no parameters)

    For every common windows message there is a related ON_WM_* macro. For a windows message named WM_FOO there is the macro ON_WM_FOO. These macros don't take parameters but require you to have a handler function with a specific name, return type and parameter list.

    There are some macros for common notification messages like button notifications (BN_*), e.g. ON_BN_CLICKED. These macros want the ID of the control and the name of a void () handler function.

    There is also the generic ON_CONTROL macro that is used for getting messages from other controls. It works only for notifications sent via the WM_COMMAND message. Pass the notification code, the control ID and a void () handler as parameters.

    To handle messages from a menu item or accelerator, use the ON_COMMAND macro. Pass the ID and a void () handler.

    There is also the ON_COMMAND_RANGE macro that allows you to use one handler for a range of commands and the undocumented ON_COMMAND_EX macro.
    As an addition there are the ON_UPDATE_COMMAND_UI and ON_UPDATE_COMMAND_UI_RANGE macros that allow you to enable/disable menu items.

    To handle notifications from advanced controls (like the common controls) use the ON_NOTIFY, ON_NOTIFY_RANGE, ON_NOTIFY_EX and ON_NOTIFY_EX_RANGE macros. ON_NOTIFY wants the notification code, the control ID and a void (NMHDR *, LRESULT *) handler.

    Finally there are some special macros to handle user-defined messages:
    ON_MESSAGE, ON_REGISTERED_MESSAGE, ON_THREAD_MESSAGE and ON_REGISTERED_THREAD_MESSAGE.

    The first two want a LRESULT (WPARAM, LPARAM) handler and the other two a void (UINT, LONG)

    For more information see the message maps topic in the reference.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width