Webocreation

Monday, November 23, 2009

StatusBar in visual basics

StatusBar

Status bars appear at the bottom of the application screen as a series of panes. These panes display information, usually in the form of text strings, although graphics can also be displayed.

To see a sample SDI application window with a status bar at the bottom, click this icon.





This section includes the following topics:

® Introduction to Status Bars
® Specifying Status Bars in AppWizard
® Adding a Pane to the Status Bar
® Displaying Status Bar States

Introduction to Status Bars

The default status bar displays the status of the CAPS LOCK key, the NUM LOCK key, and the SCROLL LOCK key. When the user clicks a menu item or toolbar button in an application, the status bar displays menu prompts that describe the basic functionality of the selected item.


Status bars are created from the CStatusBar class and take an array of IDs, one ID for each of the panes it contains. When you create an application in AppWizard and select Initial status bar in Step 4, AppWizard creates this array for your status bar. The array is placed in the source file for your MainFrame window class.

This sample code shows the array that AppWizard provides:

static UINT BASED_CODE indicators[] =
{ ID_SEPARATOR, // message line indicator
ID_INDICATOR_CAPS,
ID_INDICATOR_NUM,
ID_INDICATOR_SCRL,
};

The status bar panes are arranged and numbered horizontally along the status bar from left to right, starting at pane 0. You can add panes by adding IDs to the array. You can size the panes as needed and add separators by using ID_SEPARATOR elements.

After all the other panes are in place, the leftmost pane, at position 0, takes up all the remaining space on the status bar. This pane is most often used as a message area where the prompt strings of the various UI elements are displayed.

Like a toolbar, the status bar object is embedded in its parent frame window and is constructed automatically when the frame window is constructed. Since status bar panes are indicators of the state of an application, a call to the SetIndicators member function of class CStatusBar associates an ID from the array with each pane.

Specifying Status Bars in AppWizard

When you use AppWizard to build your application, you can specify an initial status bar. If you indicate that you want a status bar in your application, AppWizard does the following:

® Adds a protected CStatusBar data member to the MainFrame class header file, as shown in the following example:

// mainfrm.h : header file for the main frame class
class CMainFrame : public CMDIFrameWnd {
...
protected: // control bar embedded members
...
CStatusBar m_wndStatusBar;
CToolBar m_wndToolBar;
...
};

® Calls the CStatusBar::Create member function that creates the status bar and its windows. AppWizard inserts the following code:

// mainfrm.cpp : implementation file for the main frame class
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) {
...
if (!m_wndStatusBar.Create(this) ||
!m_wndStatusBar.SetIndicators(indicators,
sizeof(indicators)/sizeof(UINT)))
...
}

Adding a Pane to the Status Bar

If you want to provide additional information in the status bar, you can add panes and fill them with text or graphics.

Developer Studio does not contain a specialized editor for status bars; however, you can add a status bar pane by using the Menu editor to create a menu item on a dummy menu, and then writing code to manage the pane.

The following steps describe how to add a pane to the status bar. The rest of the topic explains the steps in more detail.

To add a pane to the status bar

1. Use the Menu editor to create a menu item on a dummy menu. You need the command ID of the menu item to access the status bar pane.
2. In the Prompt box on the Menu Item Properties property sheet, type in a string to initialize and size the pane. The pane size is based on the length of the string that you entered.
3. Modify the array to add the new command ID at the appropriate location. If you want the new pane to be the second pane from the left, it should appear as the second item in the array.
4. Write the code to manage the pane display.

By default, new status bar panes are enabled. If a pane is associated with a command ID, the pane displays the prompt string for that command ID.

Creating the Associated Command ID

Along with the command ID, it is important to supply an initial prompt string for this dummy menu item. The string serves two purposes: first, it is used by the application's framework to size the new pane when it is created; second, the string is used to initialize the pane and is displayed in the pane by the application on startup.

Modifying the Array

The panes of the status bar are represented by elements in an array, found in the file Mainfrm.cpp. Add the command ID associated with the new pane to this array. The position of the command ID in the array determines the order in which it will appear in the status bar. Note that the first element, ID_SEPARATOR, represents the default area, pane 0, where menu and toolbar button prompts are displayed.

Setting Pane Text

You can set text in a status bar pane in three ways:

® Use the member function CStatusBar::SetPaneText to write to the pane immediately.
® Write the information in an update-command handler associated with the pane.
® Call CWnd::SetWindowText to update the text in pane 0 only.

Using CStatusBar::SetPaneText

The leftmost pane in the default status bar displays the menu and toolbar button prompts. Panes are consecutively numbered from the left, beginning with pane 0. You can write text to pane 0 with the functions CFrameWnd::SetMessageText or CWnd::SetWindowText. You can write to any pane in the status bar with the function CStatusBar::SetPaneText, as shown:

