如何将CCmdTarget派生类嵌入到MFC消息链中?

发布于 2024-10-27 12:17:30 字数 919 浏览 5 评论 0原文

我想从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暗恋未遂 2024-11-03 12:17:30

您不必向视图类的消息映射添加任何内容。相反,您应该重写 OnCmdMsg 函数将命令和更新消息路由到您的 CSettings 类,如下所示:

class CMyView : public CView {
    // ...
    virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra,
        AFX_CMDHANDLERINFO* pHandlerInfo);
};

BOOL CMyView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
    AFX_CMDHANDLERINFO* pHandlerInfo)
{
    if (m_sett.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
        return TRUE;

    return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);    
}

查看 此页面

另外,如果您有 80 多个设置,您可能需要考虑使用 ON_COMAND_RANGEON_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 your CSettings class, like this:

class CMyView : public CView {
    // ...
    virtual BOOL OnCmdMsg(UINT nID, int nCode, void* pExtra,
        AFX_CMDHANDLERINFO* pHandlerInfo);
};

BOOL CMyView::OnCmdMsg(UINT nID, int nCode, void* pExtra,
    AFX_CMDHANDLERINFO* pHandlerInfo)
{
    if (m_sett.OnCmdMsg(nID, nCode, pExtra, pHandlerInfo))
        return TRUE;

    return CView::OnCmdMsg(nID, nCode, pExtra, pHandlerInfo);    
}

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 and ON_UPDATE_COMMAND_UI_RANGE to avoid having to write handler functions for each individual setting.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文