Another Flat ToolBar (does not require MSIE)

For those of you, who have already read this article: The major
enhancements start here
.
Changes for Revision 2.
Download section
I’m looking for someone who has enough time to help me !

Some time ago, I saw Roger Onslow’s flat toolbar implementation. The fact, that I need
a special product (MSIE) (or even DLL ->comctl32.dll) was somewhat inconvenient to me.
So I started to develop my own version of a flat looking toolbar without such
requirements. The result is a class called CToolBarEx.

With CToolBarEx one can toggle between flat- and "classic"-mode. The
appropriate look will be shown immediataly.

Don’t wonder if some parts of the code seem to be well known to you. The drawing of
separators and the gripper was (more or less) stolen from Roger’s toolbar (why should I do
all of the hard bits again 😉

In flat-mode CToolBarEx makes all of the drawings by itself; in classic-mode, MFC does
the work.

Since VC++ >= 4.2 provides custom-draw abilities, this feature will be emulated in
flat mode by a local helper class, so one can use this feature regardless of the current
mode (flat or classic). To get some further informations on owner-drawing, have a look at
the implementation file ToolBarEx.cpp. The MainFrm.cpp in the sample (165KB) application may provide even more
informations, if you’re not familiar with owner-drawing on toolbars.

However, CToolBarEx should be able to work with older versions of VC++ too …

CToolBarEx consists of two files:
ToolBarEx.h
ToolBarEx.cpp
Download Source 8KB
Download Project 165KB

To use CToolBarEx in an MFC application, you have to perform the following steps (I
assume you use App-/Class-Wizard):
1. #include "ToolBarEx.h"
     in either StdAfx.h or MainFrm.h

2. Change the type of CMainFrame::m_wndToolBar from
    CToolBar to CToolBarEx

The CToolBarEx class provides the following public methods (in addition to its
ancestor-classes):

// Set the mode of the toolbar. The mode changes immediately
// on the screen.
void SetFlatLook( BOOL bFlat = TRUE );

// determine wether the current mode is "flat"
BOOL IsFlatLook() const;

// This function I've personally missed in CToolBar
// for more than one time ...
void GetSizes( CSize & sizeButton, CSize & sizeImage ) const;

// Get the window to which the toolbar initially was docked. This
// is not necessarily the window returned by CWnd::GetParent() or
// CControlBar::GetDockingFrame(). Both these functions don't
// return the expected window, if the toolbar is floating ...
CWnd * GetParentFrame() const;

Update: The code has now been enhanced

  • texts on buttons now work
  • gripper improved for a closer look like Office97
  • disabled images now look embossed
  • (thanks to Victor Vogelpoel)
    (Zafir: Also to Oscar who sent in the same fix)
  • a separator is drawn only if it has no WRAP state set
  •  


New style (as in Office or DevStudio).

To enable the new 3D style you have to exchange the call to EnableDocking() in your
CMainFrame’s OnCreate() method with a call to FrameEnableDocking():

// original code:
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    // ...
    EnableDocking(CBRS_ALIGN_ANY);
    DockControlBar(&m_wndToolBar);

    // ...
    return 0;
}

// changed code to get "real" 3D style:

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)

    // ...
    FrameEnableDocking(this, CBRS_ALIGN_ANY);

    DockControlBar(&m_wndToolBar);

    // ...
    return 0;
}

Help !

Since my commercial projects have a higher priority than my hobby projects, I don’t
have enough time to complete the persistence and customization of the toolbar in a
reasonable time. I’m looking for someone, who wants to help me completing this. I have a
somewhat more advanced version of the class, that implements some aspects of these issues
(send me mail to get the source and see the images below). Both these problems got more
complex than I expected …

Context Menu
context menu of the toolbar

Toolbar Dialog Resource
"Toolbar" customization dialog in the resource

Toolbar Dialog
"Toolbar" customization dialog in action

CToolBarEx still consists of two files:
CToolBarEx.h and
CToolBarEx.cpp
Download enhanced source 25K
Download enhanced sample 56K

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read