Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
That completes our work with menus for the moment. Next we turn to working with toolbars. ### Using Dockable Toolbars In this section, we learn to add buttons to toolbars, place images in those buttons, and connect those buttons to code. We also learn to embed other types of controls in addition to buttons into toolbars—we use a combo box, but you could as easily use a text box, list box, or other type of control. Inserting controls into toolbars is not something you can do with the design tools that come with Visual C++, so we do it ourselves. In this example, we add a combo box with three items in it corresponding to three types of graphic figures: Circle, Rectangle, and Rounded Rectangle. The user can select one of these figures and then press a button in the toolbar to draw it this way:
          ------------------------------------------------------
         |File  Edit  View  Choices  Draw  Help          |
         |------------------------------------------------------|
         | ----------------     --- --- --- ---          |
         ||Rectangle     |v|   |   |   |   |   |         |
         | ----------------     --- --- --- ---          |
         |------------------------------------------------------|
         |                                               |
         |          --------------------------------             |
         |         |                             |       |
         |         |                             |       |
         |         |                             |       |
         |         |                             |       |
         |         |                             |       |
         |         |                             |       |
         |          --------------------------------             |
         |                                               |
         |                                               |
          ------------------------------------------------------
Let’s start by creating a new MDI Visual C++ project named Combo. Our first step is to modify the toolbar of our new project to include a combo box. Let’s examine how that works now. #### Creating a New Toolbar Class Because there is no way to add a combo box to a toolbar with the Visual C++ Toolbar Editor, we need to create a new toolbar class in MainFrm.h, based on the Visual C++ CToolBar class. We do that by calling the new toolbar class CToolBar2 and including a member combo box, m_Combo, which is supported by the Visual C++ CComboBox class.
    // MainFrm.h : interface of the CMainFrame class
    //
Visual C++ Control Classes Visual C++ controls are supported by specific classes, like CCombo for combo boxes, CEdit for text boxes, and so on. You can declare objects of these classes, call a Create() function, and then show them in a window yourself.
    /////////////////////////////////////////////////////////////////////////////
    #if !defined(AFX_MAINFRM_H__D36DAA69_9E10_11D1_887F_D42B07C10710__INCLUDED_)
    #define AFX_MAINFRM_H__D36DAA69_9E10_11D1_887F_D42B07C10710__INCLUDED_
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
    class CToolBar2 : public CToolBar       ⇐
    {                                       ⇐
    public:                                 ⇐
        CComboBox m_Combo;                  ⇐
    };                                      ⇐
Next, we change the declaration of the member toolbar in our program to use the CToolBar2 class instead of the default CToolBar class (in MainFrm.h).
    class CMainFrame : public CFrameWnd
    {
    protected: // create from serialization only
        CMainFrame();
        DECLARE_DYNCREATE(CMainFrame)
        boolean m_OVR;
        CToolBar2   m_wndToolBar;                   ⇐
Our toolbar has a combo box member object, but we still need to create and install that combo box. We do one last thing before installing our combo box—provide the rest of the program with a way to find out which item is currently selected in the combo box. #### Interacting with the Toolbar’s Combo Box We need to add a new member function to the main frame class—GetItemNumber()—in MainFrm.h. This function will return the number of the currently selected item in the combo box using the combo box’s GetCurSel() function.
    // Attributes
    public:
    // Operations
    public:
        int GetItemNumber() {return m_wndToolBar.m_Combo.GetCurSel();}⇐
        .
        .
        .
This gives the rest of the program a way of interacting with the combo box in the toolbar, so we should add that combo box to the toolbar. #### Inserting the Combo Box into the Toolbar We can create and add the combo box to the toolbar in the CMainFrame class’s OnCreate() function with the following code, the toolbar itself:
    int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
         return -1;

        if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
         | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
         !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
        {
         TRACE0(“Failed to create toolbar\n”);
         return -1;      // fail to create
        }
Next, we insert the combo box into the toolbar. To do this, we set aside space in the toolbar for the combo box using the little-known SetButtonInfo() function, which is how Visual C++ itself sets up the toolbar. We insert space for the combo box and a little additional space to separate the combo box from the first toolbar tool. To do that, we need two new ID numbers: one for the combo box and one for a toolbar separator, which simply represents space in the toolbar. Create the two ID values—IDC_COMBO and ID_SEPARATOR—with the String Table Editor (the editor supplies numeric values for these IDs). Set aside space in the toolbar for item 0 and item 1; this way, we give the ID of the item we’re inserting and its type (we use the TBBBS_SEPARATOR type here because we’re just setting aside space for the combo box), as well as the width of the control in pixels.
        m_wndToolBar.SetButtonInfo(0, IDC_COMBO, TBBS_SEPARATOR, 150);⇐
        m_wndToolBar.SetButtonInfo(1, ID_SEPARATOR, TBBS_SEPARATOR, 20);⇐
         .
         .
         .
Next, we create the combo box itself. We need to determine the total size of the combo box, including the dropdown part of the combo box, when we create it. We do that with the CToolBar GetItemRect() function, which fills a CRect structure with the size of item 0, the combo box. Then, we add 100 pixels vertically to allow space for the combo box’s dropdown list.
        m_wndToolBar.SetButtonInfo(0, IDC_COMBO, TBBS_SEPARATOR, 150);
        m_wndToolBar.SetButtonInfo(1, ID_SEPARATOR, TBBS_SEPARATOR, 20);
        CRect rCombo;                               ⇐
        m_wndToolBar.GetItemRect(0, &rCombo);       ⇐
        rCombo.bottom += 100;                      ⇐
         .
         .
         .
Now we create the combo box with its Create() function, making it a dropdown combo box that fits in the CRect object we’ve set up, pass a pointer to its parent window (which is the toolbar itself), and the combo box’s ID (IDC_COMBO).
        m_wndToolBar.SetButtonInfo(0, IDC_COMBO, TBBS_SEPARATOR, 150);
        m_wndToolBar.SetButtonInfo(1, ID_SEPARATOR, TBBS_SEPARATOR, 20);
        CRect rCombo;
        m_wndToolBar.GetItemRect(0, &rCombo);
        rCombo.bottom += 100;
        m_wndToolBar.m_Combo.Create(CBS_DROPDOWNLIST | WS_VISIBLE, rCombo,
    &m_wndToolBar, IDC_COMBO);⇐
         .
         .
         .
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.