本專欄上兩篇文章分別介紹了【MFC】05.MFC第一大機(jī)制:程序啟動(dòng)機(jī)制和【MFC】06.MFC第二大機(jī)制:窗口創(chuàng)建機(jī)制,這篇文章來(lái)為大家介紹MFC的第三大機(jī)制:消息映射
-
typfd要實(shí)現(xiàn)消息映射,必須滿足的三個(gè)條件:
類必須繼承于CmdTargert
類必須聲明重定義 DECLARE_MESSAGE_MAP
類外必須實(shí)現(xiàn)DEGIN_MESSINGE_MAP()
END_MESSAGE_MAP()
自己的窗口類{ LERESULT onCreate(WPARAM wParam,LPARAM lParam){ AfxMessageBox("WM_CREATE"); } //定義宏: DECLARE_MESSAGE_MAP() } //類外實(shí)現(xiàn)消息映射: BEGIN_MESSAGE_MAP(cMyFrameWnd,CFrameWnd) ON_MESSAGE(WM_CREATE,onCreate); END_MESSAGE_MAP()
-
在Win32程序中封裝消息:
我們定義一張映射表,當(dāng)進(jìn)入WndProc的時(shí)候,通過(guò)查找這張表的映射關(guān)系,來(lái)執(zhí)行對(duì)應(yīng)的函數(shù):
typedef struct MESSAGE_ENTRY{ int message; int (*pFun)(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam); } struct MESSAGE_ENTRY MessageMap[]{ 映射容器: WM_PAINT,函數(shù)地址 }
MFC的消息映射機(jī)制:
- 宏展開(kāi):
DECLARE_MESSAGE_MAP(){ //靜態(tài)函數(shù) static const AFX_MSGMAP* PASCAL GetThisMessageMap(); //虛函數(shù) virtual const AFX_MSGMAP* GetMessageMap() const; } 實(shí)現(xiàn)宏展開(kāi): DECLARE_MESSAGE_MAP(){ //靜態(tài)函數(shù) static const AFX_MSGMAP* PASCAL GetThisMessageMap(); //虛函數(shù) virtual const AFX_MSGMAP* GetMessageMap() const; } BEGIN_MESSAGE_MAP(CMFCApplication1App, CWinApp){ //這里是實(shí)現(xiàn)虛函數(shù), 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[] = { {0, 0, 0, 0, AfxSig_end, (AFX_PMSG)0 } }; static const AFX_MSGMAP messageMap = { &TheBaseClass::GetThisMessageMap,//我們的弗雷德靜態(tài)函數(shù)地址 &_messageEntries[0] };//本類消息結(jié)構(gòu)體的數(shù)組首地址 return &messageMap; } }
我們來(lái)看看struct AFXmMSGMAP_ENTRY結(jié)構(gòu)體:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-634587.html
struct AFX_MSGMAP_ENTRY
{
UINT nMessage; //消息ID
UINT nCode; //win32通知碼
UINT nID; //命令I(lǐng)D WM_COMMAND 菜單 按鈕 快捷鍵 加速鍵 1000
UINT nLastID; //最后ID 1004
UINT_PTR nSig; //處理消息的類型
AFX_PMSG pfn; //我們的處理消息的函數(shù)地址
};
消息回調(diào):文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-634587.html
LRESULT CALLBACK AfxWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam)
{
CWnd* pWnd = CWnd::FromHandlePermanent(hWnd);
AfxCallWndProc(pWnd, hWnd, nMsg, wParam, lParam);
{
pWnd->WindowProc(nMsg, wParam, lParam)
{
OnWndMsg(message, wParam, lParam, &lResult)
{
//函數(shù)簽名 里面有函數(shù)指針
union MessageMapFunctions mmf;
//返回鏈表頭節(jié)點(diǎn)
const AFX_MSGMAP* pMessageMap; pMessageMap = this->GetMessageMap();
const AFX_MSGMAP_ENTRY* lpEntry;
for (/* pMessageMap already init'ed */;
pMessageMap->pfnGetBaseMap != NULL; //判斷節(jié)點(diǎn)等不等于空
pMessageMap = (*pMessageMap->pfnGetBaseMap)()//找下一個(gè)節(jié)點(diǎn)
)
{
lpEntry = AfxFindMessageEntry(pMessageMap->lpEntries,message, 0, 0)) != NULL)
//消息函數(shù)的地址
mmf.pfn = lpEntry->pfn;
switch (lpEntry->nSig)
{
lResult = (this->*mmf.pfn_l_w_l)(wParam, lParam);
}
}
}
}
}
}
到了這里,關(guān)于【MFC】07.MFC第三大機(jī)制:消息映射-筆記的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!