Is it possible to add CMFCToolBar to a dialog?

柔情痞子 提交于 2019-12-07 21:56:36

问题


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?


回答1:


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 );
    }
...
}



回答2:


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);
}



回答3:


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.



来源:https://stackoverflow.com/questions/1063156/is-it-possible-to-add-cmfctoolbar-to-a-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!