Adding a Toolbar to a Dialog-Based Application

WEBINAR: On-demand webcast

How to Boost Database Development Productivity on Linux, Docker, and Kubernetes with Microsoft SQL Server 2017 REGISTER >

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

First:

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
if(!m_wndToolBar.Create(this) 
|| !m_wndToolBar.LoadToolBar(IDR_TOOLBAR1))
{
 TRACE0("Failed to Create Dialog Toolbar\n");
 EndDialog(IDCANCEL);
}

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.
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,
 AFX_IDW_CONTROLBAR_LAST,0,reposQuery,rcClientNew);

// 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,
 rcClientNew.top-rcClientOld.top);

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
 ScreenToClient(rcChild); 

// Changes the Child Rect by the values of the claculated offset
 rcChild.OffsetRect(ptOffset); 
  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
RepositionBars(AFX_IDW_CONTROLBAR_FIRST,
 AFX_IDW_CONTROLBAR_LAST,0);
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...

Downloads

Download demo project - 27 Kb


Comments

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

    Posted by pean on 05/16/2007 05:49am

    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.

    Reply
  • A very light mistake on text in web page

    Posted by r-gimilio on 02/18/2007 04:27am

    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.

    Reply
  • Very easy to implement

    Posted by r-gimilio on 02/17/2007 03: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

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

    Posted by anup_daware on 10/06/2006 03: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

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

    Posted by preci0usGal on 01/25/2006 04:19am

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

    Reply
  • Can i do the same within a child Dialog

    Posted by Legacy on 02/18/2004 12: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 03:04pm

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

      Reply
    Reply
  • how can make dockable that toolbar ?

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

    Originally posted by: vanta

    how can make doc that toolbar ?

    Reply
  • how do i do it in SDK win32

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

    Originally posted by: Pavan

    how to add toolbar and images on it using WIN32 SDK

    Reply
  • D�j� Vu....

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

    Originally posted by: Boulifb

    Hey, did you invented this code?

    Take a look:
    http://www.codeguru.com/dialog/toolbars.shtml

    Best regards.

    Bouli.

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

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

    Originally posted by: Rune-E

    Hi!
    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!

    Reply
  • 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.

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