General Articles and Tutorials

Remove the close button from a floating toolbar

Author: Kirk Stowell
Platform: Visual C++ MFC
Downloads:
g8_src.zip - Source Files Only [ 1.27 Kb ]

You can remove the close button from a floating toolbar by extending the CToolBar class and overriding the ON_WM_WINDOWPOSCHANGED message handler. This message handler is called whenever the size, position or Z order has changed for the CToolBar. We don't use the WM_SIZE message handler because it is only called when the size alone has changed and may not get called every time we float the toolbar.

Also, we can take advantage of m_pDockBar from the CControlBar base class rather than making a call to GetParent() to get the parent frame of the toolbar. Derive a class from CToolBar, I called this class CToolBarEx, but you can choose one that suits your needs. We only need to remove the close button when the toolbar is floating, so check to see if the bar is actually floating, making sure that m_pDockBar is a valid pointer.

Add this member variable to your new class:

BOOL m_bMenuRemoved;

This will get set to TRUE when we remove the system menu, ensuring that we only remove the system menu when needed.

We will then need to get a pointer to the parent of m_pDockBar to check to see if it is indeed a CDockFrameWnd class so we can safely remove the system menu from the CToolBar.

Here is the code:

#include <afxpriv.h>
void CXToolBar::OnWindowPosChanged(WINDOWPOS FAR* lpwndpos)
{
    CToolBar::OnWindowPosChanged(lpwndpos);
    // should only be called once, when floated.
    if( IsFloating() )
    {
        if( m_pDockBar && !m_bMenuRemoved )
        {
            CWnd* pParent = m_pDockBar-&gt;GetParent();
            if( pParent->IsKindOf(
                          RUNTIME_CLASS(CMiniFrameWnd)))
            {
                pParent->ModifyStyle( WS_SYSMENU, 0, 0 );
                m_bMenuRemoved = TRUE;
            }
        }
    }
    else if( m_bMenuRemoved ) {
        m_bMenuRemoved = FALSE;
    }
}