Click here to Skip to main content
11,796,393 members (72,132 online)
Click here to Skip to main content

How to place controls on toolbars

, 12 Jan 2000 237.5K 122
Rate this:
Please Sign up or sign in to vote.
How to place combo-boxes, edit boxes, progress controls, etc. into toolbars
<!-- Article Starts -->

It is very easy (once you see how it is done) to place combo-boxes, edit boxes, progress controls, etc. into toolbars. Below are two examples of this, in the first a ComboBox is placed on a toolbar, and in the second a cluster of checkboxes is added. In both cases the technique is the same:

Step 1: Place a button on the toolbar in the spot where you want the control(s) to eventually be. YOU MUST place a seperator on either side of the button!. Give the button an easily remembered resource name such as IDP_PLACEHOLDER2 in the example below.

Step 2: Derive a class from CToolBar and give it a member variable for the control you will be creating. For the ComboBox example that class looks like this. No extra methods are required, just a place for the instance of the control to live.

class CMainToolBar : public CToolBar 
    CComboBox m_wndSnap;

Step 3: In your main frame's .h file replace the instance of the CToolBar with you new class. Be sure to add an include statement for the class definition created in step 1.

protected: // control bar embedded members
    CStatusBar m_wndStatusBar;
    CMainToolBar m_wndToolBar;

Step 4: At the end of your main frame's OnCreate method you replace the placeholder button with your control as follows:

int SMCMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    //Body of On Create goes here

    #define SNAP_WIDTH 80 //the width of the combo box

    //set up the ComboBox control as a snap mode select box
    //First get the index of the placeholder's position in the toolbar
    index = 0;
    while (m_wndToolBar.GetItemID(index) != IDP_PLACEHOLDER2) index++;

    //next convert that button to a seperator and get its position
    m_wndToolBar.SetButtonInfo(index, IDP_PLACEHOLDER2, TBBS_SEPARATOR,
    m_wndToolBar.GetItemRect(index, &rect);

    //expand the rectangle to allow the combo box room to drop down;
    rect.bottom += 200;

    // then .Create the combo box and show it
    if (!m_wndToolBar.m_wndSnap.Create(WS_CHILD|WS_VISIBLE|CBS_AUTOHSCROLL| 
                                       rect, &m_wndToolBar, IDC_SNAP_COMBO))
        TRACE0("Failed to create combo-box\n");
        return FALSE;

    //fill the combo box
    m_wndToolBar.m_wndSnap.AddString("SNAP OFF");
    m_wndToolBar.m_wndSnap.AddString("SNAP GRID");
    m_wndToolBar.m_wndSnap.AddString("SNAP RASTER");
    m_wndToolBar.m_wndSnap.AddString("SNAP VERTEX");
    m_wndToolBar.m_wndSnap.AddString("SNAP LINE");

The result looks like this:

Here is one that is a little trickier:

Four check boxes are placed in the toolbar. In addition to adding multiple controls in place of a single button this example shows how to change the font of the checkboxes.

1. Derive the new toolbar class and add it to the main frame. Also add a CFont called gSmallFont to the Main Frame.

class CCoupleToolBar : public CToolBar
    CButton m_wndCenter;
    CButton m_wndEdge;
    CButton m_wndTrack;
    CButton m_wndZoom;

2. Place a placeholder button on the toolbar resource MAKING SURE to leave a space on either side. This is done just as in the first example.

3. At the end of OnCreate in the main frame, first set up the font we are going to use, then replace the placeholder button with the new controls.

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    // Body of OnCreate here
    //set up the small font to use for the button text
    // remember to declare CFont gSmallFont in the .h file
    // font for tool bar use
    CClientDC DC(GetDesktopWindow());
    long logical_pixels = DC.GetDeviceCaps(LOGPIXELSX);
    if(logical_pixels <100)

        gSmallFont.CreatePointFont(67, "DEFAULT", NULL);  
        gSmallFont.CreatePointFont(50, "DEFAULT", NULL); 
    //create the four check boxes  
    #define CHECK_WIDTH 94  
    int index;   
    CRect rect;   
    CRect safe_rect;
    index = 0;  
    while (m_wndViewBar.GetItemID(index) != IDP_PLACHOLDER) index++;     
    // Create the "CENTER" check box   
    m_wndViewBar.SetButtonInfo(index, IDP_PLACHOLDER, TBBS_SEPARATOR,
    m_wndViewBar.GetItemRect(index, &rect); 
    rect.left +=2;   
    rect.right=rect.left + ((CHECK_WIDTH / 2)-4);; + 10;   
    if (!m_wndViewBar.m_wndCenter.Create("CNTR",
        BS_CHECKBOX|WS_CHILD|WS_VISIBLE, rect, &m_wndViewBar,
        TRACE0("Failed to create CENTER check-box\n");       
        return FALSE;    
        (WPARAM)HFONT(gSmallFont),TRUE); = rect.bottom += 2;   
    rect.bottom = + 10;    
    if (!m_wndViewBar.m_wndEdge.Create("EDGE",
        BS_CHECKBOX|WS_CHILD|WS_VISIBLE, rect, &m_wndViewBar,
        TRACE0("Failed to create EDGE check-box\n");        
        return FALSE; 
        (WPARAM)HFONT(gSmallFont), TRUE);
    rect = safe_rect;
    rect.left += ((CHECK_WIDTH / 2) + 4); = 2;

    rect.bottom = + 10; 
    if (!m_wndViewBar.m_wndZoom.Create("ZOOM",
        BS_CHECKBOX|WS_CHILD|WS_VISIBLE, rect, &m_wndViewBar,
        TRACE0("Failed to create ZOOM check-box\n");    
        return FALSE;   
        (WPARAM)HFONT(gSmallFont),TRUE); = rect.bottom += 2;   
    rect.bottom = + 10;  
    if (!m_wndViewBar.m_wndTrack.Create("TRKR",
        BS_CHECKBOX|WS_CHILD|WS_VISIBLE, rect, &m_wndViewBar,
        TRACE0("Failed to create EDGE check-box\n");    
        return FALSE;   
        (WPARAM)HFONT(gSmallFont), TRUE); 


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Randy More
United States United States
No Biography provided

You may also be interested in...

Comments and Discussions

Generalthe &quot;little&quot; ComboBox Pin
Cynicannibal29-Oct-03 23:55
sussCynicannibal29-Oct-03 23:55 
GeneralCatch Index Pin
Thim7-Oct-03 2:41
sussThim7-Oct-03 2:41 
GeneralRe: Catch Index Pin
Thim7-Oct-03 4:06
memberThim7-Oct-03 4:06 
QuestionWhy can't I place a CStatic on the toolbar? Pin
Highersong5-Jun-03 0:28
memberHighersong5-Jun-03 0:28 
AnswerRe: Why can't I place a CStatic on the toolbar? Pin
Brendan Tregear6-Aug-03 15:41
memberBrendan Tregear6-Aug-03 15:41 
GeneralRe: Why can't I place a CStatic on the toolbar? Pin
vikas amin28-Sep-05 23:05
membervikas amin28-Sep-05 23:05 
QuestionHow to Catch Event from the VB ActiveX Control from ToolBar ? Pin
DominicOn1-May-03 10:39
memberDominicOn1-May-03 10:39 
GeneralMenu on toolbar Pin
CSZX3-Apr-02 22:49
memberCSZX3-Apr-02 22:49 
QuestionHow to use Drop Down Arrow to show a wnd, just as what the arrow by Undo-button does in Visual Studio? Pin
msvcna26-Mar-02 16:46
membermsvcna26-Mar-02 16:46 
GeneralResizing Embedded Controls Pin
Marcus Carey21-Mar-02 14:06
memberMarcus Carey21-Mar-02 14:06 
Generalset focus & lost focus Pin
piyer19-Mar-02 21:56
memberpiyer19-Mar-02 21:56 
GeneralCapturing data from ComboBox Pin
Stew2-Jan-02 5:30
memberStew2-Jan-02 5:30 
GeneralWondering what I'm doing wrong Pin
Stew1-Jan-02 16:50
memberStew1-Jan-02 16:50 
GeneralRe: Wondering what I'm doing wrong Pin
Anonymous28-Jun-02 14:43
memberAnonymous28-Jun-02 14:43 
Generalsome code to enable checkboxes Pin
Aydin Bakir6-Dec-01 3:13
memberAydin Bakir6-Dec-01 3:13 
GeneralWhen toolbar is too small... Pin
Vale30-Oct-01 4:11
memberVale30-Oct-01 4:11 
GeneralCustom-colored buttons on a toolbar Pin
Amir Kibar10-Oct-01 1:54
memberAmir Kibar10-Oct-01 1:54 
GeneralNeed to add button to default toolbar in WinSDK Pin
Ashley Pinto8-Oct-01 6:41
memberAshley Pinto8-Oct-01 6:41 
GeneralToolbar without C++ Pin
Novacain2-Aug-01 21:50
memberNovacain2-Aug-01 21:50 
GeneralTooltips Pin
Jean-Yves26-Dec-00 21:21
memberJean-Yves26-Dec-00 21:21 
GeneralRe: Tooltips Pin
Uma Mahes28-Oct-03 19:50
memberUma Mahes28-Oct-03 19:50 
GeneralRe: Tooltips Pin
DenisZ4-Nov-03 2:10
sussDenisZ4-Nov-03 2:10 
GeneralOver and over Pin
Alexander27-Jul-00 1:50
sussAlexander27-Jul-00 1:50 
GeneralRe: Over and over Pin
User27-Jul-00 1:59
sussUser27-Jul-00 1:59 
GeneralRe: Over and over Pin
Alexander27-Jul-00 3:15
sussAlexander27-Jul-00 3:15 
GeneralRe: Over and over Pin
Shyguy from China6-Nov-00 20:38
memberShyguy from China6-Nov-00 20:38 
GeneralRe: Over and over Pin
Member #408077827-Jan-08 18:08
memberMember #408077827-Jan-08 18:08 
GeneralRe: Over and over Pin
Member #408077827-Jan-08 18:14
memberMember #408077827-Jan-08 18:14 
GeneralRe: Over and over Pin
Dave R17-Aug-00 4:32
sussDave R17-Aug-00 4:32 
GeneralRe: Over and over Pin
Andy Metcalfe11-Oct-00 1:31
sussAndy Metcalfe11-Oct-00 1:31 
GeneralRe: Over and over Pin
Ray Romero22-Nov-00 7:13
memberRay Romero22-Nov-00 7:13 
Masoud Samimi30-Dec-00 9:43
memberMasoud Samimi30-Dec-00 9:43 
GeneralRe: Over and over Pin
cypherljk30-Dec-00 7:27
membercypherljk30-Dec-00 7:27 
GeneralRe: Over and over Pin
Warren Gardner10-Jul-01 10:21
memberWarren Gardner10-Jul-01 10:21 
QuestionWhat about edit/spin combinations? Pin
Rob Edwards9-Jun-00 10:13
sussRob Edwards9-Jun-00 10:13 
AnswerRe: What about edit/spin combinations? Pin
Masoud Samimi27-Dec-00 0:56
memberMasoud Samimi27-Dec-00 0:56 
GeneralCombo font - too large Pin
Frédéric Marion-Poll5-Mar-00 11:57
sussFrédéric Marion-Poll5-Mar-00 11:57 
GeneralRe: Combo font - too large Pin
Richard Odenweller4-Apr-00 7:23
sussRichard Odenweller4-Apr-00 7:23 
GeneralRe: Combo font - too large Pin
Adam Bialowas18-Aug-01 10:20
memberAdam Bialowas18-Aug-01 10:20 
GeneralRe: Combo font - too large Pin
drhender27-Jun-02 5:33
memberdrhender27-Jun-02 5:33 
GeneralRe: Combo font - too large Pin
k77725-Jun-11 6:08
memberk77725-Jun-11 6:08 
GeneralDrop down combo box in toolbar Pin
Diana Cook11-Feb-00 7:33
sussDiana Cook11-Feb-00 7:33 
GeneralRe: Drop down combo box in toolbar Pin
Thomas Maier15-May-00 0:40
sussThomas Maier15-May-00 0:40 
GeneralRe: Drop down combo box in toolbar Pin
Tim Hodgson1-Jun-00 10:02
sussTim Hodgson1-Jun-00 10:02 
GeneralRe: Drop down combo box in toolbar Pin
aydin bakir6-Dec-01 2:29
memberaydin bakir6-Dec-01 2:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.151002.1 | Last Updated 13 Jan 2000
Article Copyright 2000 by Randy More
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid