📄 ch09.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
<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 += '<link rel="stylesheet" href="/includes/stylesheets/ebooks.css"></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="ebooks.css" tppabs="http://www.fintech.ru/includes/stylesheets/ebooks.css">
<TITLE>Special Edition Using Visual C++ 6 -- Ch 9 -- Status Bars and Toolbars</TITLE>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF">
<CENTER>
<H1><IMG SRC="que.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/que.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM" BORDER="0"><BR>
Special Edition Using Visual C++ 6</H1>
</CENTER>
<CENTER>
<P><A HREF="ch08.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/ch08/ch08.htm"><IMG SRC="previous.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="ch10.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/ch10/ch10.htm"><IMG
SRC="next.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="index.htm" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/index.htm"><IMG SRC="contents.gif" tppabs="http://www.fintech.ru/library/prog/SEUsingVC6/button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A>
<HR>
</CENTER>
<CENTER>
<H1>- 9 -</H1>
</CENTER>
<CENTER>
<H1>Status Bars and Toolbars</H1>
</CENTER>
<UL>
<LI><A HREF="#Heading1">Working with Toolbars</A>
<UL>
<LI><A HREF="#Heading2">Deleting Toolbar Buttons</A>
<LI><A HREF="#Heading3">Adding Buttons to a Toolbar</A>
<LI><A HREF="#Heading4">The CToolBar Class's Member Functions</A>
</UL>
<LI><A HREF="#Heading5">Working with Status Bars</A>
<UL>
<LI><A HREF="#Heading6">Creating a New Command ID</A>
<LI><A HREF="#Heading7">Creating the Default String</A>
<LI><A HREF="#Heading8">Adding the ID to the Indicators Array</A>
<LI><A HREF="#Heading9">Creating the Pane's Command-Update Handler</A>
<LI><A HREF="#Heading10">Setting the Status Bar's Appearance</A>
</UL>
<LI><A HREF="#Heading11">Working with Rebars</A>
</UL>
<P>
<HR SIZE="4">
<CENTER>
<H1></H1>
</CENTER>
<P>Building a good user interface is half the battle of programming a Windows application.
Luckily, Visual C++ and its AppWizard supply an amazing amount of help in creating
an application that supports all the expected user-interface elements, including
menus, dialog boxes, toolbars, and status bars. The subjects of menus and dialog
boxes are covered in Chapters 2, "Dialogs and Controls," and 8, "Building
a Complete Application: ShowString." In this chapter, you learn how to get the
most out of toolbars and status bars.</P>
<P>
<H2><A NAME="Heading1"></A>Working with Toolbars</H2>
<P>The buttons on a toolbar correspond to commands, just as the items on a menu do.
Although you can add a toolbar to your application with AppWizard, you still need
to use a little programming polish to make things just right. This is because every
application is different and AppWizard can create only the most generally useful
toolbar for most applications. When you create your own toolbars, you will probably
want to add or delete buttons to support your application's unique command set.</P>
<P>For example, when you create a standard AppWizard application with a toolbar,
AppWizard creates the toolbar shown in Figure 9.1. This toolbar provides buttons
for the commonly used commands in the File and Edit menus, as well as a button for
displaying the About dialog box. What if your application doesn't support these commands?
It's up to you to modify the default toolbar to fit your application.</P>
<P><A HREF="javascript:popUp('09uvc01.gif')"><B>FIG. 9.1</B></A><B> </B><I>The default
toolbar provides buttons for commonly used commands.</I></P>
<P>
<H3><A NAME="Heading2"></A>Deleting Toolbar Buttons</H3>
<P>Create a multiple document interface application with a toolbar by choosing File,
New; selecting the Project tab; highlighting MFC AppWizard (exe); naming the application
<B>Tool</B>; and accepting the defaults in every dialog box. If you like, you can
click the Finish button in step 1 to speed up the process. AppWizard provides a docking
toolbar by default. Build and run the application, and you should see a toolbar of
your own, just like Figure 9.1.</P>
<P>Before moving on, play with this toolbar a little. On the View menu, you can toggle
whether the toolbar is displayed. Turn it off and then on again. Now click and hold
on the toolbar between buttons and pull it down into the working area of your application.
Let it go, and it's a floating palette. Drag it around and drop it at the bottom
of the application or one of the sides--it will dock against any side of the main
window. Watch the tracking rectangle change shape to show you it will dock if you
drop it. Drag it back off again so that it's floating and close it by clicking the
small x in the upper-right corner. Bring it back with the View menu and notice that
it comes back right where you left it. All this functionality is yours free from
AppWizard and MFC.</P>
<P>The first step in modifying the toolbar is to delete buttons you no longer need.
To do this, first select the ResourceView tab to display your application's resources
by clicking on the + next to Tool Resources. Click the + next to Toolbar and double-click
the IDR_MAINFRAME toolbar resource to edit it, as shown in Figure 9.2. (The Graphics
and Colors palettes, shown floating in Figure 9.2, are docked by default. You can
move them around by grabbing the wrinkles at the top.)</P>
<P><A HREF="javascript:popUp('09uvc02.gif')"><B>FIG. 9.2</B></A> <I>Use the toolbar
editor to customize your application's toolbar.</I></P>
<P>After you have the toolbar editor on the screen, deleting buttons is as easy as
dragging the unwanted buttons from the toolbar. Place your mouse pointer on the button,
hold down the left mouse button, and drag the unwanted button away from the toolbar.
When you release the mouse button, the toolbar button disappears. In the Tool application,
delete all the buttons except the Help button with a yellow question mark. Figure
9.3 shows the edited toolbar with only the Help button remaining. The single blank
button template is only a starting point for the next button you want to create.
If you leave it blank, it doesn't appear in the final toolbar.</P>
<P><A HREF="javascript:popUp('09uvc03.gif')"><B>FIG. 9.3</B></A><B> </B><I>This edited
toolbar has only a single button left (not counting the blank button template).</I></P>
<H3><A NAME="Heading3"></A>Adding Buttons to a Toolbar</H3>
<P>Adding buttons to a toolbar is a two-step process: First you draw the button's
icon, and then you match the button with its command. To draw a new button, first
click the blank button template in the toolbar. The blank button appears enlarged
in the edit window, as shown in Figure 9.4.</P>
<P><A HREF="javascript:popUp('09uvc04.gif')"><B>FIG. 9.4</B></A><B> </B><I>Click
the button template to open it in the button editor.</I></P>
<P>Suppose you want to create a toolbar button that draws a red circle in the application's
window. Draw a red circle on the blank button with the Ellipse tool, and you've created
the button's icon. Open the properties box and give the button an appropriate ID,
such as ID_CIRCLE in this case.</P>
<P>Now you need to define the button's description and ToolTip. The description appears
in the application's status bar. In this case, a description of "Draws a red
circle in the window" might be good. The ToolTip appears whenever the user leaves
the mouse pointer over the button for a second or two, acting as a reminder of the
button's purpose. A ToolTip of <I>Circle</I> would be appropriate for the circle
button. Type these two text strings into the Prompt box. The description comes first,
followed by the newline character (\n) and the ToolTip, as shown in Figure 9.5.</P>
<P><A HREF="javascript:popUp('09uvc05.gif')"><B>FIG. 9.5</B></A><B> </B><I>After
drawing the button, specify its properties.</I></P>
<P>You've now defined a command ID for your new toolbar button. Usually, you use
the command ID of an existing menu item already connected to some code. In these
cases, simply choose the existing command ID from the drop-down box, and your work
is done. The prompt is taken from the properties of the menu item, and the message
handler has already been arranged for the menu item. You will already be handling
the menu item, and that code will handle the toolbar click, too. In this application,
the toolbar button doesn't mirror a menu item, so you will associate the ID with
a message-handler function that MFC automatically calls when the user clicks the
button.</P>
<P>To do this, follow these steps:</P>
<DL>
<DD><B>1. </B>Make sure the button for which you want to create a message handler
is selected in the custom toolbar, and then open ClassWizard.
<P>
<DT></DT>
<DD><B>2. </B>The MFC ClassWizard property sheet appears, with the button's ID already
selected (see Figure 9.6). To add the message-response function, select in the Class
Name box the class to which you want to add the function (the sample application
uses the view class).
<P>
<DT></DT>
<DD><B>3. </B>Double-click the COMMAND selection in the Messages box.
<P>
<DT></DT>
<DD><B>4. </B>Accept the function name that MFC suggests in the next message box,
and you're all set. Click OK to finalize your changes.
</DL>
<BLOCKQUOTE>
<P>
<HR>
<strong>NOTE:</strong> If you haven't defined a message-response function for a toolbar
button, or if there is no instance of the class that catches the message, MFC disables
the button when you run the application. For example, if the message is caught by
the document or view in an MDI application and there is no open document, the button
is disabled. The same is true for menu commands--in fact, for all intents and purposes,
toolbar buttons <I>are</I> menu commands.
<HR>
</BLOCKQUOTE>
<P><A HREF="javascript:popUp('09uvc06.gif')"><B>FIG. 9.6</B></A><B> </B><I>You can
use ClassWizard to catch messages from your toolbar buttons.</I></P>
<BLOCKQUOTE>
<P>
<HR>
<BR>
<strong>NOTE:</strong> Ordinarily, toolbar buttons duplicate menu commands, providing a
quicker way for the user to select commonly used commands in the menus. In that case,
the menu item and the toolbar button both represent the exact same command, and you
give both the same ID. Then the same message-response function is called, whether
the user selects the command from the menu bar or the toolbar. 
<HR>
</BLOCKQUOTE>
<P>If you compile and run the application now, you will see the window shown in Figure
9.7. In the figure, you can see the new toolbar button, as well as its ToolTip and
description line. The toolbar looks sparse in this example, but you can add as many
buttons as you like.</P>
<P>You can create as many buttons as you need; just follow the same procedure for
each. After you have created the buttons, you're through with the toolbar resources
and ready to write the code that responds to the buttons. For example, in the previous
example, a circle button was added to the toolbar, and a message-response function,
called OnCircle(), was added to the program. MFC calls that message-response function
whenever the user clicks the associated button. However, right now, that function
doesn't do anything, as shown in Listing 9.1.</P>
<P><A HREF="javascript:popUp('09uvc07.gif')"><B>FIG. 9.7</B></A><B> </B><I>The new
toolbar button shows its ToolTip and description.</I></P>
<P>
<H4>Listing 9.1  An Empty Message-Response Function</H4>
<PRE>void CToolView::OnCircle()
{
// TODO: Add your command handler code here
</PRE>
<PRE>}
</PRE>
<P>Although the circle button is supposed to draw a red circle in the window, you
can see that the OnCircle() function is going to need a little help accomplishing
that task. Add the lines shown in Listing 9.2 to the function so that the circle
button will do what it's supposed to do, as shown in Figure 9.8. This drawing code
makes a brush, selects it into the DC, draws an ellipse with it, and then restores
the old brush. The details of drawing are discussed in Chapter 5, "Drawing on
the Screen."</P>
<P>
<H4>Listing 9.2  CToolView::OnCircle()</H4>
<PRE>void CToolView::OnCircle()
{
CClientDC clientDC(this);
CBrush newBrush(RGB(255,0,0));
CBrush* oldBrush = clientDC.SelectObject(&newBrush);
clientDC.Ellipse(20, 20, 200, 200);
clientDC.SelectObject(oldBrush);
</PRE>
<PRE>}
</PRE>
<H3><A NAME="Heading4"></A>The CToolBar Class's Member Functions</H3>
<P>In most cases, after you have created your toolbar resource and associated its
buttons with the appropriate command IDs, you don't need to bother any more with
the toolbar. The code generated by AppWizard creates the toolbar for you, and MFC
takes care of calling the buttons' response functions for you. However, at times
you might want to change the toolbar's default behavior or appearance in some way.
In those cases, you can call on the CToolBar class's member functions, which are
listed in Table 9.1 along with their descriptions. The toolbar is accessible from
the CMainFrame class as the m_wndToolBar member variable. Usually, you change the
toolbar behavior in CMainFrame::OnCreate().</P>
<P><A HREF="javascript:popUp('09uvc08.gif')"><B>FIG. 9.8</B></A><B> </B><I>After
adding code to OnCircle(), the new toolbar button actually does something.</I></P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -