Flat Toolbar (requires IE3+)

. The toolbar class discussed in this article works with the standard MFC CToolBar.

NOTE: You must also have new the COMCTL32.DLL (version 4.7 or later). This is installed with Internet Explorer 3 and will come standard with the new Windows 98. So if you are using VC++ 5 then you probably already have this DLL.

Here is a toolbar enhancement that gives you DevStudio-like toolbars.

These have a flat look with a "gripper" on the left hand side and separator lines between groups. As the mouse moves over the toolbar, the tools get a button created around them.

There are a number of issues that needed to be addressed here.

MFC uses the style bits that control the flat look for its own purposes. So you cannot set this style when creating the toolbar, but must modify the style afterwards. To do this, I have added a SetFlatLookStyle() function.

Flat look toolbars are painted transparently. Unfortunately, MFC has not been written with transparent toolbars in mind, so one needs to repaint the background. This is done on size and move messages, for example when draggin a docked toolbar. They are also done when the tool button's style changes. For example, when a button changes from pushed to released, the background needs to be repainted.

The toolbar control does not draw the separators between groups of toolbar buttons, but instead leaves a gap. The class intecepts the WM_PAINT method and adds separators at the correct position.

The toolbar control also does not support the gripper at the left or top end of the toolbar. The class adjusts the client area and draws the gripper approriately.

To use this class, simply change your use of CToolBar to CFlatToolBar and call the SetFlatLookStyle() function after creating the toolbar itself (ie. after toolbar bitmaps are loaded).

// FlatToolBar.h
// (c) 1997, Roger Onslow

#if !defined(__FLATTOOLBAR_H__)
#define __FLATTOOLBAR_H__

#if _MSC_VER >= 1000
#pragma once
#endif // _MSC_VER >= 1000

class CFlatToolBar : public CToolBar 
	void SetFlatLookStyle();
	void RepaintBackground();
	void DrawSeparators();
	void DrawSeparators(CClientDC* pDC);
	void EraseNonClient();
	void DrawGripper(CWindowDC *pDC, CRect& rectWindow);
	// ClassWizard generated virtual function overrides
	virtual void OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler);
	// Message Handlers
	afx_msg void OnWindowPosChanging(LPWINDOWPOS lpWndPos);
	afx_msg void OnPaint();
	afx_msg void OnNcPaint();
	afx_msg void OnNcCalcSize( BOOL bCalcValidRects, NCCALCSIZE_PARAMS*	lpncsp );


// Microsoft Developer Studio will insert additional declarations immediately before the previous line.

#endif // !defined(__FLATTOOLBAR_H__)

// FlatToolBar.cpp
// (c) 1997, Roger Onslow

#include "stdafx.h"
#include "FlatToolBar.h"

#ifdef _DEBUG
#undef THIS_FILE
#define new DEBUG_NEW

static char BASED_CODE THIS_FILE[] = __FILE__;



// Must set after creation, as MFC wipes out extra style bits
void CFlatToolBar::SetFlatLookStyle()
	// Set flat style (transparent)
	// others are...
	// #define TBSTYLE_TOOLTIPS        0x0100
	// #define TBSTYLE_WRAPABLE        0x0200
	// #define TBSTYLE_ALTDRAG         0x0400
	// #define TBSTYLE_FLAT            0x0800
	// #define TBSTYLE_LIST            0x1000

