How to Create Message Maps in MFC

A Windows operating system is message driven environment. An event like a mouse click or a printer signal sends a message to the running application which in turn handles the message. The Microsoft Foundation Class (MFC) Library supports a message driven programming model. Its central component is the Message Map. Here, the OnLButtonDown function handles left mouse clicks in the CMyWindow class.

Things You'll Need

  • Microsoft Visual Studio IDE (integrated development environment)
  • Book on Visual C++ such as, Programming Windows With MFC by Jeff Prosise
Show More

Instructions

    • 1

      Launch your Microsoft Visual Studio. For this example Visual C++ 6.0 was used.

    • 2

      Create a Win32 Application project. Click File, New, Projects and then "Win32 Application". Enter any name you want in the "Project name" text box. In the second page of the wizard select "An empty project". Click Finish and click OK. This project has no default files.

    • 3

      Add a source file to the project. Click Project, "Add to Project," New and then select "C++ Source File". Give the file a name in the "File name" text box. Click "OK."

    • 4

      Copy and paste the following code on the source file:

      #include



      class CMyWindow :public CFrameWnd

      {

      public:

      CMyWindow()

      {

      Create(NULL,"MFC Message Map Demo");

      }

      void OnLButtonDown(UINT flags, CPoint point);

      DECLARE_MESSAGE_MAP()

      };



      BEGIN_MESSAGE_MAP( CMyWindow, CFrameWnd)

      ON_WM_LBUTTONDOWN()

      END_MESSAGE_MAP()



      void CMyWindow::OnLButtonDown(UINT flags, CPoint point)

      {

      CFrameWnd::OnLButtonDown(flags, point);

      MessageBox("Left Button clicked");

      }



      class MyApp :public CWinApp

      {

      CMyWindow *pMyWin;

      public:

      BOOL InitInstance()

      {

      pMyWin = new CMyWindow();

      m_pMainWnd = pMyWin;

      m_pMainWnd->ShowWindow(1);

      return 1;

      }

      };



      MyApp theApp;
    • 5

      Understand what the code means:

      DECLARE_MESSAGE_MAP:

      This macro tells the application that the class which contains it, in this case "CMyWindow", has a message map and can handle messages. Only classes derived from CCmdTarget can execute message maps.

      BEGIN_MESSAGE_MAP & END_MESSAGE_MAP:

      These macros can be thought of as the open close braces of a message map. "BEGIN_MESSAGE_MAP" takes two parameters, the name of the class that hosts the message map, in this case "CMyWindow", and its immediate ancestor, "CFrameWnd".

      ON_WM_LBUTTONDOWN:

      This is the specialty macro that specifies that CMyWindow handles left mouse clicks through the function OnLButtonDown. When a left mouse click is associated with CMyWindow the OnLButtonDown class is called automatically.
    • 6

      Click and execute the program. Click "Build" on the menu and "Compile program_name". Wait until it compiles. Click "Build" and "Execute program_name".

Tips & Warnings

  • A class can have only one message map.

Related Searches:

Resources

Comments

You May Also Like

Related Ads

Featured