BOOL CStatusBar::SetPaneText(int nIndex, LPCTSTR
lpszNewText, BOOL bUpdate = TRUE)
SetPaneText returns a nonzero value if successful; otherwise, it returns zero. SetPaneText sets the pane text to the string pointed to by lpszNewText. An index can be dynamically determined with the function CStatusBar::CommandToIndex.

Creating an Update Command Handler

If the text that you display is better calculated during idle time, you can create an update command handler for the pane's associated command ID, and then write to the pane by using CCmdUI::SetText, as shown:

// in a command UI handler
// to activate the status bar pane
pCmdUI->Enable(TRUE);
// to modify text in status bar
pCmdUI->SetText(HelloPhrase);


Calling CWnd::SetWindowText
The third method for setting pane text can be used only when writing to pane 0. The pointer variable in the function call points to the location of the string to be displayed.

void CWnd::SetWindowText ( LPCTSTR lpszString );

Displaying Status Bar States

Status bars can maintain various states. The state of a status bar can change while the status bar is visible. For example, when a button is clicked, it appears as if it is pressed; if a toolbar button is unavailable, it appears dimmed.

Status Bar States

The defining borders of a status bar pane can be set or removed. In addition, text in a status bar pane can be hidden or visible, or changed dynamically.

Using Command Handlers to Change Status Bar States

Command handlers for status bar panes are called whenever the system reaches an idle state. (For more information, search for CWinThread::OnIdle in Visual C++ Help.) The handlers allow the visible states of the panes to change dynamically.

The following table lists familiar CCmdUI member functions available in a command handler. These handlers affect the visual state of status bars rather than menu items.

Function Description

Enable Enables or disables the user interface. For status bars, Enable hides or shows text in a status bar pane.


SetCheck Sets the border of a status bar pane.
SetRadio Same as SetCheck.
SetText In a status bar pane, SetText changes the text that is displayed.


Sample Aplications

Here are short descriptions of the sample applications related to this chapter. These sample applications are located in the folder \Samples\Ch08.


Sample application folder
Description of application

\Menus1 A simple SDI application with menu handlers. Draws text in the middle of the client area.

\Menus2 Same as Menus1, with command update handlers added.
\Menus3 Same as Menus2, except that four command handlers point to one function. Shows that multiple message map entries can point to the same function.
\StatBar Similar to Menus1. Shows how to add a pane to the default status bar and write text into the new pane by using a dummy menu item.
\CustSBar Similar to Menus1. Shows how to replace the default status bar with a custom status bar. This custom status bar duplicates the functionality of the default status bar (CAPS state, status line) and has graphics drawn in one of its panes.
\Tool Similar to Menus1. Shows how to add icons to the default toolbar that duplicate the functionality of menu items.

Self-Check Questions

1. Typically, how does an MFC application fill its menu bar with top-level menus?

a A. You must create all top level menus by using the Menu editor.
a B. There is no resource for the menu bar; it is created dynamically by the application’s framework.
a C. ClassWizard is used to add a CMenu object for each top-level menu.
aa D. AppWizard supplies the project with default menu resources.

2. How do you create an accelerator key resource and associate it to an existing menu item (for example, \tCtrl+C for the Copy command)?
a A. In the Menu editor, in the Properties dialog box, append the menu item caption with the appropriate string (\tCtrl+C).
a B. In the string table, locate the associated command, and edit the caption property to append the accelerator string (\tCtrl+C).
a C. In the Accelerator editor, create a new accelerator for the corresponding key sequence, and then associate it to the appropriate existing command ID.
aa D. You must hand-edit the resource file because Developer Studio does not contain an editor for shortcut keys.

3. Which one of following statements is true about toolbars and toolbar buttons?

a A. A toolbar is a window (a Windows 95 common control) that contains a single bitmap.
a B. If you do not choose AppWizard support for toolbars, it is not possible to later add them to your application.
a C. A toolbar button must be associated with an existing, displayable menu item.
a D. Each toolbar button must always have an associated ToolTip string.

4. Which one of following statements is true about status bars and their panes?

a A. They can be easily managed with the status bar editor in Developer Studio.
a B. Indicator pane number 0, which is used to display prompt strings, is always sized to fit the string "ID_SEPARATOR" within it.
a C. The status bar is owned by the application’s view object.
a D. Text can be written to any pane by using either CCmdUI::SetText in an update command handler, or

CStatusBar::SetPaneText in any function.

5. When are ON_UPDATE_COMMAND_UI handlers called?
a A. In the OnUpdateCmd handler.
a B. During idle processing.
a C. During command routing only.
aa D. During general message routing.

No comments:

Post a Comment