是否可以将 CMFCToolBar 添加到对话框中?

发布于 2024-07-25 23:51:07 字数 88 浏览 4 评论 0原文

我刚刚尝试了将 CToolbar 添加到新 CMFCToolBar 上的对话框的标准方法。 但这不起作用。 在我深入研究新的实现之前,我想知道它是否真的可行?

I just tryed the standard way for adding CToolbar to a dialog on the new CMFCToolBar. But it doesn't work. Befor I dip into the new implementation, I want to know if it actually possible?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

-黛色若梦 2024-08-01 23:51:07

我不确定你所说的“标准方式”是什么意思,但你当然可以以编程方式做到这一点:

// In MyDlg.h
class CMyDlg : public CDialog
{
...
    CMFCToolBar m_ToolBar;
...
};

// In MyDlg.cpp
BOOL CMyDlg::OnInitDialog()
{
...
    if( m_ToolBar.Create( this, AFX_DEFAULT_TOOLBAR_STYLE, 100 ) )
    {
        m_ToolBar.SetPaneStyle( m_ToolBar.GetPaneStyle() 
            & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_ANY) );

        m_ToolBar.InsertButton( CMFCToolBarButton( ID_APP_ABOUT, -1, _T("About") ) );
        m_ToolBar.InsertButton( CMFCToolBarButton( ID_APP_EXIT, -1, _T("Exit") ) );

        CSize   sizeToolBar = m_ToolBar.CalcFixedLayout( FALSE, TRUE );
        m_ToolBar.SetWindowPos( NULL, 0, 0, sizeToolBar.cx, sizeToolBar.cy,
            SWP_NOACTIVATE | SWP_NOZORDER );
    }
...
}

I'm not sure what you mean by "the standard way", but you can certainly do it programatically:

// In MyDlg.h
class CMyDlg : public CDialog
{
...
    CMFCToolBar m_ToolBar;
...
};

// In MyDlg.cpp
BOOL CMyDlg::OnInitDialog()
{
...
    if( m_ToolBar.Create( this, AFX_DEFAULT_TOOLBAR_STYLE, 100 ) )
    {
        m_ToolBar.SetPaneStyle( m_ToolBar.GetPaneStyle() 
            & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_ANY) );

        m_ToolBar.InsertButton( CMFCToolBarButton( ID_APP_ABOUT, -1, _T("About") ) );
        m_ToolBar.InsertButton( CMFCToolBarButton( ID_APP_EXIT, -1, _T("Exit") ) );

        CSize   sizeToolBar = m_ToolBar.CalcFixedLayout( FALSE, TRUE );
        m_ToolBar.SetWindowPos( NULL, 0, 0, sizeToolBar.cx, sizeToolBar.cy,
            SWP_NOACTIVATE | SWP_NOZORDER );
    }
...
}
我不咬妳我踢妳 2024-08-01 23:51:07

如果您只需处理仅对话框命令,技巧是将 SetRouteCommandsViaFrame 设置为 FALSE。 然后所有者(通常是对话框)将用于命令而不是主框架。

If you only have to deal with dialog-only commands the trick is to set SetRouteCommandsViaFrame to FALSE. Then the owner (usually the dialog) will be used for commands instead of the main frame.

·深蓝 2024-08-01 23:51:07

上面的 OnInitDialog 确实工作得很好,除非该命令是仅对话框命令。

如果没有应用程序或主机处理程序,按钮将自行禁用并且不会调用处理程序。 请注意,上述代码路由正确,因为 ID_APP_ABOUTID_APP_EXIT 已经具有 CWinAppEx 处理程序。

不涉及重写 OnCmdMsg 的解决方法是在应用程序或主机级别添加处理程序。 然后工具栏保持活动状态,并将正确找到正确的对话框处理程序。 向 CMainFrame 添加一个简单的 void 处理程序就可以解决问题(下面的示例代码)。 未调用 void 处理程序。

BOOL CMyDlg::OnInitDialog()                    
{                    
    ...  
    m_ToolBar.InsertButton( CMFCToolBarButton( **ID_DLG_COMMAND**, -1, _T("DlgCommand") ) ); 
    ...     
}    

//CMainFrame
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWndEx)
...
    ON_COMMAND(**ID_DLG_COMMAND**, VoidHandler)
END_MESSAGE_MAP()

void CMainFrame::VoidHandler()
{
    assert(0);
}

The OnInitDialog above does work well except in cases where the command is a dialog-only command.

If there is no application or main-frame handler the button disables itself and the handler is not called. Note that the above code routes correctly because ID_APP_ABOUT and ID_APP_EXIT already have CWinAppEx handlers.

A workaround that does not involve overriding OnCmdMsg is to add a handler at the application or main-frame level. The toolbar then stays active and will correctly find its way to the proper dialog handler. Adding a simple void handler to CMainFrame does the trick (sample code below). The void handler is not called.

BOOL CMyDlg::OnInitDialog()                    
{                    
    ...  
    m_ToolBar.InsertButton( CMFCToolBarButton( **ID_DLG_COMMAND**, -1, _T("DlgCommand") ) ); 
    ...     
}    

//CMainFrame
BEGIN_MESSAGE_MAP(CMainFrame, CMDIFrameWndEx)
...
    ON_COMMAND(**ID_DLG_COMMAND**, VoidHandler)
END_MESSAGE_MAP()

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