計算機(jī)二級C宏智者的利刃,愚者的惡夢-中-

字號:

(續(xù)上)例三、用c宏,自動生成代碼這方面的例子也是多得很,不過有鑒于很多朋友不用很多編譯器,不做嵌入式的開發(fā),我就舉個win平臺的例子吧。我們知道m(xù)fc實(shí)現(xiàn)了windows的消息映射,比如: on_command(idm_about, onabout)
    on_command(idm_filenew, onfilenew)
    它是如何實(shí)現(xiàn)的idm_about和onabout的關(guān)聯(lián)的呢?這要用到幾個宏。
    #define declare_message_map() /
    private: /
    static const afx_msgmap_entry _messageentries[]; /
    protected: /
    static afx_data const afx_msgmap messagemap; /
    virtual const afx_msgmap* getmessagemap() const; /
    #define begin_message_map(theclass, baseclass) /
    const afx_msgmap* theclass::getmessagemap() const /
    { return &theclass::messagemap; } /
    afx_comdat afx_datadef const afx_msgmap theclass::messagemap = /
    { &baseclass::messagemap, &theclass::_messageentries[0] }; /
    afx_comdat const afx_msgmap_entry theclass::_messageentries[] = /
    { /
    #define on_command(id, memberfxn) /
    { wm_command, 0, (word)id, (word)id, afxsig_vv, (afx_pmsg)memberfxn },
    #define end_message_map() /
    {0, 0, 0, 0, afxsig_end, (afx_pmsg)0 } /
    }; /
    #define declare_message_map() /
    private: /
    static const afx_msgmap_entry _messageentries[]; /
    protected: /
    static afx_data const afx_msgmap messagemap; /
    virtual const afx_msgmap* getmessagemap() const; /
    #define begin_message_map(theclass, baseclass) /
    const afx_msgmap* theclass::getmessagemap() const /
    { return &theclass::messagemap; } /
    afx_comdat afx_datadef const afx_msgmap theclass::messagemap = /
    { &baseclass::messagemap, &theclass::_messageentries[0] }; /
    afx_comdat const afx_msgmap_entry theclass::_messageentries[] = /
    { /
    #define on_command(id, memberfxn) /
    { wm_command, 0, (word)id, (word)id, afxsig_vv, (afx_pmsg)memberfxn },
    #define end_message_map() /
    {0, 0, 0, 0, afxsig_end, (afx_pmsg)0 } /
    }; /
    嘿嘿,就這么幾個宏,就構(gòu)造出一個消息數(shù)組來.