home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
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

Bookmark It

Search this book:
 
Previous Table of Contents Next


Our combo box is created and connected to the toolbar. At this point, then, we can fill it with the items we need—“Circle,” “Rectangle,” and “Rounded Rectangle”—using the CComboBox’s AddString() function.

    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);
    CString Item;                               ⇐
    Item = “Circle”;                            ⇐


Figure 4.11  The Toolbar Editor.

    m_wndToolBar.m_Combo.AddString(Item);⇐
    Item = “Rectangle”;                         ⇐
    m_wndToolBar.m_Combo.AddString(Item);       ⇐
    Item = “Rounded Rectangle”;                 ⇐
    m_wndToolBar.m_Combo.AddString(Item);       ⇐
       .
       .
       .

Now that we’ve added the combo box, we need to add the drawing tool that is used with the toolbar.

Adding Tools to the Toolbar

We add tools to the toolbar with the Toolbar Editor. To open that editor, double-click the IDR_MAINFRAME item in the Toolbar folder in the ResourceView. This opens the Toolbar Editor, as shown in Figure 4.11.

You can move the tools in the toolbar simply by dragging them with the mouse. Move the new, empty tool into position just before the Help button in the toolbar and click it to open. We simply draw an arrow cursor in that button now, as also shown in Figure 4.11.

Give this new tool the ID ID_DRAW by double-clicking it in the Toolbar Editor and entering ID_DRAW in its property page. Then, using ClassWizard, connect that ID to a new function, OnDrawFigure(). We can’t use OnDraw()because it’s already taken.

void CComboView::OnDrawFigure()
{
    // TODO: Add your command handler code here
}

The program calls this function when we’re supposed to draw a figure. To determine which figure we’re supposed to draw, we need to determine what item is selected in the combo box: Circle, Rectangle, or Rounded Rectangle. We can get that information from the main frame object’s GetItemNumber() function that we’ve added to that object. We place the number of the currently selected item in the combo box into a new member variable, m_FigureType, then we invalidate the view to display the figure.

void CComboView::OnDrawFigure()
{
    m_FigureType = ((CMainFrame*) GetParent())->GetItemNumber();⇐
    Invalidate();                ⇐
}

We complete the drawing process in OnDraw(), depending on the value in m_FigureType.

void CComboView::OnDraw(CDC* pDC)
{
    CComboDoc* pDoc = GetDocument();
    ASSERT_VALID(pDoc);
    switch(m_FigureType){                               ⇐
    case 0:                                             ⇐
     pDC->Ellipse(200, 50, 400, 250);                   ⇐
     break;                                             ⇐
    case 1:                                             ⇐
     pDC->Rectangle(200, 50, 400, 250);                 ⇐
     break;                                             ⇐
    case 2:                                             ⇐
     pDC->RoundRect(200, 50, 400, 250, 20, 20);         ⇐
     break;                                             ⇐
    }                                                   ⇐
}


Figure 4.12  Using dockable toolbars with a combo box.

When you run the program, as shown in Figure 4.12, you can see that our combo box appears in the toolbar and we can select items in it. In fact, we can even detach the toolbar using the mouse and move it around. When you move the toolbar back to its original position, it docks—or repositions itself—there, rejoining the border of the window.

When the user selects a figure to draw and clicks the drawing tool, that figure appears in the window, as shown in Figure 4.12. Our combo program is a success so far—now we’re using combo boxes in dockable toolbars.

Visual C++ also supports Internet Explorer-style rebars, which we look at next.

Using Rebars

Rebars toolbars typically contain other toolbars in bands that you can slide and adjust using gripper handles, just like the ones you see in Internet Explorer. You can specify that your program use rebars in Step 4 of the AppWizard; if you do, the rebar control will be m_wndReBar. After you’ve added your controls to the toolbar as we’ve just done, you insert that toolbar into the rebar, place that toolbar into a REBARBANDINFO structure, and call the rebar’s InsertBand() function to insert the toolbar into a band in the rebar.

REBARBANDINFO rbi;
rbi.cbSize= sizeof(REBARBANDINFO);
rbi.fMask= RBBIM_BACKGROUND | RBBIM_CHILD | RBBIM_CHILDSIZE | RBBIM_STYLE | 
RBBIM_TEXT;
rbi.fStyle= RBBS_GRIPPERALWAYS;
rbi.cxMinChild= 300;
rbi.cyMinChild= 30;
rbi.lpText= “Band #1”;
rbi.cch= 7;
rbi.cx= 300;
rbi.hbmBack= (HBITMAP)m_bmap;
rbi.hwndChild= (HWND)m_wndToolBar;

m_wndReBar.InsertBand(-1, &rbi);

We continue by working with status bars.

Using Status Bars

The status bar is the bar that appears at the bottom of a windowed program, and it has several functions. One of the main functions of a status bar is to display the program’s status.


Figure 4.13  Status bar prompts and tooltips.

Setting a Status Bar’s Text

Here we set the status bar’s text to “OK.” To do that, we use the main frame window’s member object m_wndStatusBar. This object has a function, GetStatusBarCtrl(), that returns the status bar control that all status bars are built on. We can use that control’s SetText() function to set the text in the status bar like this:

    m_wndStatusBar.GetStatusBarCtrl().SetText(“OK”, 0, 0);

Although we are able to set the text in the status bar ourselves, the program sets the status text itself under certain circumstances, and we take a look at that next.

Adding Tooltips and Status Bar Prompts

When the user lets the mouse cursor rest over a toolbar button, a prompt explaining that button appears in the toolbar and a tooltip appears as a small yellow window next to the toolbar button. Let’s add a status bar prompt and a tooltip to our new toolbar button. We gave that button the ID ID_DRAW, so we use the String Editor to give that ID a new caption: “Draw a figure\nDraw a figure.” The first string becomes the status bar prompt, and the second (after the “\n”) string becomes the tooltip (these strings need not be the same). Now, we’re supporting status bar prompts and tooltips, as shown in Figure 4.13.

The last topic covered in this chapter is the use of status bar indicators. Status bar indicators are the small boxes you see at the right of the status bar that indicate such things as whether capslock or overstrike mode are on.


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.