突然間想起這個是因爲最近看到了AFX_MSGMAP_ENTRY這個結構,結果愣了半天,於是乎乾脆又將這一塊的相關知識看了一遍,哎,人上了歲數記憶就不行,需要寫點東西幫忙記錄一下自己的學習歷程,不提這個,跑題了:
提起MFC消息,到底程序在哪裏處理消息的,因爲MFC對windows程序的很多地方進行了封裝,導致入口地方不是那麼容易找到,不過其實無非就是封裝而已——CWinApp(應用程序),在MFC類窗口CxxxApp中查看基類就能找到,調試一下就會發現MFC程序執行的大致流程是:
BOOL AFXAPI AfxInternalPumpMessage()
{
MSG msg;
::GetMessage(&msg, NULL, NULL, NULL);
if (!AfxPreTranslateMessage(&msg))
{
::TranslateMessage(&msg);
::DispatchMessage(&msg);
}
return TRUE;
}</span></span>
typedef struct tagMSG {
HWND hwnd;
UINT message; //消息的類型(即事件的類型)
WPARAM wParam; //附加或稱數據參數1
LPARAM lParam;
DWORD time;
POINT pt;
} MSG; </span>
BEGIN_MESSAGE_MAP(CexampleDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()
#define BEGIN_MESSAGE_MAP(theClass, baseClass) \
PTM_WARNING_DISABLE \
const AFX_MSGMAP* theClass::GetMessageMap() const \
{ return GetThisMessageMap(); } \
const AFX_MSGMAP* PASCAL theClass::GetThisMessageMap() \
{ \
typedef theClass ThisClass; \
typedef baseClass TheBaseClass; \
static const AFX_MSGMAP_ENTRY _messageEntries[] = \
{
#define END_MESSAGE_MAP() \
{0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } \
}; \
static const AFX_MSGMAP messageMap = \
{ &TheBaseClass::GetThisMessageMap, &_messageEntries[0] }; \
return &messageMap; \
} \
PTM_WARNING_RESTORE
struct AFX_MSGMAP_ENTRY
{
UINT nMessage; // windows message
UINT nCode; // control code or WM_NOTIFY code
UINT nID; // control ID (or 0 for windows messages)
UINT nLastID; // used for entries specifying a range of control id's
UINT_PTR nSig; // signature type (action) or pointer to message #
AFX_PMSG pfn; // routine to call (or special value)
};
struct AFX_MSGMAP
{
const AFX_MSGMAP* (PASCAL* pfnGetBaseMap)();
const AFX_MSGMAP_ENTRY* lpEntries;
};
BEGIN_MESSAGE_MAP(CexampleDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
END_MESSAGE_MAP()