📄 ch12.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
<!--
function popUp(pPage) {
var fullURL = document.location;
var textURL = fullURL.toString();
var URLlen = textURL.length;
var lenMinusPage = textURL.lastIndexOf("/");
lenMinusPage += 1;
var fullPath = textURL.substring(0,lenMinusPage);
popUpWin = window.open('','popWin','resizable=yes,scrollbars=no,width=525,height=394');
figDoc= popUpWin.document;
zhtm= '<HTML><HEAD><TITLE>' + pPage + '</TITLE>';
zhtm += '</head>';
zhtm += '<BODY bgcolor="#FFFFFF">';
zhtm += '<IMG SRC="' + fullPath + pPage + '">';
zhtm += '<P><B>' + pPage + '</B>';
zhtm += '</BODY></HTML>';
window.popUpWin.document.write(zhtm);
window.popUpWin.document.close();
// Johnny Jackson 4/28/98
}
//-->
</SCRIPT>
<!--<link rel="stylesheet" href="../../../../includes/stylesheets/ebooks.css">-->
<META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1">
<TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Ch 12 -- Adding Toolbars and Status Bars</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"
BORDER="0"><BR>
Teach Yourself Visual C++ 6 in 21 Days</H1>
<CENTER>
<P><A HREF="../ch11/ch11.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch13/ch13.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<H1 ALIGN="CENTER">- 12 -<BR>
Adding Toolbars and Status Bars</H1>
<H1></H1>
<UL>
<LI><A HREF="#Heading1">Toolbars, Status Bars, and Menus</A>
<LI><A HREF="#Heading2">Designing a Toolbar</A>
<UL>
<LI><A HREF="#Heading3">Creating a New Toolbar</A>
<LI><A HREF="#Heading4">Attaching the Toolbar to the Application Frame</A>
<LI><A HREF="#Heading5">Controlling the Toolbar Visibility</A>
</UL>
<LI><A HREF="#Heading6">Adding a Combo Box to a Toolbar</A>
<UL>
<LI><A HREF="#Heading7">Editing the Project Resources</A>
<LI><A HREF="#Heading8">Creating the Toolbar Combo Box</A>
<LI><A HREF="#Heading9">Handling the Toolbar Combo Box Events</A>
<LI><A HREF="#Heading10">Updating the Toolbar Combo Box</A>
</UL>
<LI><A HREF="#Heading11">Adding a New Status Bar Element</A>
<UL>
<LI><A HREF="#Heading12">Adding a New Status Bar Pane</A>
<LI><A HREF="#Heading13">Setting a Status Bar Pane Text</A>
</UL>
<LI><A HREF="#Heading14">Summary</A>
<LI><A HREF="#Heading15">Q&A</A>
<LI><A HREF="#Heading16">Workshop</A>
<UL>
<LI><A HREF="#Heading17">Quiz</A>
<LI><A HREF="#Heading18">Exercises</A>
</UL>
</UL>
<P>
<HR SIZE="4">
<BR>
When you created your SDI and MDI applications, they not only came with default menus
already attached, but also they came with simple toolbars to go with the menus. These
simple toolbars had the standard set of functions (New, Open, Save, Print, Cut, Copy,
and Paste) that are on the toolbars of most Windows applications. Most applications
don't limit their toolbars to just this standard selection of functions but have
customized toolbars that reflect the specific functionality of the application.</P>
<P>In addition to the toolbars, the SDI and MDI applications have a status bar at
the bottom of the frame that provides textual descriptions of the toolbar buttons
and menu entries. The status bar also has default areas that display whether the
Caps, Num, and Scroll Lock keys are on.</P>
<P>Today, you will learn</P>
<P>
<UL>
<LI>How to design your own toolbar.
<P>
<LI>How to attach your toolbar to the application frame.
<P>
<LI>How to show and hide your toolbar with a menu entry.
<P>
<LI>How to place a combo box on your toolbar.
<P>
<LI>How to display descriptions of your toolbar entries in the status bar.
<P>
<LI>How to add your own status bar elements.
</UL>
<H2><A NAME="Heading1"></A>Toolbars, Status Bars, and Menus</H2>
<P>One of the driving intentions behind the development of Graphical User Interfaces
(GUI) such as Windows was the goal of making computers easier to use and learn. In
the effort to accomplish this goal, GUI designers stated that all applications should
use a standard set of menus and that the menus should be organized in a standardized
manner. When Microsoft designed the Windows operating system, it followed this same
philosophy, using a standard set of menus organized in a standard order on most of
its applications.</P>
<P>A funny thing happened once Windows became widely used. The application designers
found that new users still had a difficult time learning new applications and that
advanced users found the menus cumbersome. As a result, the application designers
invented toolbars as one solution to both problems.</P>
<P>A toolbar is a small band attached to the window frame or a dialog window that
is floating independent of the application frame. This band (or dialog) has a number
of small buttons containing graphic images that can be used in place of the menus.
The application designers place the most commonly used functions for their applications
on these toolbars and do their best to design graphical images that illustrate the
functions the buttons serve.</P>
<P>Once advanced users learned what each of the toolbar buttons do, the toolbars
were a hit. However, novice users still had problems learning what the toolbar does.
As a result, the application designers went back to the drawing board to come up
with ways to help the new user learn how use the toolbar buttons.</P>
<P>One of the solutions was to use the information bar that many of them had begun
placing at the bottom of application windows to provide detailed descriptions of
both menu entries and toolbar buttons. One of the other solutions was to provide
a little pop-up window with a short description of the button that appears whenever
the mouse is positioned over the button for more than a couple of seconds. The first
of these solutions became known as the status bar, and the second became known as
tooltips. Both solutions are in common practice with most Windows applications today.</P>
<P>If you want to design and use your own toolbars and status bars in your applications,
you might think that Visual C++ provides plenty of support for your efforts and even
makes it easy to implement. After all, Microsoft's own application developers have
been in the forefront of developing these elements, and most, if not all, of Microsoft's
Windows applications are developed using its own Visual C++. Well, you are correct
in making that assumption, and today, you'll learn how to create your own custom
toolbars and status bars for your applications.</P>
<P>
<H2><A NAME="Heading2"></A>Designing a Toolbar</H2>
<P>For learning how to create your own toolbar, you will modify the application that
you created on Day 10, "Creating Single Document Interface Applications,"
the SDI drawing application, to add a toolbar for selecting the color to use in drawing.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> Although the sample application you are working with today is an
extension to the application you built on Day 10, all file and class names have been
changed from Day10 to Toolbar. If you are making the changes in the Day 10 project,
then when the following text specifies that you make changes to the CToolbarDoc class,
you should make the changes to the CDay10Doc class. Likewise, when you are asked
to edit the Toolbar.rc file, you can edit the Day10.rc file.
<HR>
</BLOCKQUOTE>
<P>If all you want to do is add a few additional toolbar buttons to the default toolbar
that the AppWizard creates when you start a new SDI or MDI application, you can pull
up the toolbar in the Visual C++ designer through the Resource View in the workspace
pane and begin adding new buttons. Just as in the Menu Designer, the end of the toolbar
always has a blank entry, waiting for you to turn it into another toolbar button,
as shown in Figure 12.1. All you have to do is select this blank button, drag it
to the right if you want a separator between it and the button beside it, or drag
it to a different position if you want it moved. After you have the button in the
desired location, you paint an icon on the button that illustrates the function that
the button will trigger. Finally, double-click the button in the toolbar view to
open the button's properties dialog and give the button the same ID as the menu that
it will trigger. The moment that you compile and run your application, you will have
a new toolbar button that performs a menu selection that you chose. If you want to
get rid of a toolbar button, just grab it on the toolbar view, and drag it off the
toolbar.</P>
<P><A HREF="javascript:popUp('12fig01tif.gif')"><B>FIGURE 12.1.</B></A><B> </B><I>The
toolbar designer.</I></P>
<P><I></I>
<H3><A NAME="Heading3"></A>Creating a New Toolbar</H3>
<P>To insert a new toolbar, right-click on the Toolbar folder and select Insert Toolbar
from the pop-up menu. This creates an empty toolbar with a single blank button. As
you start drawing an icon on each of the blank buttons in the toolbar, another blank
button is added on the end.</P>
<P>For use in your drawing application, fill eight buttons with the eight colors
available in the drawing application.</P>
<P>Once you draw icons on each of the buttons in your toolbar, double-click on the
first button in the toolbar view. This should open the toolbar button properties
dialog. In the ID field, enter (or select from the drop-down list) the ID of the
menu that this toolbar button should trigger. In the Prompt field, enter the description
that should appear in the status bar for this toolbar button. (If you entered a prompt
for the menu, then this field is automatically populated with the menu description.)
At the end of the status bar description, add \n and a short description to appear
in the tooltips for the toolbar button.</P>
<BLOCKQUOTE>
<P>
<HR>
<STRONG>NOTE:</STRONG> In C/C++, the \n string is a shorthand notation for "begin a
new line." In the prompt for toolbar buttons and menu entries, this string is
used to separate the status bar descriptions of the menu entries and the tooltips
pop-up prompt that appears when the mouse is held over a toolbar button for a few
seconds. The first line of the prompt is used for the status bar description, and
the second line is used for the tooltips description. The tooltips description is
only used with the toolbars, so there's no reason to add this for menu entries that
will have no toolbar equivalents.
<HR>
</BLOCKQUOTE>
<P>For example, for the black button on the toolbar that you are creating for your
drawing application, enter an ID of <B>ID_COLOR_BLACK</B> and a prompt of <B>Black
drawing color\nBlack</B>, as shown in Figure 12.2.</P>
<P><A HREF="javascript:popUp('12fig02tif.gif')"><B>FIGURE 12.2.</B></A><B> </B><I>The
toolbar button properties dialog.</I></P>
<P>Once you finish designing your toolbar and have icons on all of your buttons with
the properties set for each button, you will change the toolbar ID. In the workspace
pane, right-click the new toolbar that you just added and open its properties dialog.
Change the toolbar ID to a descriptive name.</P>
<P>As an example, for the color toolbar that you created for your drawing application,
change the toolbar ID to IDR_TBCOLOR.</P>
<P>
<H3><A NAME="Heading4"></A>Attaching the Toolbar to the Application Frame</H3>
<P>In the previous SDI and MDI applications, you didn't add any functionality that
required you to touch the frame window. Well, because the toolbar is attached to
the frame, you'll have to begin adding and modifying code in that module. If you
open the CMainFrame class to the OnCreate function, you'll see where it's creating
the existing toolbar and then later in this function where the toolbar is being attached
to the frame.</P>
<P>Before you can add your toolbar to the application frame, you need to add a variable
to the CMainFrame class to hold the new toolbar. This variable of type CToolBar should
be protected in accessibility.</P>
<P>To add your color toolbar to your draw application, right-click the CMainFrame
class in the Class View tab of the workspace pane. Select Add Member Variable from
the pop-up menu, and specify the variable type as CToolBar, the name as m_wndColorBar,
and the access as protected.</P>
<P>After you add a variable for your toolbar, you need to add some code in the OnCreate
function in the CMainFrame class to add the toolbar and attach it to the frame. Make
the modifications in Listing 12.1 to add the color toolbar to your drawing application.</P>
<P>
<H4>LISTING 12.1. THE MODIFIED CMainFrame.OnCreate FUNCTION.</H4>
<PRE> 1: int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
2: {
3: if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
4: return -1;
5:
6: if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT,
ÂWS_CHILD | WS_VISIBLE | CBRS_TOP
7: | CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | ÂCBRS_SIZE_DYNAMIC) ||
8: !m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
9: {
10: TRACE0("Failed to create toolbar\n");
11: return -1; // fail to create
12: }
13:
14: ///////////////////////
</PRE>
<PRE>15: // MY CODE STARTS HERE
</PRE>
<PRE>16: ///////////////////////
17:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -