There are two different types of elements in a status bar: Indicators and Separators.
Indicators are small elements that usually display a word or a short
phrase. Examples of common indicators are ones for Cap Lock
,
Num Lock
, and Insert
. Most indicators are enabled
when a particular key has been pressed. Open a document in Wordpad
and press the Caps Lock
key: the CAP
indicator
will appear in the status bar.
Separators are larger elements that display a longer phrase. The default status bar has a pane that displays a short phrase explaining the menu option or toolbar icon that is currently below the cursor.
Usually the Indicators have static content and the Separators have dynamic content. However, it is possible to make an Indicator dynamic, it just requires a little more coding. The main difference bewteen an Indicator and a Separator is that the Indicator has a default value corresponding to the string in the string table and has an automatic Update Command interface. A Separator does not have a default value and the programmer must take charge of updating the Separator.
The status bar is defined by the indicator array. This is a global array
that is defined in the CMainFrame
class. The array can contain
any number of Indicators and Separators. To add an indicator
to the array, add the Indicator's ID from the String Table.
All Separators have an ID of ID_SEPARATOR
. Everything
else should be the ID of an Indicator. This example has two
Separators and one Indicator. The first Separator will expand
to fill the status bar. The Indicator will be large enough to display
the entire string from the String Table. All other Separtors
will have a default width of 25% of the status bar.
static UINT indicators[] = { ID_SEPARATOR, // status line indicator ID_INDICATOR_LEFT, ID_SEPARATOR };
Add a new string to the String Table.
ID_INDICATOR_
).
Determine which class will map the Update Command handler for the indicator.
CCmdUI*
parameter. This should be placed before the
DECLARE_MESSAGE_MAP
macro.
afx_msg void OnUpdateLeft(CCmdUI* pCCmdUI);
BEGIN_MESSAGE_MAP
and END_MESSAGE_MAP
macros.
ON_UPDATE_COMMAND_UI(ID_INDICATOR_LEFT, OnUpdateLeft)
void CStatusView::OnUpdateLeft(CCmdUI* pCmdUI) { pCmdUI->Enable(::GetKeyState(VK_LBUTTON) < 0); }
The width of the indicator in the status bar will be set to the width of the string in the string table. When the UI is enabled, the string from the string table will appear in the status bar. When the UI is disabled, the indicator will be empty.
There can be more than one Separator. To create a Separator,
just add an ID_SEPARATOR
to the indicator array.
Determine when the Separator should be updated.
m_wndStatusBar
member variable of the CMainFrame
class has public
access. Be sure to include the MainFrm.h
header in the class
that will be accessing the status bar.
CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd;
SetPaneText
method with the index and the new value
pFrame->m_wndStatusBar.SetPaneText(2,"Left Button Down");
This example sets the text in OnLButtonDown
and clears it in
OnLButtonUp
. This is being handled by the view, so the .cpp
of the view should
#include "MainFrm.h"
void CStatusView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd; pFrame->m_wndStatusBar.SetPaneText(2,"Left Button Down"); } void CStatusView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CMainFrame* pFrame = (CMainFrame*) AfxGetApp()->m_pMainWnd; pFrame->m_wndStatusBar.SetPaneText(2,""); }
Indicators have a default width that is the width of its default string.
Separators either expand to fill the status bar, or have a default width
of 25% of the status bar. These can be changed with a call to
SetPaneInfo
. The info is referenced by the index in the indicator
array. The info that can be changed is
SBPS_NOBORDERS
No 3-D border around the pane.
SBPS_POPOUT
Reverse border so that text "pops out."
SBPS_DISABLED
Do not draw text.
SBPS_STRETCH
Stretch pane to fill unused space. Only
one pane per status bar can have this style.
SBPS_NORMAL
No stretch, borders, or pop-out.
ID_SEPARATOR
(Or 0). For
Indicators, use the ID from the String Table.
For example, this call would set the first pane to a width of 200 pixels, and it would not stretch to fill the status bar.
m_wndStatusBar.SetPaneInfo(0, ID_SEPARATOR, SBPS_NORMAL, 200);