Search code examples
c++dialogcmfctoolbar

Why is my CMFCToolBar overlapped on CDialog?


I am at the design stage here.

Code:

if (m_ToolBar.CreateEx(this, TBSTYLE_TRANSPARENT | TBSTYLE_FLAT,
    AFX_DEFAULT_TOOLBAR_STYLE, CRect(1, 1, 1, 1), IDR_TOOLBAR))
{
    m_ToolBar.SetPaneStyle(m_ToolBar.GetPaneStyle()
        & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_ANY));

    m_ToolBar.SetBorders();

    m_ToolBar.LoadBitmap(IDB_BMP_SM_IMAGELIST);

    m_ToolBar.InsertButton(CMFCToolBarButton(ID_FILE_OPEN_CHRISTIAN_LIFE_AND_MINISTRY_REPORT, 0, _T("Open MWB"), TRUE));
    m_ToolBar.InsertButton(CMFCToolBarButton(ID_FILE_CREATE_CHRISTIAN_LIFE_AND_MINISTRY_REPORT, 0, _T("Create MWB"), TRUE));
    m_ToolBar.InsertSeparator(2);
    m_ToolBar.InsertButton(CMFCToolBarButton(ID_FILE_OPENREPORT, 0, _T("Open SRR"), TRUE));
    m_ToolBar.InsertButton(CMFCToolBarButton(ID_FILE_CREATEREPORT, 0, _T("Create SRR"), TRUE));
    m_ToolBar.InsertSeparator(5);
    m_ToolBar.InsertButton(CMFCToolBarButton(ID_OPTIONS_PUBLISHERS_DATABASE, 0, _T("Publishers Database"), TRUE));


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

}

I call the above in OnInitDialog of my CDialog.

Why is the toolbar overlapped:

enter image description here

Update

I simplified the code to just use a basic toolbar resource:

if (m_ToolBar.CreateEx(this, TBSTYLE_TRANSPARENT | TBSTYLE_FLAT,
    AFX_DEFAULT_TOOLBAR_STYLE, CRect(1, 1, 1, 1), IDR_TOOLBAR))
{
    m_ToolBar.SetPaneStyle(m_ToolBar.GetPaneStyle()
        & ~(CBRS_GRIPPER | CBRS_SIZE_DYNAMIC | CBRS_BORDER_ANY));

    m_ToolBar.SetBorders();

    m_ToolBar.LoadToolBar(IDR_TOOLBAR);

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

But the results are still the same.

Update

I noticed if I just resize my dialog in the IDE that I then have room for the toolbar. But how to do I accurately resize it in the IDE to allow for a 32 pixel high toolbar?

enter image description here


Solution

  • Someone provided a link to this archived article.

    This is my updated code (called from OnInitDialog):

    void CMeetingScheduleAssistantDlg::CreateToolbar()
    {
        DWORD dwCtrlStyle = TBSTYLE_FLAT | TBSTYLE_TOOLTIPS | CBRS_SIZE_DYNAMIC;
        DWORD dwStyle = AFX_DEFAULT_TOOLBAR_STYLE;
        if (m_ToolBar.CreateEx(this, dwCtrlStyle,
            dwStyle, CRect(1, 1, 1, 1), IDR_TOOLBAR))
        {
            dwStyle = CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC;
            m_ToolBar.SetPaneStyle(m_ToolBar.GetPaneStyle() | dwStyle);
    
            CMFCToolBarInfo info;
    
            info.m_uiColdResID = IDB_PNG_MAIN_TOOLBAR;
            info.m_uiHotResID = IDB_PNG_MAIN_TOOLBAR;
            info.m_uiLargeColdResID = IDB_PNG_MAIN_TOOLBAR;
            info.m_uiLargeHotResID = IDB_PNG_MAIN_TOOLBAR;
    
            m_ToolBar.LoadToolBarEx(IDR_TOOLBAR, info, FALSE);
    
            CSize   sizeToolBar = m_ToolBar.CalcFixedLayout(TRUE, TRUE);
            m_ToolBar.SetWindowPos(NULL, 0, 0, sizeToolBar.cx, sizeToolBar.cy,
                SWP_NOACTIVATE | SWP_NOZORDER);
    
            // Move all controls down
            CPoint ptOffset(0, sizeToolBar.cy);
    
            CRect  rcChild;
            CWnd* pwndChild = GetWindow(GW_CHILD);
            while (pwndChild)
            {
                if (pwndChild->GetSafeHwnd() != m_ToolBar.GetSafeHwnd())
                {
                    pwndChild->GetWindowRect(rcChild);
                    ScreenToClient(rcChild);
                    rcChild.OffsetRect(ptOffset);
                    pwndChild->MoveWindow(rcChild, FALSE);
                }
                pwndChild = pwndChild->GetNextWindow();
            }
    
            // Resize the window
            CRect rcWindow;
            GetWindowRect(rcWindow);
            rcWindow.bottom += sizeToolBar.cy;
            MoveWindow(rcWindow, FALSE);
    
        }
    }
    

    In short I had to manually move down all controls and resize the window. Looks OK now:

    New Toolbar