MFC(Microsoft Foundation Class
Library) Message Handling
Janani A
10 Oct 2011
Agenda
• Win32 application
• MFC
• Macros
• Message mapping
A Windows program
InitApplication()
Windows Program (contd…)
InitInstance()
Run()
Message Handler
MFC - Overview
• An "application framework" for programming in Microsoft
Windows.
• It's easy to extend or override the basic functionality that MFC
provides.
• Object Linking and Embedding (OLE), Data Access Objects
(DAO) and Open Database Connectivity (ODBC) etc
Execution begins here…
int AFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
ASSERT(hPrevInstance == NULL);
int nReturnCode = -1;
CWinApp* pApp = AfxGetApp();
// AFX internal initialization
if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow))
goto InitFailure;
// App global initializations (rare)
ASSERT_VALID(pApp);
if (pApp != NULL && !pApp->InitApplication())
goto InitFailure;
ASSERT_VALID(pApp);
// Perform specific initializations
if (!pApp->InitInstance())
{
if (pApp->m_pMainWnd != NULL)
{
TRACE(traceAppMsg, 0, "Warning: Destroying non-NULL m_pMainWndn");
pApp->m_pMainWnd->DestroyWindow();
}
nReturnCode = pApp->ExitInstance();
goto InitFailure;
}
ASSERT_VALID(pApp);
nReturnCode = pApp->Run();
ASSERT_VALID(pApp);
InitFailure:
AfxWinTerm();
return nReturnCode;
}
The Windows program using MFC
class MyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
class MyWnd : public CFrameWnd
{
public:
MyWnd()
{
Create (NULL, "My Application");
}
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
};
BEGIN_MESSAGE_MAP(MyWnd, CFrameWnd)
ON_WM_LBUTTONDOWN()
END_MESSAGE_MAP()
void MyWnd:: OnLButtonDown(UINT nFlags, CPoint point)
{
MessageBox(TEXT("Left mouse button pressed..."),NULL,MB_OK);
}
BOOL MyApp:: InitInstance()
{
m_pMainWnd = new MyWnd();
m_pMainWnd -> ShowWindow(m_nCmdShow);
m_pMainWnd -> UpdateWindow();
return TRUE;
}
MyApp intApp;
Macros
//DECLARE_MESSAGE_MAP()
#define DECLARE_MESSAGE_MAP() 
private :
static const AFX_MSGMAP_ENTRY _messageEntries[];
protected:
static const AFX_MSGMAP messageMap;
virtual const AFX_MSGMAP* GetMessageMap() const;
//BEGIN_MESSAGE_MAP()
#define BEGIN_MESSAGE_MAP(theClass, baseClass) 
const AFX_MSGMAP* theClass::GetMessageMap() const 
{ return &theClass::messageMap; } 
AFX_DATADEF const AFX_MSGMAP theClass::messageMap = 
{ &baseClass::messageMap, &theClass::_messageEntries[0] }; 
const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = 
{ 
#define END_MESSAGE_MAP() 
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } 
};
#define ON_WM_LBUTTONDOWN() 
{ WM_LBUTTONDOWN, 0, 0, 0, AfxSig_vwp, 
(AFX_PMSG)(AFX_PMSGW)(void (AFX_MSG_CALL CWnd::*)(UINT, CPoint))&OnLButtonDown },
After Pre-processing
class MyApp : public CWinApp
{
public:
virtual BOOL InitInstance();
};
class MyWnd : public CFrameWnd
{
public:
MyWnd()
{
Create (NULL, "My Application");
}
afx_msg void snFlags, CPoint point);
private :
static const AFX_MSGMAP_ENTRY
_messageEntries[];
protected:
static const AFX_MSGMAP messageMap;
virtual const AFX_MSGMAP* GetMessageMap()
const;
};
const AFX_MSGMAP* MyWnd::GetMessageMap() const
{ return & MyWnd::messageMap; }
AFX_DATADEF const AFX_MSGMAP MyWnd::messageMap =
{ &CFrameWnd::messageMap, & MyWnd::_messageEntries[0] };
const AFX_MSGMAP_ENTRY MyWnd::_messageEntries[] =
{
{ WM_LBUTTONDOWN, 0, 0, 0, AfxSig_vwp,
(AFX_PMSG)(AFX_PMSGW)(void (AFX_MSG_CALL
CWnd::*)(UINT, CPoint))&OnLButtonDown },
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 }
};
void MyWnd:: OnLButtonDown(UINT nFlags, CPoint point)
{
MessageBox(TEXT("Left mouse button pressed..."),
NULL,MB_OK);
}
BOOL MyApp:: InitInstance()
{
m_pMainWnd = new MyWnd();
m_pMainWnd -> ShowWindow(m_nCmdShow);
m_pMainWnd -> UpdateWindow();
return TRUE;
}
MyApp intApp;
Message map structures
Message Handling
• The CCmdTarget class: Base class for CFrameWnd,
CView, CWinApp, CDocument, CWnd with functions/
handlers for polymorphism. Override base functions
for custom behavior.
• Message Maps: Map the message codes to handlers
(CCmdTarget). Declare with
DECLARE_MESSAGE_MAP
References
• http://msdn.microsoft.com/en-us/library/d06h2x6e(v=vs.80).aspx
• http://www.functionx.com/visualc/
• MFC Internals- Inside The Microsoft Foundation Class, George Shepherd
and Scot Wingo
• http://home.arcor.de/it-
solution/cpp/books/Professional%20MFC/CH03_7.HTM
• afxmsg_.h
THANK YOU

