VC++ Programming @ UESTC
1
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
In this section,we’ll talk a little bit of the document-view
architecture
Learn to create and use menus and keyboard accelerators.
Talk about command message handling in two dimensions.
Use rich edit control for text editing.
Use property sheet for setting edit options.
VC++ Programming @ UESTC
2
Class Goals as the followings:
1,Understand document-view architecture and know
how the document,main frame window,view
window,toolbar window,status bar window and
menu talk to each other.
2,Know how to make windows menu and floating
pop-up menu.
3,Use resource editor to add keyboard accelerators.
4,Understand command routing system and command
message handling.
5,Use rich edit control to play with text fonts,etc.
6,Know the usage of property sheets including the
DDX.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
3
Window Structure
Title Bar
Menu Bar
Toolbar window
View window
Status bar window
SDI
Main
Frame
window
Child
windows
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
4
DOCUMENT-VIEW architecture
The view window is just a child window inside the client
area of the main frame window which is the window we
ordinarily talk about.
The title bar and menu bar belongs to the main frame
window.
The view window,the toolbar window and status bar
window are inside the client area of the main frame
window.
The application framework provides a sophisticated
routing system for the command messages.
,
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
5
Document is the content of our application and the
view is how we look at it,For instance,document
is just like data in excel file and the views like
different chart tables(Row chart,Column
chart,PivotChart,etc) based on that same data,
The view’s inherited GetDocument member
function returns a pointer to the document object.
The key is to separate data from how we look at
them,It adds another layer in the OOD world for
easy organizing our work.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
6
Let’s create a Single Document Interface (SDI)
application and add menus and keyboard accelerators.
Step One,create a SDI exe project
Step two,create menu items
1,Open resource editor
2,Drag the empty box to the place you want to put and click
the right mouse button then click the,properties” item on
the popup menu,
3,Don’t give a ID number for the first level menu item(let it
be gray),make sure you check the,Pop-up” box,Give a
caption you like and put,&I” inside a bracelet following
the caption,then click,Enter” key.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
7
Step two,create menu items
4,The Pop-up option give you a chance to create a next
level menu items,The,&I” is the shortcut system the
framework gives you,The Ctrl-I will activate the menu
item.
5,Click the newly created top level menu item and right
click the empty box under,click the,properties” item and
do the similar job as above except you give a ID this time,
Uncheck the,Pop-up” option.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
8
Step two,create menu items
6,This ID will respond to user events inside your
VC++ code,The ID’s structure reflects the menu
levels,like ID_INSERT_LINES_LINE which is
a three level submenu ID.
7,create another second level menu,don’t give ID
this time and check the,Pop-up” option,Now
you can create a sub menu under this one.
8,In this way you may create any level sub-menu as
you like.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
9
Step three,create keyboard accelerators
1,Click the Accelerator--IDR_MAINFRAME inside
resource editor and you’ll see many IDs associated
with Keys.
2,Inside the right panel,right click your mouse and click
the,new accelerator” on the popup menu,You’ll see a
popup,Accel Properties” window.
3,Choose a ID you may want to respond to.
4,Type a letter and choose any combination of modifiers,
Don’t make the same accelerator for any two different
user events.
5,A accelerator does not have to be associated with a
menu item,It connects the keystrokes with your
responding VC++ codes.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
10
Command Message Handling
The ID connects the menu items to the message handling,
Make menu items first,then map the associated ID to the
application.
Use ClassWizard to add command message handlers in view class.
Step One,Use the framework to make your life easier
1,Choose the view class under the,Class Name”;
2,Select Object’s ID you want to respond;
3,Choose,COMMAND” inside,Messages” panel and you’ll see
a popup dialog window;
4,Click,Enter”,and go to view class,*.h” and,*.cpp” to see
what’s added there.
Step two,add codes inside view class to do the real work.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
11
Look at the command message routing system.
The framework checks the command message handler in
one of the sequences listed here.
The application calls only the higher level handler if
there’re two in different class.
The application can disable a menu item if it does not
find a message handler for that menu item.,图表 (T)”
menu item is disabled for this reason.
SDI App MDI App
View View
Document Document
SDI main frame window MDI child frame window
Application MDI main frame window
Application
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
12
Chapter Twelve Menus,Keyboard Accelerators,the RichEdit
Control and Property Sheets
Why does the command message handler in document class
not react even if we comment out the codes inside view
class function?
Does this not obey to the message routing rules?
We need to comment out the whole message handler in
view class,cpp file to make the document class do the job!!!
This proves the order in which the application framework
handle command message.
VC++ Programming @ UESTC
13
Command Message Handling in Derived Class
The command routing system is one dimension of
message handling,the class hierarchy is the second,
The derived class inherits all the base class
message map functions.
If you want to override one of the base class
message map function,you must add both a
function and a message map entry to your derived
class.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
14
Update Command User Interface (UI) Handlers
You add UI handler to keep the menu items synchronized
with the application’s state,You make the appearance of
menu items match the internal state of the application.
The handler function’s argument is a CCmdUI object which
contains a pointer to the corresponding menu item.
void
CSDIMenuKBView::OnUpdateInsertLinesLine(CCmdUI*
pCmdUI)
an example
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
15
MFC Text Edit Options
We can use rich edit control to make a fairly decent text
editor.
Windows supplies two text editing tools,the original edit
control (CEditView) and the rich edit common control
(CRichEditView).
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
16
The CEditView has all the functionality of both CView and
CEdit.
CRichEditView is designed to be used with the
CRichEditDoc and CRichEditCntrItem classes to
implement a complete ActiveX container application,we’’ll
skip it now.
In our class we’ll use a CView and CRichEditCtrl,
In our example,the CRichEditCtrl saves its contents in a
CString data member instead of in the document to make
things simple at this time.
Let’s build ex12A now,
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
17
A property sheet is a nice UI element that allows you to
cram lots of categorized info into small dialog,We add a
property sheet to allow users change the rich edit control’s
font characteristics.
First Let’s watch the PropertySheet Action picture
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
18
Menu Selection? PropertySheet Activated
User do Page Selection and make changes
User click OK or Apply Button
DDX called to change data and Send Message
to view class
View class call RichEdit control to change the
text format
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
19
Details about Property Sheets
Let’s add property sheets to the previous project to
change the font of the rich edit control.
Following the general steps to build a property sheet:
1,Use the resource editor to create a series of dialog
templates for use
2,Generate a class for each template and choose
CPropertyPage as base class,add data member for the
control class.
3,Generate a single class derived from CPropertySheet,add
one data member for each page class.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
20
steps to build a property sheet:
4,In sheet class constructor,call AddPage function for each
page.
5,In the application,construct an object of the derived
CPropertySheet class and then call DoModal when the
selection is made.
6,Take Special care of the,Apply” Button action
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
21
Take Special care of the,Apply” Button action
1,The Property Sheet Dialog Data Exchange(DDX) updates
the data member when the user switches to and from that
page.
2,The,Apply” Button is disabled initially,You make it
enabled when the user makes changes on that page.
3,The framework gets a WM_NOTIFY message for all
button clicks and then call virtual OnApply for all pages,
So you need to override OnApply in only one page class.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
22
4,Because CPage4::OnInitDialog() is called to set the spin
button control,OnCommand is called and Apply is
enabled for this page.
5,A global view pointer is used to send the user-defined
message to the view,A better way would be to pass the
view pointer to the sheet constructor and then to the page
constructor.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
23
Create a pop-up floating menu using CMenu class.
1,Use resource editor to create a second menu resource and
submenu as before;
2,Use ClassWizard to add a WM_CONTEXTMENU
message handler in the view class
3,Add some codes to load the floating menu by using
CMenu class.
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
24
void CEx12aView::OnContextMenu(CWnd* pWnd,CPoint
point)
{
CMenu menu;
menu.LoadMenu(IDR_MYFLOATINGMENU);
menu.GetSubMenu(0)->TrackPopupMenu
(TPM_LEFTALIGN|TPM_RIGHTBUTTON,
point.x,point.y,this);
}
Chapter 12 Menus,Keyboard Accelerators,
the RichEdit Control and Property Sheets
VC++ Programming @ UESTC
25
Chapter 13 Toolbars and Status Bars
Customize the toolbar
Customize the status bar
VC++ Programming @ UESTC
26
Control Bars and the Application Framework
CToolBar,CStatusBar are all derived from CControlBar,
which is derived from CWnd.
CControlBar supports the control bar windows inside
frame windows,
These control bar windows resizes with the parent frame
windows.
The framework takes care of the construction,windows
creation and destruction of the control bar objects.
Chapter 13 Toolbars and Status Bars
VC++ Programming @ UESTC
27
The Toolbar
A toolbar consists of arranged graphical buttons that
respond to messages just as other normal buttons.
Toolbar is fully dockable.
You put diff bitmap pictures on each toolbar button.
Chapter 13 Toolbars and Status Bars
VC++ Programming @ UESTC
28
The Toolbar Button States
It can be one of the six states.
It can be pushbutton or check box button.
Chapter 13 Toolbars and Status Bars
VC++ Programming @ UESTC
29
Command message
Most of the time,a toolbar matches a menu option.
But it doesn’t have to mirror a menu item.
How to group the toolbar buttons? Separator.
Use classwizard to match the message.
Better to add keyboard accelerators for buttons.
Chapter 13 Toolbars and Status Bars
VC++ Programming @ UESTC
30
UI Message Handlers
same as menu item
CCmdUI::Enale(FALSE) grays the buttons.
CCmdUI::SetCheck() implements checkbox
buttons.
The update of the buttons appearance happens
during the idle processing.
Chapter 13 Toolbars and Status Bars
VC++ Programming @ UESTC
31
ToolTips
Same as menu item
edit the property for each button,add,prompt” which is
ended with,\n” character.
Chapter 13 Toolbars and Status Bars
VC++ Programming @ UESTC
32
Locating the main frame window
You can’t use CWnd::GetParentFrame() to get the main frame
window in MDI application because its parent is MDI child
frame.
Instead you use the following inside view class
CMainFrame* pFrame =
(CMainFrame*) AfxGetApp()->m_pMainWnd;
CToolBar* pToolBar = &pFrame->m_wndToolBar;
Example
Chapter 13 Toolbars and Status Bars
VC++ Programming @ UESTC
33
The Status Bar
It is used to display information.
It has two types of text panes:
1,Message line panes
2,Status indicator panes
Chapter 13 Toolbars and Status Bars
VC++ Programming @ UESTC
34
Status Indicator Pane
Add a new ID_INDICATOR_ * in the string table of
resource editor,This ID’s static caption can be used to show
certain action for your program.
pCmdUI->Enable(::GetKeyState(VK_CAPITAL)&1);
Chapter 13 Toolbars and Status Bars
VC++ Programming @ UESTC
35
The Message Line pane
Adding ID_SEPARATOR inside the static indicator array in
MainFrm.cpp will add a separate pane in the status bar,
Your program can dynamically change its text.
CMainFrame* pFrame =
(CMainFrame*) AfxGetApp()->m_pMainWnd;
CStatusBar* pStatus = &pFrame->m_wndStatusBar;
pStatus->SetPaneText(0,“Any Text”);
Example,Ex13b
Chapter 13 Toolbars and Status Bars