| Tenouk C & C++ | MFC Home | Toolbars, Status Bars and ReBar 2 | Reusable Frame Window Base Class 1 | Download | Site Index |


 

 

 

 

 

Module 8b:

Toolbars, Status Bars and ReBar 3

 

 

 

 

 

This is a continuation from the previous module... Program examples compiled using Visual C++ 6.0 (MFC 6.0) compiler on Windows XP Pro machine with Service Pack 2. Topics and sub topics for this Tutorial are listed below:

  1. The Internet Explorer Rebar Toolbar

  2. Anatomy of a Rebar

  3. The MYMFC13 Rebar Example

 

 

 

The Internet Explorer Rebar Toolbar

 

Visual C++ 6.0 contains many features that are part of Internet Explorer 4.0 and above: the Internet Explorer Common Controls. One of the controls in the IE Common Controls is a rebar. You're probably already familiar with the rebar if you have ever used Internet Explorer. The rebar differs from the default MFC toolbar in that it provides grippers and allows the user to "slide" its horizontal and vertical positions, whereas the MFC toolbar's position is changed via drag-and-drop docking. Rebars also allow the developer to provide many more internal control types, such as drop-down menus, than are available in CToolBar.

 

Anatomy of a Rebar

 

Figure 24 shows the various terminologies used on a rebar. Each internal toolbar in a rebar is called a band. The raised edge where the user slides the band is called a gripper. Each band can also have a label.

 

Rebar terminology

 

Figure 24: Rebar terminology.

 

MFC provides two classes that facilitate working with rebars:

 

  • CReBar: A high-level abstraction class that provides members for adding CToolBar and CDialogBar classes to rebars as bands. CReBar also handles communication (such as message notifications) between the underlying control and the MFC framework.

  • CReBarCtrl: A low-level wrapper class that wraps the IE ReBar control. This class provides numerous members for creating and manipulating rebars but does not provide the niceties that are found in CReBar.

 

Most MFC applications use CReBar and call the member function GetReBarCtrl(), which returns a CReBarCtrl pointer to gain access to the lower-level control if needed.

 

The MYMFC13 Rebar Project Example

 

Let's get familiar with the rebar by jumping into an example. This example creates an SDI application that has a rebar with two bands: a familiar toolbar band and a dialog bar band. Figure 25 shows the example in action.

 

MYMFC13 rebar example.

 

Figure 25: MYMFC13 rebar example.

 

Here are the steps required to create the MYMFC13 example:

 

Run AppWizard to generate mfcproject\mymfc13. Select Single Document. In Step 4, be sure you select Internet Explorer ReBars under the How Do You Want Your Toolbars To Look option. Figure 26 below shows the correct settings. Accept all other default settings.

 

AppWizard Step 4 settings for the rebar control.

 

Figure 26: AppWizard Step 4 settings for the rebar control.

 

MYMFC13 project summary.

 

Figure 27: MYMFC13 project summary.

 

Compile and run the application. When you run the application, you will see that AppWizard has automatically created a rebar with two bands. One band contains a conventional toolbar and the other contains the text "TODO: layout dialog bar" in the band. Figure 28 below shows the initial rebar control.

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

--------------------------------------------------------------------------------

Initial windows for MYMFC13 example with the default rebar controls.

 

Figure 28: Initial windows for MYMFC13 example with the default rebar controls.

 

At this point, you can open the MainFrm.h header file and see the code below, which declares the CReBar data member m_ndReBar.

 

protected:  // control bar embedded members

    CStatusBar  m_wndStatusBar;

    CToolBar    m_wndToolBar;

    CReBar      m_wndReBar;

    CDialogBar  m_wndDlgBar;

 

Visual C++ code segment

 

Listing 21.

 

In the MainFrm.cpp file, you can see the code that adds the toolbar and the dialog bar to the CReBar object:

    if (!m_wndReBar.Create(this) || !m_wndReBar.AddBar(&m_wndToolBar) || !m_wndReBar.AddBar(&m_wndDlgBar))

    {

        TRACE0("Failed to create rebar\n");

        return -1;      // fail to create

    }

 

Visual C++ code segment

 

Listing 22.

 

Lay out the Dialog Bar. Open the Visual C++ resource editor. Under the Dialog heading you'll find a dialog resource for the dialog bar with the ID IDR_MAINFRAME. Open IDR_MAINFRAME and you'll see the dialog bar with the text "TODO: layout dialog bar" as shown below.

 

Dialog bar ready to be edited.

 

Figure 29: Dialog bar ready to be edited.

 

