Is it possible to add CMFCToolBar to a dialog?

2019-05-27 09:01发布

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?

3条回答
▲ chillily
2楼-- · 2019-05-27 09:42

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 );
    }
...
}
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-05-27 09:53

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);
}
查看更多
唯我独甜
4楼-- · 2019-05-27 09:55

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.

查看更多
登录 后发表回答