What is the use of message map ?
Answers were Sorted based on User's Feedback
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 |
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 |
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 |
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 |
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 |
how u can move CDC pointer to one line in cview?
What is the difference between OnInitialUpdate and OnUpdate?
What MFC base classes provide support for ActiveX controls?
What is the initial function to be called in MFC and what it will do
Hi All, I have created one MFC Dialog Based application.now if i am running the application its working fine,instead of closing he application i minimized the application,if i run the application again,i am getting the Dialog. I want to prevent the calling of application multiple times. please give me the code and let me know in which method i need to make changes. Praveer
What is userdefine Messages in MFC?
visual Pogramming c++ coding for create a paint application.. (Please someone help me)
What is the use of CObject::Dump function ?
What is mfc class hierarchy?
What is the difference between Struts and JSF? Pls list some most suitable differences.
how do u identify RTTI in vc++
Tell me the different controls in MFC ?