Working with Status Bars
Status bars are mostly benign objects that sit at the bottom of your application'swindow, doing whatever MFC instructs them to do. This consists of displaying command
descriptions and the status of various keys on the keyboard, including the Caps Lock and
Scroll Lock keys. In fact, status bars are so mundane from the programmer's point of
view (at least they are in an AppWizard application) that they aren't even represented
by a resource that you can edit like a toolbar. When you tell AppWizard to incorporate
a status bar into your application, there is not much left for you to do.
Or is there? A status bar, just like a toolbar, must reflect the interface needs of your
specific application. For that reason, the CStatusBar class features a set of methods
with which you can customize the status bar's appearance and operation. Table 9.2 lists
the methods along with brief descriptions.
Table 9.2 Methods of the CStatusBar Class
Method Description
CommandToIndex() Obtains an indicator's index, given its ID
Create() Creates the status bar
GetItemID() Obtains an indicator's ID, given its index
GetItemRect() Obtains an item's display rectangle, given its index
GetPaneInfo() Obtains information about an indicator
GetPaneStyle() Obtains an indicator's style
GetPaneText() Obtains an indicator's text
GetStatusBarCtrl() Obtains a reference to the CStatusBarCtrl object
represented by the CStatusBar object
SetIndicators() Sets the indicators' IDs
SetPaneInfo() Sets the indicators' IDs, widths, and styles
SetPaneStyle() Sets an indicator's style
SetPaneText() Sets an indicator's text
When you create a status bar as part of an AppWizard application, you see a window
similar to that shown in Figure 9.10. (To make your own, create a project called Status
and accept all the defaults, as you did for the Tool application.) The status bar has
several parts, called panes, that display certain information about the status of the
application and the system. These panes, which are marked in Figure 9.10, include
indicators for the Caps Lock, Num Lock, and Scroll Lock keys, as well as a message area
for showing status text and command descriptions. To see a command description, place
your mouse pointer over a button on the toolbar (see Figure 9.11).
The most common way to customize a status bar is to add new panes. To add a pane to a
status bar, complete these steps:
1. Create a command ID for the new pane.
2. Create a default string for the pane.
3. Add the pane's command ID to the status bar's indicators array.
4. Create a command-update handler for the pane.
FIG. 9.10 The default MFC status bar contains a number of informative panes.
The following sections cover these steps in detail.
FIG. 9.11 The message area is mainly used for command descriptions.
Creating a New Command ID
This step is easy, thanks to Visual C++'s symbol browser. To add the command ID, start by
choosing View, Resource Symbols. When you do, you see the Resource Symbols dialog box
(see Figure 9.12), which displays the currently defined symbols for your application's
resources. Click the New button, and the New Symbol dialog box appears. Type the new
ID, ID_MYNEWPANE, into the Name box (see Figure 9.13). Usually, you can accept the
value that MFC suggests for the ID.
FIG. 9.12 Use the Resource Symbols dialog box to add new command IDs to your application.
FIG. 9.13 Type the new ID's name and value into the New Symbol dialog box.
Click the OK and Close buttons to finalize your selections, and your new command ID is
defined.
Creating the Default String
You have now defined a resource ID, but it isn't being used. To represent a status bar
pane, the ID must have a default string defined for it. To define the string, first go to
the ResourceView window (by clicking the ResourceView tab in the workspace pane) and
double-click the String Table resource to open it in the string table editor, as shown in
Figure 9.14.
Now, choose Insert, New String to open the String Properties dialog box. Type the new
pane's command ID ID_MYNEWPANE into the ID box (or choose it from the drop-down
list) and the default string (Default string in this case) into the Caption box (see
Figure 9.15).
Adding the ID to the Indicators Array
When MFC constructs your status bar, it uses an array of IDs to determine which panes
to display and where to display them. This array of IDs is passed as an argument to the
status bar's SetIndicators() member function, which is called in the CMainFrame class's
OnCreate() function. You find this array of IDs, shown in Listing 9.3, near the top of the
MainFrm.cpp file. One way to reach these lines in the source code editor is to switch to
ClassView, expand CMainFrame, double-click OnCreate(), and scroll up one page.
Alternatively, you could use FileView to open MainFrm.cpp and scroll down to this
code.
FIG. 9.14 Define the new pane's default string in the string table.
FIG. 9.15 Use the String Properties dialog box to define the new pane's default string.
Listing 9.3 MainFrm.cpp - The Indicator Array
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
To add your new pane to the array, type the pane's ID into the array at the position in
which you want it to appear in the status bar, followed by a comma. (The first pane,
ID_SEPARATOR, should always remain in the first position.) Listing 9.4 shows the
indicator array with the new pane added.
Listing 9.4 MainFrm.cpp - The Expanded Indicator Array
static UINT indicators[] =
{
ID_SEPARATOR, // status line indicator
ID_MYNEWPANE,
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};
No comments:
Post a Comment