// Because buttons are transparent, we need to repaint the background
void CFlatToolBar::RepaintBackground()
	// get parent window (there should be one)
	CWnd* pParent = GetParent();
	if (pParent) {
		// get rect for this toolbar
		CRect rw; GetWindowRect(&rw);
		// convert rect to parent coords
		CRect rc = rw; pParent->ScreenToClient(&rc);
		// invalidate this part of parent
		// now do all the other toolbars (etc) that belong to the parent
		for (
			CWnd* pSibling = pParent->GetWindow(GW_CHILD);
		pSibling = pSibling->GetNextWindow(GW_HWNDNEXT)
			) {
			// but do not draw ourselves
			if (pSibling == this) continue;
			// convert rect to siblings coords
			CRect rc = rw; pSibling->ScreenToClient(&rc);
			// invalidate this part of sibling

// Draw the separators in the client area
void CFlatToolBar::DrawSeparators()
	// get a dc for the client area
	CClientDC dc(this);
	// draw the separators on it

// Draw the separators
void CFlatToolBar::DrawSeparators(CClientDC* pDC)
	// horizontal vs vertical
	bool ishorz = (m_dwStyle & CBRS_ORIENT_HORZ) != 0;
	// get number of buttons
	int nIndexMax = (int)DefWindowProc(TB_BUTTONCOUNT, 0, 0);
	int nIndex;
	// try each button
	for (nIndex = 0; nIndex < nIndexMax; nIndex++)
		UINT dwStyle=GetButtonStyle(nIndex);
		UINT wStyle=LOWORD(dwStyle);
		// if it is a separator
		if (wStyle == TBBS_SEPARATOR)
			// get it's rectangle and width
			CRect rect;
			// if small enough to be a true separator
			int w=rect.Width();
			if (w <= 8)
				if (ishorz)
					// draw the separator bar in the middle
					CRect rectbar=rect;
					int x=(rectbar.left+rectbar.right)/2;
					// draw the separator bar in the middle
					CRect rectbar = rect;
					rectbar.left = rectbar.left - m_sizeButton.cx;
					rectbar.right = rectbar.left + m_sizeButton.cx;
					rectbar.top = rectbar.bottom+1;
					rectbar.bottom = rectbar.top+3;
					int y = (rectbar.top+rectbar.bottom)/2;
					rectbar.top = y-1;
					rectbar.bottom = y+1;

// Draw the gripper at left or top
void CFlatToolBar::DrawGripper(CWindowDC *pDC, CRect& rectWindow)
	// get the gripper rect (1 pixel smaller than toolbar)
	CRect gripper = rectWindow;
	if (m_dwStyle & CBRS_FLOATING) {
		// no grippers
	} else if (m_dwStyle & CBRS_ORIENT_HORZ) {
		// gripper at left
		gripper.right = gripper.left+3;
		rectWindow.left += 8;
	} else {
		// gripper at top
		gripper.bottom = gripper.top+3;
		rectWindow.top += 8;

// Erase the non-client area (borders) - copied from MFC implementation
void CFlatToolBar::EraseNonClient()
	// get window DC that is clipped to the non-client area
	CWindowDC dc(this);
	CRect rectClient;
	CRect rectWindow;
	rectClient.OffsetRect(-rectWindow.left, -rectWindow.top);
	// draw borders in non-client area
	rectWindow.OffsetRect(-rectWindow.left, -rectWindow.top);
	DrawBorders(&dc, rectWindow);
	// erase parts not drawn
	SendMessage(WM_ERASEBKGND, (WPARAM)dc.m_hDC);
	DrawGripper(&dc, rectWindow); // 

// Because buttons are transparaent, we need to repaint background if style changes 
void CFlatToolBar::OnUpdateCmdUI(CFrameWnd* pTarget, BOOL bDisableIfNoHndler)
    static CUIntArray styles; // get the number of buttons
    int nIndexMax=(int)DefWindowProc(TB_BUTTONCOUNT, 0, 0); 
    int nIndex; // save styles 
    for (nIndex=0; nIndex < nIndexMax; nIndex++)
        UINT dwStyle=GetButtonStyle(nIndex);
    // do base class processing 
    // make checked button appear pushed in 
    for (nIndex=0; nIndex < nIndexMax; nIndex++) 
        UINT dwStyle=GetButtonStyle(nIndex);
        if (dwStyle & TBBS_DISABLED) 
            // don't touch if disabled (avoids flicker) 
        else if (dwStyle & TBBS_CHECKBOX) 
            UINT dwStyleWas=dwStyle; // if checked, make it pressed, else not pressed 
            if (dwStyle & TBBS_CHECKED) 
                dwStyle |=TBBS_PRESSED;
            else if (!(styles[nIndex]&TBBS_CHECKED) && (styles[nIndex]&TBBS_PRESSED)) 
                dwStyle |=TBBS_PRESSED; 
                dwStyle &=~TBBS_PRESSED;
            // set new style if changed 
            if (dwStyleWas !=dwStyle) SetButtonStyle(nIndex,dwStyle); 
    // check for changes to style (buttons presssed/released)
    for (nIndex=0; nIndex < nIndexMax; nIndex++) 
        UINT dwStyle=GetButtonStyle(nIndex);
        if (styles[nIndex] !=dwStyle) {
            // repaint whole toolbar (not just this button) 
            // no need to check any more 

// Because buttons are transparent, we need to repaint background on size or move 
void CFlatToolBar::OnWindowPosChanging(LPWINDOWPOS lpwp) 
    // default processing 

// Paint the toolbar 
void CFlatToolBar::OnPaint() 
    // standard tolbar 
    // erase the background 
    // plus separators 

// Paint the non-client area copied from MFC implementatios 
void CFlatToolBar::OnNcPaint() 
    // EraseNonClient(); don't do it here 

// Calculate the non-client area adjusting for grippers 
void CFlatToolBar::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp) 
    // adjust non-client area for gripper at left or top 
    if (m_dwStyle & CBRS_FLOATING) 
        // no grippers 
    else if (m_dwStyle & CBRS_ORIENT_HORZ) 
        // move 2 pixels right to make room 
        lpncsp->rgrc[0].left += 2;
        lpncsp->rgrc[0].right += 2;
        // move 4 pixels downto make room
        lpncsp->rgrc[0].top += 4;
        lpncsp->rgrc[0].bottom += 4;


  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • As all sorts of data becomes available for storage, analysis and retrieval - so called 'Big Data' - there are potentially huge benefits, but equally huge challenges...
  • The agile organization needs knowledge to act on, quickly and effectively. Though many organizations are clamouring for "Big Data", not nearly as many know what to do with it...
  • Cloud-based integration solutions can be confusing. Adding to the confusion are the multiple ways IT departments can deliver such integration...

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date