如何将CCmdTarget派生类嵌入到MFC消息链中?
我想从 View 类中提取一些特定于设置的代码并将其放入 CSettings 类中。
当设置数量超过 80 时,我不想让我的 CView 类变得臃肿。
菜单(和功能区)中有关设置的所有消息都应在 CSettings 类中处理。
我唯一不明白的是如何将消息映射条目嵌入到 CMyView 消息映射中。
//The main purpose of class CSettings is to remove all the logic of settings from View.
class CSettings : public CCmdTarget
{
DECLARE_MESSAGE_MAP()
DECLARE_DYNAMIC(CSettings)
void OnCheckS1() {
m_bVal1 = !m_bVal1;
}
void OnUpdateCheck1(CCmdUI* pCmdUI){
pCmdUI->SetRadio(m_bVal1);
}
bool m_bVal1;
<other 80 settings>
}
BEGIN_MESSAGE_MAP(CSettings, CCmdTarget)
ON_COMMAND(ID_CHECK_S1, &CSettings::OnCheckS1)
ON_UPDATE_COMMAND_UI(ID_CHECK_S1, &CSettings::OnUpdateCheck1)
END_MESSAGE_MAP()
class CMyView : public CView
{
...
CSettings m_sett;
}
BEGIN_MESSAGE_MAP(CMyViewView, CView)
--->>> ??? <<<----
END_MESSAGE_MAP()
I want to extract some settings-specific code from View class and put it in to CSettings class.
I don’t want to bloat my CView class when amount of settings will be more than 80.
All the messages from Menu (and ribbon) about settings should be processed in CSettings class.
The only thing I can't understand is how to embed message map entries in to CMyView message map.
//The main purpose of class CSettings is to remove all the logic of settings from View.
class CSettings : public CCmdTarget
{
DECLARE_MESSAGE_MAP()
DECLARE_DYNAMIC(CSettings)
void OnCheckS1() {
m_bVal1 = !m_bVal1;
}
void OnUpdateCheck1(CCmdUI* pCmdUI){
pCmdUI->SetRadio(m_bVal1);
}
bool m_bVal1;
<other 80 settings>
}
BEGIN_MESSAGE_MAP(CSettings, CCmdTarget)
ON_COMMAND(ID_CHECK_S1, &CSettings::OnCheckS1)
ON_UPDATE_COMMAND_UI(ID_CHECK_S1, &CSettings::OnUpdateCheck1)
END_MESSAGE_MAP()
class CMyView : public CView
{
...
CSettings m_sett;
}
BEGIN_MESSAGE_MAP(CMyViewView, CView)
--->>> ??? <<<----
END_MESSAGE_MAP()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必向视图类的消息映射添加任何内容。相反,您应该重写
OnCmdMsg
函数将命令和更新消息路由到您的CSettings
类,如下所示:查看 此页面。
另外,如果您有 80 多个设置,您可能需要考虑使用
ON_COMAND_RANGE
和ON_UPDATE_COMMAND_UI_RANGE
以避免为每个单独的设置编写处理函数。You don't have to add anything to your view class's message map. Instead you should override the
OnCmdMsg
function to route the command and update messages to yourCSettings
class, like this:Have a look at this page in MSDN for more information.
Also, if you've got 80+ settings, you might want to consider using
ON_COMAND_RANGE
andON_UPDATE_COMMAND_UI_RANGE
to avoid having to write handler functions for each individual setting.