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

In SDI how many view's are attached to document object and in MDI how many view's are attached to Document object?

2 Answers   Wipro,


If there is more than 100 control in a window how we can change the Taborder of a controls

4 Answers   Satyam,


what do you mean by psychic window?

2 Answers   Patni,


Whats is DDX & DDV in MFC?

1 Answers   HCL,


Have you ever used win32 APIs ?

4 Answers   Microsoft,






how can u change button shape at run time

3 Answers   Samsung,


What is mfc class hierarchy?

0 Answers  


How can update edit control data of an executing application from other application?

3 Answers   HP,


How you find memory leaks?

4 Answers   ABB,


What is Thread ?(VC++)What is the difference between Cmutex and Csemaphone?

5 Answers   Atos Origin,


What is the difference between the SDI and MDI

3 Answers   Invensys,


Tell me the work of HTREDUCE and HTZOOM

1 Answers   E Logic,


Categories