What is the use of message map ?

Answers were Sorted based on User's Feedback



What is the use of message map ? ..

Answer / arvind

MessageMap is a logical table that maps the windows messages
to the member functions of the class.

MFC handles Windows message in a different way. Because MFC
applications are built upon classes, it is more convenient
to handle messages within class member functions instead of
one big callbackfunction.
In MFC, this is achieved through message mapping: we can
implement the functions that will be
used to execute commands, and use macros defined in MFC to
direct the messages into these member
functions.

Is This Answer Correct ?    35 Yes 8 No

What is the use of message map ? ..

Answer / 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

What is the use of message map ? ..

Answer / fahim

Message map is a macro used to handle messgaes by calling
appropriate functions.
i.e
BEGIN_MESSAGE_MAP(CMywnd,CFrameWnd)
ON_WM_PAINT()
END_MESSAGE_MAP

Here OnPaint method will be called whn WM_PAINT message
comes.

Is This Answer Correct ?    21 Yes 16 No

What is the use of message map ? ..

Answer / noor

message map table is used to register the messages.those
messages that displayed in message map table it operate by
the application and those message that did not enter in the
message map table it control CFrame.
BEGIN_Message_MAP(myframe.CFrameWnd)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()

Is This Answer Correct ?    4 Yes 5 No

What is the use of message map ? ..

Answer / raju

Message map is to avoid following drawbacks:
1)Most windows only process a small number of messages,yet
each window require to gaint virtual function table with
the the entries of each message.
2)virtual functions don't handle user defined messages and
other custom cases.

Is This Answer Correct ?    6 Yes 9 No

Post New Answer

More MFC Interview Questions

How to convert a CString variable to char* or LPTSTR?

5 Answers   Dover,


19)how to set Back Ground Picture to a Dialog Box in MFC ?

1 Answers  


What are the types of button controls?

10 Answers  


What is the use of UpdateData funciton ?

7 Answers  


1)How to change the size of a button at run time ?

1 Answers  






What function is used to disable a control at runtime?

7 Answers  


Hi can anyone explain about the synchronization objects types and where we are using in the code.

1 Answers  


What is socket?

3 Answers  


What function is used to retrieve the currently selected index in a list box?

2 Answers  


I want recent paper pattern for HP company?

0 Answers   HP,


if i modified data in 1 view how does the other view knows

2 Answers   Siemens,


if both base and derived class have the constructors if i create an object for derive class which class constructor is executed first

10 Answers   Siemens, Symphony,


Categories