MFC Message Handling

  • 1.
    MFC(Microsoft Foundation Class Library)Message Handling Janani A 10 Oct 2011
  • 2.
    Agenda • Win32 application •MFC • Macros • Message mapping
  • 3.
  • 4.
  • 5.
    MFC - Overview •An "application framework" for programming in Microsoft Windows. • It's easy to extend or override the basic functionality that MFC provides. • Object Linking and Embedding (OLE), Data Access Objects (DAO) and Open Database Connectivity (ODBC) etc
  • 6.
    Execution begins here… intAFXAPI AfxWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { ASSERT(hPrevInstance == NULL); int nReturnCode = -1; CWinApp* pApp = AfxGetApp(); // AFX internal initialization if (!AfxWinInit(hInstance, hPrevInstance, lpCmdLine, nCmdShow)) goto InitFailure; // App global initializations (rare) ASSERT_VALID(pApp); if (pApp != NULL && !pApp->InitApplication()) goto InitFailure; ASSERT_VALID(pApp); // Perform specific initializations if (!pApp->InitInstance()) { if (pApp->m_pMainWnd != NULL) { TRACE(traceAppMsg, 0, "Warning: Destroying non-NULL m_pMainWndn"); pApp->m_pMainWnd->DestroyWindow(); } nReturnCode = pApp->ExitInstance(); goto InitFailure; } ASSERT_VALID(pApp); nReturnCode = pApp->Run(); ASSERT_VALID(pApp); InitFailure: AfxWinTerm(); return nReturnCode; }
  • 7.
    The Windows programusing MFC class MyApp : public CWinApp { public: virtual BOOL InitInstance(); }; class MyWnd : public CFrameWnd { public: MyWnd() { Create (NULL, "My Application"); } afx_msg void OnLButtonDown(UINT nFlags, CPoint point); DECLARE_MESSAGE_MAP() }; BEGIN_MESSAGE_MAP(MyWnd, CFrameWnd) ON_WM_LBUTTONDOWN() END_MESSAGE_MAP() void MyWnd:: OnLButtonDown(UINT nFlags, CPoint point) { MessageBox(TEXT("Left mouse button pressed..."),NULL,MB_OK); } BOOL MyApp:: InitInstance() { m_pMainWnd = new MyWnd(); m_pMainWnd -> ShowWindow(m_nCmdShow); m_pMainWnd -> UpdateWindow(); return TRUE; } MyApp intApp;
  • 8.
    Macros //DECLARE_MESSAGE_MAP() #define DECLARE_MESSAGE_MAP() private: static const AFX_MSGMAP_ENTRY _messageEntries[]; protected: static const AFX_MSGMAP messageMap; virtual const AFX_MSGMAP* GetMessageMap() const; //BEGIN_MESSAGE_MAP() #define BEGIN_MESSAGE_MAP(theClass, baseClass) const AFX_MSGMAP* theClass::GetMessageMap() const { return &theClass::messageMap; } AFX_DATADEF const AFX_MSGMAP theClass::messageMap = { &baseClass::messageMap, &theClass::_messageEntries[0] }; const AFX_MSGMAP_ENTRY theClass::_messageEntries[] = { #define END_MESSAGE_MAP() {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } }; #define ON_WM_LBUTTONDOWN() { WM_LBUTTONDOWN, 0, 0, 0, AfxSig_vwp, (AFX_PMSG)(AFX_PMSGW)(void (AFX_MSG_CALL CWnd::*)(UINT, CPoint))&OnLButtonDown },
  • 9.
    After Pre-processing class MyApp: public CWinApp { public: virtual BOOL InitInstance(); }; class MyWnd : public CFrameWnd { public: MyWnd() { Create (NULL, "My Application"); } afx_msg void snFlags, CPoint point); private : static const AFX_MSGMAP_ENTRY _messageEntries[]; protected: static const AFX_MSGMAP messageMap; virtual const AFX_MSGMAP* GetMessageMap() const; }; const AFX_MSGMAP* MyWnd::GetMessageMap() const { return & MyWnd::messageMap; } AFX_DATADEF const AFX_MSGMAP MyWnd::messageMap = { &CFrameWnd::messageMap, & MyWnd::_messageEntries[0] }; const AFX_MSGMAP_ENTRY MyWnd::_messageEntries[] = { { WM_LBUTTONDOWN, 0, 0, 0, AfxSig_vwp, (AFX_PMSG)(AFX_PMSGW)(void (AFX_MSG_CALL CWnd::*)(UINT, CPoint))&OnLButtonDown }, {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } }; void MyWnd:: OnLButtonDown(UINT nFlags, CPoint point) { MessageBox(TEXT("Left mouse button pressed..."), NULL,MB_OK); } BOOL MyApp:: InitInstance() { m_pMainWnd = new MyWnd(); m_pMainWnd -> ShowWindow(m_nCmdShow); m_pMainWnd -> UpdateWindow(); return TRUE; } MyApp intApp;
  • 10.
  • 11.
    Message Handling • TheCCmdTarget class: Base class for CFrameWnd, CView, CWinApp, CDocument, CWnd with functions/ handlers for polymorphism. Override base functions for custom behavior. • Message Maps: Map the message codes to handlers (CCmdTarget). Declare with DECLARE_MESSAGE_MAP
  • 12.
    References • http://msdn.microsoft.com/en-us/library/d06h2x6e(v=vs.80).aspx • http://www.functionx.com/visualc/ •MFC Internals- Inside The Microsoft Foundation Class, George Shepherd and Scot Wingo • http://home.arcor.de/it- solution/cpp/books/Professional%20MFC/CH03_7.HTM • afxmsg_.h
  • 13.