MFC Tutorial Part 2 – Handling Message Maps

This article is the next step after MFC Tutorial Part 1 which dealt with a simple task of creating a window. This part tries to cover the details about Message Map.

Message Maps are the way by which MFC handles the Application messages. Any class which is derived from CCmdTarget is a candidate for handling messages. Previously in win32 SDK, a programmer is supposed to handle all messages posted to the message loop. The application he wrote should have a function to handle the messages and do the actions according to the
commands passed to it. To the relief of all MFC has relieved the programmers from all these pains and allows programmers just to restrict themselves with the functionality implementations.

Let’s look at some sample code to understand the basics of this message maps.

Step1:
Create a new project of type Win32 application. In the second screen of the wizard, select the first option. Do not allow the wizard to add any files.

Step 2:
After the project is created, click on Menu–>Project –> Add to Project –>New and select a .cpp file and give a name to it.

Step 3:
Copy and paste the code below.

	
//MFC2.CPP - MFC Tutorial Part 2 from
CoderSource.net

#include <afxwin.h>

class MFC_Tutorial_Window :public CFrameWnd
{
public:
    MFC_Tutorial_Window()
    {
        Create(NULL,"MFC Tutorial Part 2 CoderSource Window");
    }
    void OnLButtonDown(UINT nFlags, CPoint point);
    DECLARE_MESSAGE_MAP()
};

BEGIN_MESSAGE_MAP( MFC_Tutorial_Window, CFrameWnd)
      ON_WM_LBUTTONDOWN() //Macro to map the left button click to the handler
END_MESSAGE_MAP()

void MFC_Tutorial_Window::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
   CFrameWnd::OnLButtonDown(nFlags, point);
   MessageBox("Left Button clicked");
}

class MyApp :public CWinApp
{
     MFC_Tutorial_Window *wnd;?
public:
     BOOL InitInstance()
     {
         wnd = new MFC_Tutorial_Window();
         m_pMainWnd = wnd;
         m_pMainWnd->ShowWindow(1);
         return 1;
     }
};

MyApp theApp;

//End of program

The changes/additions to the previous code are highlighted in bold colors. There are only 4 additional macros which are used here.

DECLARE_MESSAGE_MAP:
This tells the application that the class in which this is called is going to have a message map and handle messages. A class can have only one message map. Also a class will be eligible to execute a message map if it is derived from CCmdTarget or a class which is derived from CCmdTarget.

BEGIN_MESSAGE_MAP & END_MESSAGE_MAP:
This macro takes two parameters. The class name which implements the message map and the base class for it. It then is succeeded by the macros which represent messages viz., ON_WM_LBUTTONDOWN, ON_WM_SIZE etc., It is then closed by END_MESSAGE_MAP(3rd Macro).

ON_WM_LBUTTONDOWN:
This is the macro which declares that the MFC_Tutorial_Window is going to handle Left button clicks and the function which will handle this is OnLButtonDown(UINT nFlags, CPoint point). When there is any click related to this class, the mentioned function will be called automatically with the specific parameters. This is how all the messages are handled.

Compile and execute this program. Do not forget to include MFC Library Project –> Settings –> General tab –> Microsoft Foundation classes combo and select “Use MFC in shared dll”.

After running this MFC_Tutorial_Window, if you click using the left mouse button, a message box will be displayed.