Let's follow AppWizard's friendly suggestion and put some real controls into the dialog bar. First delete the static control with the "TODO" text in it. Next place a combo box in the dialog bar and enter some default data items: one, two, buckle, my, shoe!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Adding and modifying the combo box in the dialog bar properties.

 

Figure 30: Adding and modifying the combo box in the dialog bar properties.

 

Now place a button on the dialog bar and change the button's text to Increment. Next place a progress bar with the default properties on the dialog bar. Finally place another button with the text Decrement on the dialog bar. When you are done laying out the dialog bar, it should look similar to Figure 32.

 

Adding and modifying progress bar control properties.

 

Figure 31: Adding and modifying progress bar control properties.

 

Adding and modifying button control properties.

 

Figure 32: Adding and modifying button control properties.

 

Associate the dialog bar with the CMainFrame class. Before we can program the handlers for the Increment and Decrement buttons, we need to attach the dialog bar to a class using ClassWizard. While in the resource editor, bring up ClassWizard by double-clicking on the Increment button. You will now see this dialog.

 

New class dialog prompt for IDR_NAINFRAME.

 

Figure 34: New class dialog prompt for IDR_NAINFRAME.

 

Choose Select An Existing Class. We choose this option because we want our dialog resource to be a band in the toolbar, not a separate dialog class. Click OK and you will see these choices.

 

Associating the dialog bar with the CMainFrame class.

 

Figure 35: Associating the dialog bar with the CMainFrame class.

 

 

Choose CMainFrame from the list and click Select. ClassWizard will prompt you with one last dialog as shown below.

 

Associating CMainFrame class to the Dialog class prompt.

 

Figure 36: Associating CMainFrame class to the Dialog class prompt.

 

Click Yes and then exit ClassWizard. You have successfully associated the IDR_MAINFRAME dialog bar with the CMainFrame class.

Program the dialog bar. To program the dialog bar, bring up the IDR_MAINFRAME dialog resource in the resource editor again and double-click on the Increment button. ClassWizard will automatically create an ONBUTTON1 handler for you; accept the default name for this function and click the Edit Code button.

 

Creating the ONBUTTON1 handler for the Decrement button.

 

Figure 37: Creating the ONBUTTON1 handler for the Decrement button.

 

Enter the following code in the OnButton1() function:

 

void CMainFrame::OnButton1()

{

 CProgressCtrl * pProgress = (CProgressCtrl*)m_wndDlgBar.GetDlgItem(IDC_PROGRESS1);

 pProgress->StepIt();

}

 

Visual C++ code segment

 

Listing 23.

 

The OnButton1() handler first gets a pointer to the progress control and then calls StepIt() to increment the progress control.

Now we need to add similar code to the decrement handler. Double-click on the Decrement button in the resource editor and ClassWizard will automatically create an OnButton2() handler.

 

Adding the OnButton2() handler for the Increment button.

 

Figure 38: Adding the OnButton2() handler for the Increment button.

 

Add the following code to the OnButton2() member function:

 

void CMainFrame::OnButton2()

{

 CProgressCtrl * pProgress = (CProgressCtrl*)m_wndDlgBar.GetDlgItem(IDC_PROGRESS1);

 int nCurrentPos = pProgress->GetPos();

 pProgress->SetPos(nCurrentPos-10);

}

 

Visual C++ code segment

 

Listing 24.

 

Compile and test. Now you can compile and run MYMFC13 to see your custom rebar in action. The Increment button increases the progress bar and the Decrement button decreases it.

 

MYMFC13 program output in action, with rebar items.

 

Figure 39: MYMFC13 program output in action, with rebar items.

 

In this Module, we learned how to use MFC's toolbar, status bar, and the new rebar control. In the next Module, we'll look at how to extend MFC to implement a frame window that remembers its position.

 

 

 

 

Further reading and digging:

  1. MSDN MFC 7.0 class library online documentation.

  2. MSDN MFC 9.0 class library online documentation - latest version.

  3. Porting & Migrating your older programs.

  4. MSDN Library

  5. DCOM at MSDN.

  6. COM+ at MSDN.

  7. COM at MSDN.

  8. Windows data type.

  9. Win32 programming Tutorial.

  10. The best of C/C++, MFC, Windows and other related books.

  11. Unicode and Multibyte character set: Story and program examples.

 

 

 


 

| Tenouk C & C++ | MFC Home | Toolbars, Status Bars and ReBar 2 | Reusable Frame Window Base Class 1 | Download | Site Index |