Virtual Developer Workshop: Containerized Development with Docker

Environment:Visual C++ 6

Annoying as it is, the Visual C does not provide an automatic Toolbar for Dialog Classes. Many of the Apps I create are Dialog based. I enjoy providing the user with the prototypical MS toolbar for starting events.

I realize this article may be simplistic, however, many of the readers (I include myself in this category) often are starting from scratch on a project and require some easy code to accomplish a small task. So why re-invent the wheel.

What to do


In the Resource Editor insert a new toolbar resource. Create the Buttons on the Toolbar and assign each button a unique ID (as you would for a normal MFC Doc/View project); For our case we will call this resource IDC_TOOLBAR1

Second: Declare a vraiable in the proteted section of the CDialog Class.

CToolBar m_wndToolBar;
Third: In the CDialog::OnInitDialog member function add the following code:

Note: In order to add the toolbar we need to expand the Client Window size to accomodate the Toolbar.

// Create the Toolbar and attach the resource
|| !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
 TRACE0("Failed to Create Dialog Toolbar\n");

CRect	rcClientOld; // Old Client Rect
CRect	rcClientNew; // New Client Rect with Tollbar Added
GetClientRect(rcClientOld); // Retrive the Old Client WindowSize

// Called to reposition and resize control bars in the client 
// area of a window. The reposQuery FLAG does not really traw the 
// Toolbar.  It only does the calculations. And puts the new 
// ClientRect values in rcClientNew so we can do the rest of the 
// Math.

// All of the Child Windows (Controls) now need to be moved so 
// the Tollbar does not cover them up. Offest to move all child 
// controls after adding Tollbar
CPoint ptOffset(rcClientNew.left-rcClientOld.left,

CRect	rcChild;

// Handle to the Dialog Controls
CWnd*	pwndChild = GetWindow(GW_CHILD);  
while(pwndChild) // Cycle through all child controls
 pwndChild->GetWindowRect(rcChild); // Get the child control RECT

// Changes the Child Rect by the values of the claculated offset
  pwndChild->MoveWindow(rcChild,FALSE); // Move the Child Control
  pwndChild = pwndChild->GetNextWindow();

CRect	rcWindow;
GetWindowRect(rcWindow); // Get the RECT of the Dialog

// Increase width to new Client Width
rcWindow.right += rcClientOld.Width() - rcClientNew.Width(); 

// Increase height to new Client Height
rcWindow.bottom += rcClientOld.Height() - rcClientNew.Height(); 

MoveWindow(rcWindow,FALSE); // Redraw Window

// Now we REALLY Redraw the Toolbar
That's it. It's not dockable, but it sure beats trying to put a lot of little buttons on the Dialog.

Several people requested a demo and how to handle the buttons. Attached is a samll sample demo. Doesn't really do much. What needs to be done to add functionality. When you add the toolbar. Click on each button and add an ID (I used IDM_A, IDM_B, etc). Then from the Class Wizard. Select the ID and Add the function ON_COMMAND. Since I have not been able to get the CMDUI update function working, I simply added a pointer to the toolbar and retrieved the info for the specific button, then changed the button style and reset the button either enabling (0) or disabling(TBBS_DISABLED) the button. Hope this helps...


Download demo project - 27 Kb


  • Help me in Design in interface Windows Explorer(My Computer)

    Posted by pean on 05/16/2007 12:49pm

    hello,My name's VanPhuong.I come from Vietnam.
    Can you help me?
    now i'm studying a  subject plan in Programming Windows.Its name is 
    design in interface Windows Explorer(My Computer).Now i'm a student in 
    physics at University of Natural Sciences.So i don't understand clearly 
    in VC++.Because i hope you'll send me the code of Windows Explorer 
    which is written in VC++ in SDI.
    please sent me in email:oosoompl@yahoo.com
    thank you very much.

  • A very light mistake on text in web page

    Posted by r-gimilio on 02/18/2007 12:27pm

    You write in this web page: "For our case we will call this resource IDC_TOOLBAR1" It must be: "For our case we will call this resource IDR_TOOLBAR1". Anyway, adjust to the name given to your toolbar. It is correct in the demo project who works fine.

  • Very easy to implement

    Posted by r-gimilio on 02/17/2007 11:00pm

    Thanks for helping to understand MFC and its objects. It is useful to have a dialog box application with satus bar. Explanations very clear. Raymond GIMILIO

  • How do I put this Toolbar on specific position on the dialog???

    Posted by anup_daware on 10/06/2006 10:53am

    Hi, This all is fine and useful but the How do I put this Toolbar on specific position on the dialog??? I dont want it at top left then?? Is there any way out. Regards, Anup Daware anup.daware@gmail.com

  • Regards to this adding of toolbar to the dialog based application...

    Posted by preci0usGal on 01/25/2006 12:19pm

    i followed ur steps very closely. but my toolbar is not appearing in my dialog box. why is that so?

  • Can i do the same within a child Dialog

    Posted by Legacy on 02/18/2004 08:00am

    Originally posted by: Ahryman40k

    If i do the same thing on dialog which have Child style, the toolbar is always disable, how can i enable it again ???

    • Yes, you can

      Posted by r-gimilio on 02/17/2007 11:04pm

      Please put the code in your child Class code. Use class wizard to create OnInitDialog and OnDrawItem in your CYourDialog .cpp file.

  • how can make dockable that toolbar ?

    Posted by Legacy on 01/08/2004 08:00am

    Originally posted by: vanta

    how can make doc that toolbar ?

  • how do i do it in SDK win32

    Posted by Legacy on 10/15/2003 07:00am

    Originally posted by: Pavan

    how to add toolbar and images on it using WIN32 SDK

  • D�j� Vu....

    Posted by Legacy on 07/31/2003 07:00am

    Originally posted by: Boulifb

    Hey, did you invented this code?

    Take a look:

    Best regards.


  • How do I change the position of the toolbar?!

    Posted by Legacy on 11/27/2002 08:00am

    Originally posted by: Rune-E

    Thanks for this great article! Really helped me a lot!
    But I'm wondering if there is a way to change the position of the toolbar and make it appear on the left side of the dialog and have all the buttons grouped in two columns?!
    I would be very grateful for any information on how o do this!
    Thanks in advance!

  • Loading, Please Wait ...

  • You must have javascript enabled in order to post comments.

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

Most Popular Programming Stories

More for Developers

RSS Feeds

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