Title: CSE 791 Advanced Windows Programming
1CSE 791 Advanced Windows Programming
- Tool Bars, Status Bars and Rebars
- by
- Pranav Sharma and Pramod Chandewar
2Tool Bars
- Creating and Initializing a Toolbar
- Docking and Floating
- Controlling a Toolbars Visibility
- Keeping Toolbar Buttons in Sync with Your
Application - Adding ToolTips and Flyby Text
- Making Toolbar Settings Persistent
- Toolbar Support in AppWizard
3Toolbars
- A toolbars purpose is to provide one-click
access to commonly used commands. - MFCs CtoolBar class implements Toolbars.
- CToolBar derives much of its functionality from
the toolbar control in Comctl32.dll. - A separate and more primitive MFC class named
CToolBarCtrl provides an MFC interface to toolbar
controls.
4Creating and Initializing a Toolbar
- Creation
- Construct a CToolBar object and call
CToolBarCreate - as follows -
- m_wndToolBar.Create(this,WS_CHILD WS_VISIBLE
CBRS_BOTTOM) - Where
- m_wndToolBar is a data member of CToolBar.
- and Create is called from the frame windows
OnCreate handler.
5Creating and Initializing a Toolbar ( contd. )
- Initialization
- A freshly created toolbar is empty, so we need to
initialize it. - For initializing a toolbar we need
- a bitmap resource containing images for the faces
of toolbar buttons - an array of button IDs.
6Creating and Initializing a Toolbar ( contd. )
- 2 ways to initialize a toolbar.
- Method 1
- // In the RC file
- IDR_TOOLBAR BITMAP Toolbar.bmp
- //In the CPP file
- static UINT nButtonIDs ID_FILE_NEW,
ID_FILE_OPEN, ID_FILE_SAVE, ID_SEPARATOR,
ID_EDIT_CUT, ID_EDIT_COPY, ID_EDIT_PASTE,
ID_EDIT_UNDO, ID_SEPARATOR, ID_FILE_PRINT - m_wndToolBar.Create (this)
- m_wndToolBar.LoadBitmap (IDR_TOOLBAR)
- m_wndToolBar.SetButtons (nButtonIDs, 10)
7Creating and Initializing a Toolbar ( contd. )
- Method 2 (used by AppWizard)
- //In the RC file
- IDR_TOOLBAR BITMAP Toolbar.bmp
- IDR_TOOLBAR TOOLBAR 16, 15
- BEGIN
- BUTTON ID_FILE_NEW
- BUTTON ID_FILE_OPEN
- BUTTON ID_FILE_SAVE SEPARATOR
- BUTTON ID_EDIT_CUT BUTTON ID_EDIT_COPY
- BUTTON ID_EDIT_PASTE
- BUTTON ID_EDIT_UNDO SEPARATOR
- BUTTON ID_FILE_PRINT
- END
- // In the CPP file
- m_wndToolBar.Create (this)
- m_wndToolBar.LoadToolBar (IDR_TOOLBAR)
8Creating and Initializing a Toolbar ( contd. )
- Adding text strings to the faces of buttons
- You can add text strings to the faces of buttons
with CToolBarSetButtonText as follows - // In the CPP file
- m_wndToolBar.Create (this)
- m_wndToolBar.LoadToolBar (IDR_TOOLBAR)
m_wndToolBar.SetButtonText (0, _T ("New"))
m_wndToolBar.SetButtonText (1, _T ("Open"))
m_wndToolBar.SetButtonText (2, _T ("Save"))
m_wndToolBar.SetButtonText (4, _T ("Cut"))
m_wndToolBar.SetButtonText (5, _T ("Copy"))
m_wndToolBar.SetButtonText (6, _T ("Paste"))
m_wndToolBar.SetButtonText (7, _T ("Undo"))
m_wndToolBar.SetButtonText (9, _T ("Print")) - m_wndToolBar.SetSizes (CSize (48, 42), CSize
(40, 19))
9Creating and Initializing a Toolbar ( contd. )
- Creating check push buttons and radio push
buttons - IDR_TOOLBAR BITMAP Toolbar.bmp
- IDR_TOOLBAR TOOLBAR 16, 15
- BEGIN
- BUTTON ID_CHAR_BOLD
- ..
- // In the CPP file
- m_wndToolBar.Create (this)
m_wndToolBar.LoadToolBar (IDR_TOOLBAR)
m_wndToolBar.SetButtonStyle (0, TBBS_CHECKBOX)
m_wndToolBar.SetButtonStyle (1, TBBS_CHECKBOX)
m_wndToolBar.SetButtonStyle (2, TBBS_CHECKBOX)
m_wndToolBar.SetButtonStyle (4, TBBS_CHECKGROUP)
m_wndToolBar.SetButtonStyle (5, TBBS_CHECKGROUP)
m_wndToolBar.SetButtonStyle (6, TBBS_CHECKGROUP)
- int nStatem_wndToolBar.GetToolBarCtrl().GetState
(ID_PARA_LEFT) - m_wndToolBar.GetToolBarCtrl().SetState(ID_PARA_LE
FT,nStateTBSTATE_CHECKED)
10Docking and Floating
- By default, a toolbar is affixed to the side of
its frame window and cant be detached. - CToolBar provides the ability for the user to
grab a toolbar with a mouse, detach it from its
frame window, and either dock it to another side
or allow it to float free in a mini frame window
of its own. - Floating and Docking are enabled by calling
toolbars EnableDocking function and Frame
windows EnableDocking function. - Each of the functions parameters are bit flags
which specify which sides of the frame window the
toolbar will allow itself to be docked or which
of the sides are valid docking targets. - Both functions would be required when there are
more than one toolbars with different docking
requirements. - Toolbars are docked and undocked with the
CFrameaWndDockControlBar and FloatControlBar
functions.
11Docking and Floating (Contd.)
- Bit Flag Descriptions
- Bit Flag
Description - CBRS_ALIGN_LEFT Permit docking to the left
side of the frame
window - CBRS_ALIGN_RIGHT Permit docking to the right
side of the frame window - CBRS_ALIGN_TOP Permit docking to the top
of the frame window - CBRS_ALIGN_BOTTOM Permit docking to the bottom of
the frame window - CBRS_ALIGN_ANY Permit docking to any side
of the frame window
12Controlling a Toolbars Visibility
- CFrameWndOnBarCheck used to toggle a toolbar on
or off. - CFrameWndOnUpdateControlBarMenu updates the
menu containing the command that toggles a
toolbar on or off. - If your application uses a toolbar whose ID isnt
the default, we need to connect the toolbar to
command and update handlers that control its
visibility in 2 ways - Assign the toolbar and the menu item the same ID
and - update message-map entries as follows
- ON_UPDATE_COMMAND_UI(ID_VIEW_TOOLBAR2,
OnUpdateControlBarMenu) - ON_COMMAND_EX(ID_VIEW_TOOLBAR2, OnBarCheck)
13Controlling a Toolbars Visibility (contd.)
- b) Provide your own command and update handlers
and use CFrameWndShowControlBar as follows - //In CMainFrame message map
- ON_COMMAND (ID_VIEw_TOOLBAR2, OnViewToolbar2)
- ON_UPDATE_COMMAND_UI (ID_VIEW_TOOLBAR2,
OnUpdateViewToolbar2UI) - .
- .
- Void CMainFrameOnViewToolbar2()
-
- ShowControlBar(m_wndToolBar2,(m_wndToolBar2.GetSt
yle()WS_VISIBLE)0,FALSE) -
- Void CMainFrameOnUpdateViewToolbar2UI(CCmdUI
pCmdUI) -
- pCmdUI-gtSetCheck((m_wndToolBar2.GetStyle()
WS_VISIBLE)?10) -
14Keeping Toolbar Buttons in Sync with Your
Application
- Toolbar buttons are connected to command handlers
in the same way menu items are connected
through message maps. - Toolbar buttons can be assigned update handlers
just as menu items. - CCmdUI functions that update menu items are
equally capable of updating toolbar buttons. - We can use one update handler for updating menu
items and toolbar buttons as long as both objects
share the same ID. - For example to update Paste menu item each time
Edit menu is displayed - ON_UPDATE_COMMAN_UI(ID_EDIT_PASTE,
OnUpdateEditPasteUI) - Void CMyClassOnUpdateEditPasteUI(CCmdUI
pCmdUI) -
- pCmdUI-gtEnable(IsClipboardFormatAvailable(CF_Te
xt)) -
- The only difference between menu item updates and
toolbar updates is the timing of calls to the
update handler. - Lastly, we can use update handlers to create
check push buttons and radio push buttons without
changing button styles.
15Adding ToolTips and FlybyText
- 2 ways one way is to use VC Resource Editor.
- Another way
- //In the RC file
- IDR_TOOLBAR BITMAP Toolbar.bmp
- IDR_TOOLBAR TOOLBAR 16,15
- BEGIN
- BUTTON ID_CHAR_BOLD
- BUTTON ID_CHAR_ITALIC
- BUTTON ID_CHAR_UNDERLINE
- SEPARATOR
- BUTTON ID_PARA_LEFT
- BUTTON ID_PARA_CENTER
- BUTTON ID_PARA_RIGHT
- END
16Adding ToolTips and FlybyText (Contd.)
- STRINGTABLE
- BEGIN
- ID_CHAR_BOLD Toggle boldface on or off\nBold
- ID_CHAR_ITALIC Toggle italic on or off\nItalic
- ID_CHAR_UNDERLINE Toggle underline on or
off\nUnderline - ID_PARA_LEFT Align text flush left\nAlign Left
- ID_PARA_CENTER Center Text between
margins\nAlign Center - ID_PARA_RIGHT Align text flush right\nAlign
Right - END
- //In the CPP file
- m_wndToolBar.Create(this,WS_CHILD WS_VISIBLE
CBRS_TOP CBRS_TOOLTIPS CBRS_FLYBY) - m_wndToolBar.LoadToolBar(IDR_TOOLBAR)
17Making Toolbar Settings Persistent
- 2 functions used CFrameWndSaveBarState and
CFrameWndLoadBarState - CFrameWndSaveBarState writes information about
each toolbars state to the registry or a private
INI file. (also stores state of StatusBar) - Normally LoadBarState is called from the main
frame windows OnCreate handler after toolbars
and status bars are created. - SaveBarState is called from the frame windows
OnClose handler. - Never call SaveBarState from the frame windows
OnDestroy handler if you want to preserve the
states of floating toolbars because - A floating toolbar no longer exists when the
frame windows OnDestroy function is called.
18Toolbar Support in AppWizard
- App Wizards toolbar-creation code uses
CToolBarCreateEx rather than CToolBarCreate
and passes CBRS_GRIPPER and TBSTYLE_FLAT flags.
19The Status Bar
- The status bar is a window that displays
context-sensitive help for menu items and tool
bar buttons - It neither accepts user input nor generates
command messages. - Displays text panes under program control.
- Adding toolbars and status bars to MFC
applications is easy because of CToolBar and
CStatusBar provide thorough encapsulations of
user interface (UI) elements.
20The Status Bar Definition
- Static UINT indicators
-
- ID_SEPARATOR,
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
-
- Status bar with Caps lock, Num lock , and Scroll
Lock indicators
21Status Bar
- To use the status bar for application-specific
data ,first disable the standard status bar that
displays the menu prompt and keyboard status. - A status bar can be divided into one or more
areas that are variously referred to as panes,
panels or indicators. - The status bar supports two types of panes-
- message line panes and
- status indicator panes.
22The Message Line pane
- A Message line pane displays a string that the
program supplies dynamically. - To set value of the message line,first get access
to to the status object and then you must call
the CStatusBarSetPaneText member function with
a zero-based index parameter. - Pane 0 is the leftmost pane ,1 is the next pane
to the right, and so forth. - CMainFrame pFrame (CMainFrame)
AfxGetApp()-gtm_pMainWnd - CStatusBar pStatus pFrame-gtm_wndStatusBar
- pStatus-gtSetPaneText(0, message line for
first pane) - Its minimum length is one forth the display
width, and it expands if room is available in the
status bar.
23The Status Indicator
- A status indicator pane is linkedto a single
resource-supplied string that is displayed or
hidden by logic in an associated update command
UI message handler function. - The length of a status indicator pane is exact
length of the corresponding resource string.
24Creating and Initializing a Status Bar
- In MFC, a status bar is an instance of
CStatusBar.The frame windows OnCreate handler
creates the status bar with a statement like this
one - m_wndStatusBar.Create(this)
- Passing a this pointer makes a status bar a child
of the frame window. - A status bar created in this way is destroyed
automatically when its parent is destroyed
25Creating and Initializing a Status Bar
- After creating ,a status bar is initialized by
calling CStatusBarSetIndicators - Set Indicators specifies the number of panes the
status bar will contain and optionally assigns
string resources to individual panes - The statements to create a simple status bar
containing just one pane . - UINT nIndicator ID_SEPARATOR
- m_wndStatusBar.Create (this)
- m_wndStatusBar.SetIndicators (nIndicator,1)
26Creating and Initializing a Status Bar
- MFC defines four special indicator IDs for status
bar panes that display keyboard status and maps
them to a common handler in the CFrameWnd class - ID_INDICATOR_CAPS, which corresponds to the caps
lock key - ID_INDICATOR_NUM, which corresponds to the Num
Lock Key - ID_INDICATOR_SCRL, which corresponds to the
Scroll Lock Key - ID_INDICATOR_KANA, which corresponds to the Kana
Key on Japanese Keyboards
27Example Status bar
- static UINT nIndicators
- ID_SEPARATOR,
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL
- m_wndStatusBar.Create (this)
- m_wndStatusBar.SetIndicators (nIndicators, 4)
-
- The blank pane indicates that Scroll Lock is
inactive. - CstatusBar automatically positions all panes
after the first at the far right end of the
status bar and stretches the leftmost pane to
fill the remaining space. - Panes other than the first are also drawn
indented so that theyre visible even when
theyre blank.
28Providing Context-Sensitive Help for Menu Items
- When the user highlights a menu item, the
framework checks to see whether the applications
EXE file contains a string resource whose ID
equals the menu item ID.If the search turns up a
match ,the string resource is loaded and
displayed in the status bar pane. - The frame work provides default help strings for
ID_FILE_NEW,ID_FILE_OPEN, and other common
commands Ids.It also provides default help
strings for commands found in the system menu. - Include the header file Afxres.h in your
applications RC file and the frameworks
predefined string resources will be included. - If you use AppWizard to create the application,
Afxres.h is included for you.
29Providing Context-Sensitive Help for Menu Items
- You can override the help text for predefined
menu item IDs by defining your own string
resources with identical ID values - AFX_IDS_IDLEMESSAGE "Ready
- The frame work will display the word Ready in
the status bar when no menu is pulled down or no
item is selected.
30Creating Custom Status Bar Panes
- For complex status bars such as
- Microsoft PowerPoint
- Microsoft Word
- For starters ,you can add panes to a status bar
and size them any way you want using CStatusBars
SetPaneInfo function. - SetPaneInfo accepts four parametersthe 0-based
index of the pane whose attributes you want to
modify and the panes ID ,style, and width,in
that order.
31Creating Custom Status Bar Panes
- Style Description
- SBPS_NOBORDERS Draws the pane flush with the
surface of the status bar. - SBPS_POPOUT Draws the pane so that it
protrudes from the status bar. - SBPS_NORMAL Draws the pane so that it is
indented into the status bar. - SBPS_DISABLED Disables the pane. Disabled
panes don't display text. - SBPS_STRETCH Stretches the pane to fill
unused space when the status
bar is resized. - SBPS_OWNERDRAW Creates an owner-draw pane.
32Creating Custom Status Bar Panes
- Example
- Static UINT nIndicators
- ID_SEPARATOR,
- ID_SEPARATOR,
- ID_SEPARATOR,
-
- m_wndStatusBar.Create(this)
- m_wndStatusBar.SetIndicators (nIndicators, 3)
- m_wndStatusBar.SetPaneInfo (0, ID_SEPARATOR,SBPS_N
OBORDERS,64) - m_wndStatusBar.SetPaneInfo (1, ID_SEPARATOR,SBPS_P
OPOUT,64) - m_wndStatusBar.SetPaneInfo (2,ID_SEPARATOR,SBPS_NO
RMAL SBPS_STRETCH, 0)
33Status Bar Support in AppWizard
- Initial Status bar box in AppWizard's Step 4
dialog box checking the Initial Status Bar box
34How to get rid of keyboard Indicator panes
- Static UINT indicators
-
- ID_SEPARATOR, //status line indicator
- ID_INDICATOR_CAPS,
- ID_INDICATOR_NUM,
- ID_INDICATOR_SCRL,
-
- Remove the final three entries so that the array
looks like this - Static UINT indicators
-
- ID_SEPARATOR //status line indicator
35MyWord Application
- MyWord uses two toolbars and one status bars
- New,Open,Save in file menu and the Cut,Copy,Paste
and Undo items in Edit menu. - It includes check push buttons,radio push buttons
and combo boxes for selecting .
36MyWord Application Code
37REBARS
- Internet Explorer 3.0 introduced a new control
type to windowsthe rebar control.A Rebar is a
container for other controls. - You can add as many bands as you like ,each band
can include a child window such as a toolbar,push
button ,or combo box. - Once the band is displayed the user can move and
resize the bands . - Each band can optionally have an image from an
image list ,a label, and a bitmap.
38REBARS
- Two new classes CReBar and CReBarCtrl.
- CReBarCtrl is a low level class that provides a
very thin wrapper around a raw rebar control. - CReBar is a high level class that makes easy to
add rebars too an MFC application - CReBar publishes three member functions
- Create, which creates a rebar from a CReBar
object. - GetReBarCtrl,which returns a CReBarCtrl reference
to the underlying rebar control - AddBar, which adds a band to the rebar
39MFC toolbar into a rebar
- m_wndToolBar.CreateEx(this)
- m_wndToolBar.LoadToolBar (IDR_TOOLBAR)
- m_wndReBar.Create (this)
- m_wndReBar.AddBar (m_wndToolBar)
- AddBars optional second and third parameters to
specify a label and a background bitmap. - m_bitmap.LoadBitmap(IDB_BKGND)
- m_wndToolBar.CreateEx(this,TBSTYLE_FLAT
TBSTYLE_TRANSPARENT) - m_wndReBar.AddBar (m_wndToolBar,_T(Main),
m_bitmap)
40Using AppWizard to wrap a rebar around a toolbar