Extended Command Processing

消息映射宏除了ON_COMMAND还有其他的有扩展作用的宏,例如:
ON_COMMAND_EX、
ON_COMMAND_RANGE、
ON_COMMAND_EX_RANGE、
ON_UPDATE_COMMAND_UI_RANGE
当希望用一个函数处理几个相关的命令消息时,它们就很有用。
不能用ClassWizard添加上述几个宏,需要手动添加,并且要添加在AFX_MESSAGE_MAP的括号外面
1、ON_COMMAND_EX、ON_COMMAND_EX_RANGE的用法举例
Assume that IDM_ZOOM_1 and IDM_ZOOM_2 are related command IDs defined in resource.h. Here's the class code you'll need to process both messages with one function, OnZoom:
BEGIN_MESSAGE_MAP(CMyView, CView)
    ON_COMMAND_EX(IDM_ZOOM_1, OnZoom)
    ON_COMMAND_EX(IDM_ZOOM_2, OnZoom)
END_MESSAGE_MAP()
BOOL CMyView::OnZoom(UINT nID)
{
    if (nID == IDM_ZOOM_1) {
        // code specific to first zoom command
    }
    else {
        // code specific to second zoom command
    }
    // code common to both commands
    return TRUE; // Command goes no further
}
Here's the function prototype:
afx_msg BOOL OnZoom(UINT nID);
If the values of IDM_ZOOM_1 and IDM_ZOOM_2 were consecutive,
you could rewrite the CMyView message map as follows: 
BEGIN_MESSAGE_MAP(CMyView, CView)
    ON_COMMAND_EX_RANGE(IDM_ZOOM_1, IDM_ZOOM_2, OnZoom)
END_MESSAGE_MAP()
Now OnZoom is called for both menu choices, and the handler can determine the choice from the integer parameter.
2、ON_COMMAND_RANGE的用法说明
ID连续的几个命令需要同一个函数处理时,ON_COMMAND_RANGE就用到了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章