[C++ – MFC] Communicating with Windows

All about Windows Applications are handling various events. When you click the mouse or press a keyboard, Windows sends a message to the application and it’s up to you how to respond to this. MFC helps you a lot. It informs you that  what kind of actions a user has been done such as a button is clicked.

Because of the structure of C++, the message handling code uses a lot of macros and this usage usually makes developers confused. Therefore, it is very important to understand the structure of message handlers in MFC.  

 

1. Message Maps

The basic idea of event handling is simple. Windows catches the event, creates a message, and sends it to an application. The application message handler checks which function is mapped to the message and calls the handler function. The main point is how MFC maps between a message and a function.

Mappings between messages and functions are called a “Message Map“.

 

2. Declaring Message Maps

Several macros are used to declare the Message Map.

  • DECLARE_MESSAGE_MAP: used in a header file to specify that a message map is declared
  • BEGIN_MESSAGE_MAP, END_MESSAGE_MAP: used in a source file to declare message handlers
class CMyApp : public CWinAppEx
{
public:
  ...
  DECLARE_MESSAGE_MAP()
};

If a class definition includes the “DECLARE_MESSAGE_MAP“, the class implementation must include “BEGIN_MESSAGE_MAP” and “END_MESSAGE_MAP” macros.

BEGIN_MESSAGE_MAP(CMyApp, CWinAppEx)
  ON_COMMAND(ID_APP_ABOUT, &CMyApp::OnAppAbout)
  ON_COMMAND(ID_FILE_NEW, &CMyApp::OnFileNew)
  ON_COMMAND(ID_FILE_OPEN, &CMyApp::OnFileOpen)
  ON_COMMAND(ID_FILE_PRINT_SETUP, &CMyApp::OnFilePrintSetup)
END_MESSAGE_MAP()

The “BEGIN_MESSAGE_MAP” macro requires 2 arguments: the current class and the base class.

 

3. Message Handler Definitions

The basic syntax to declare the message handler is like this

ON_<Message>(id, handler_function_ref)

 

4. Message Categories

  • Windows Messages: Standard Windows Messages (begins with “WM_” except “WM_COMMAND”)
  • Control Notification Messages: Messages sent from controls (“WM_COMMAND”)
  • Command Messages: Messages from menus and toolbars (also “WM_COMMAND” but MFC defines unique identifiers for menus and toolbars)

 

5. Handling Messages in Your Application

Where ar you going to create handler functions?

  • Rule 1: “Windows Messages” and “Control Notification Messages” should be handled in a class derived from “CWnd“.

Frame Window classes and View classes are inherited from “CWnd“. But Application classes, Document classes, and Document Template classes are not derived from “CWnd“.

  • Rule 2: “Command Messages” are more flexible and can be handled in any kind of classes.

 

6. Message Handling Process

So far, I have explained the basic concepts of MFC message handling mechanisms.

Now, let’s find out how messages are propagated.

– SDI Application –

  1. View
  2. Document
  3. Document Template
  4. Main Frame Window
  5. Application

– MDI Application –

  1. Active View
  2. Document associate with an active View
  3. Document Template for an active document
  4. Frame Window for an active view
  5. Main Frame Window
  6. Application

One thought on “[C++ – MFC] Communicating with Windows

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s