從MFC訊息對映巨集分析MFC訊息對映的實現

2021-05-24 04:34:45 字數 1969 閱讀 1094

在mfc中,我們可以找到如下三個巨集

declare_massage_map()

begine_massage_map(class, bassclass)

end_massage_map()

下面來分析這三個巨集

1  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; 

可以看出declare_message_map() 巨集中定義了兩個靜態成員函式和乙個過載的虛函式

afx_msgmap_entry 根據題意,可以看出這是乙個訊息入口(訊息和訊息函式之間的對映)

struct

afx_msgmap_entry;

再看看afx_message結構

struct

afx_msgmap

;

可見結構體afx_msgmap中定義了兩個指標,pbasemap指向另乙個afx_msgmap,lpentries指向乙個訊息入口表。可以推想,在響應訊息時,一定是在lpentries指向的訊息入口表中尋找響應函式,也可能會在pbasemap指向的結構體中做同樣的響應函式尋找操作(有點不理解)。

過載的虛函式getmessagemap,可以猜測只是用來返回成員messagemap的位址而已

2 begin_messgae_map(class, baseclass)

作用:定義declare_message_map巨集宣告的靜態變數。

#define

begin_message_map(theclass, baseclass) 

const

afx_msgmap

*theclass::getmessagemap() 

const

afx_comdat 

const

afx_msgmap theclass::messagemap 

=; afx_comdat 

const

afx_msgmap_entry theclass::_messageentries 

=然後初始化了當前類的成員變數

messagemap。messagemap的pbasemap指標指向其父類的messagemap成員,lpentries指標指向當前類的_messageentries陣列的首位址。

afx_comdat const afx_msgmap theclass::messagemap = 

;

最後,定義了

_messageentries陣列初始化**的開始部分。

afx_comdat const afx_msgmap_entry theclass::_messageentries = 

}; 在 declare_message_map和end_message_map之間還有一些巨集,如on_command、on_wm_create等,這些巨集最終都會被生成一條afx_msgmap_entry結構體資料,並成為_messageentries訊息對映表資料的乙個元素。我們以常見的on_command巨集為例。on_command巨集的源**為:

#define

on_command(id, memberfxn) 

,

通過以上分析,我們可以得到乙個鍊錶式的資料結構,子類的

messagemap成員為鍊錶的頭節點。鍊錶的每個節點都包含乙個訊息入口表。mfc的訊息系統的標準備訊息處理函式ccmdtarget::oncmdmsg正是通過這樣乙個鍊錶查詢到訊息的響應函式,並呼叫該函式來響應訊息。

MFC訊息對映巨集說明

1 declare message map 在標頭檔案中宣告原始檔中所含有的訊息對映 2,begin message map 標記原始檔訊息對映的開始 3,end message ma 標記原始檔訊息對映的結束 4,on command 將特定命令的處理委派給類的乙個成員函式 5,on contro...

常用MFC訊息對映巨集說明

1 declare message map 在標頭檔案中宣告原始檔中所含有的訊息對映 2,begin message map 標記原始檔訊息對映的開始 3,end message ma 標記原始檔訊息對映的結束 4,on command 將特定命令的處理委派給類的乙個成員函式 5,on contro...

MFC訊息對映

run這個函式來建立和處理訊息迴圈 bool afxapi afxinternalpumpmessage return true 顯而易見,mfc中處理訊息也是利用了win32下的訊息處理 那麼還是這樣的結構 typedef struct tagmsg msg 有了這個概念之後我們知道,mfc通過訊...