What is the use of message map ?

Answer Posted / ashwani

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
Let's look at some 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
#include <afxwin.h>

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

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

void MFC_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_Window *wnd;?
public:
BOOL InitInstance()
{
wnd = new MFC_Window();
m_pMainWnd = wnd;
m_pMainWnd->ShowWindow(1);
return 1;
}
};

MyApp theApp;

//End of program

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_Window, if you click using the left
mouse button, a message box will be displayed.

Is This Answer Correct ?    29 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is the difference between Struts and JSF? Pls list some most suitable differences.

2849


how to repaint when working with horizontal scroll bar

1551


plzz tell me what we can do and dont in tally ERP & sap business one?

1884


What is mfc class hierarchy?

625


List out the basic features of MFC.

13711






Do you have an idea about MFC?

920


what message is sent to an application when the user presses the primary button?

1388


I want recent paper pattern for HP company?

1791


How do I create a dialog box in mfc?

615


How to handle dynamic menus in mfc? What happens when client calls cocreateinstance?

700


What does mfc stand for?

965


what is functioning of DIalodDataXchange ..?

1198


what is the meaning of constant FILE EXCEEDS LENGTH LIMIT while loading from ps file to vsam in the jcl?

3348


2.create for 10 batch: Employee_Number Employee_name Employee_Dateofjoining Employee_address Employee_salary 1.select the employee name who deriving more than 10 thousand salary and joined before august 08. use structure and pointers

1811