⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch10.htm

📁 VC使用所有细节的逻列
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<HTML>

<HEAD>

<TITLE>Special Edition Using Visual C++ 5 - Chapter 10</TITLE>

<LINK REL="Next" HREF="ch11.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch11.htm">

<LINK REL="Previous" HREF="ch09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch09.htm"></HEAD>

<BODY BGCOLOR="#FFFFFF" TEXT="#000000">



<H2><A ID="I1" NAME="I1"><B>Chapter 10</B></A></H2>

<H2><A ID="I2" NAME="I2"></A><B>Status Bars and Toolbars</B></H2>

<hr>

<ul>

<li> <B>Add or delete toolbar buttons</B></P>

<P>  The buttons on a toolbar must reflect your application's specific command set.</P>

<li> <B>Specify <i>ToolTips</i> and descriptions for your toolbar buttons</B></P>

<P>  When a user places the mouse pointer over a button, your application can display useful information about the command associated with that particular button.</P>

<li> <B>Respond to toolbar buttons</B></P>

<P>  Just like menu commands, toolbar buttons must be associated with message-response functions.</P>

<li> <B>Adding panes to a status bar</B></P>

<P>  You're not stuck with MFC's default status bar. Your application's status bar can display whatever information you'd like it to display.</P>

</ul>

<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 elsewhere in this book. In this chapter, you learn how to get the most out of toolbars and status bars.</P>

<H3><A ID="I3" NAME="I3"><B>Working with Toolbars</B></A></H3>

<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 get 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'll 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 10.1. This toolbar provides buttons for the commonly used commands in the <U>F</U>ile and <U>E</U>dit menus, as well as a button 
for displaying the About dialog box. But what if your application doesn't support these commands? It's up to you to modify the default toolbar to fit your application.</P>

<A HREF="Lfigs01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch10/Lfigs01.gif"><b>Fig. 10.1</b></A>

<P><I>The default toolbar provides buttons for commonly used commands.</I></P>

<P><A ID="I4" NAME="I4"><B>Deleting Toolbar Buttons</B></A></P>

<P>Create a multiple document interface application with a toolbar by choosing <U>F</U>ile, <U>N</U>ew, selecting the Project tab, 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 10.1.</P>

<P>Before moving on, play with this toolbar a little. On the View menu, you can toggle whether the toolbar is displayed or not. Turn it off, 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&#146;s a floating palette. Drag it around and drop it at the bottom of the application, or one of the sides&#151;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&#146;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. Click the + next to Toolbar, and double-click the <font 
color="#008000">IDR_MAINFRAME</font> toolbar resource to edit it, as shown in Figure 10.2.</P>

<A HREF="Lfigs02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch10/Lfigs02.gif"><b>Fig. 10.2</b></A>

<P><I>Use the </I><I>toolbar editor</I><I> 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. Just 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 10.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 may want to create. If you leave it blank, it doesn't appear in the final toolbar.</P>

<A HREF="Lfigs03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch10/Lfigs03.gif"><b>Fig. 10.3</b></A>

<P><I>This edited toolbar has only a single button left (not counting the blank button template).</I></P>

<P><A ID="I5" NAME="I5"><B>Adding Buttons to a Toolbar</B></A></P>

<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 10.4.</P>

<A HREF="Lfigs04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch10/Lfigs04.gif"><b>Fig. 10.4</b></A>

<P><I>Click the button template to bring it up in the </I><I>button editor</I><I>.</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 (via the Ellipse tool), which takes care of creating the button's icon. Bring up the properties box and give the 
button an appropriate ID, which in this case might be something like <B>ID_CIRCLE</B>.</P>

<P>Now you need to define the button's ToolTip and description. The ToolTip appears whenever the user leaves the mouse pointer over the button for a second or two and acts as a reminder of the button's purpose. A ToolTip of &quot;Circle&quot; would be 
appropriate for the circle button. The description appears in the application's status bar. In this case, a description of &quot;Draws a red circle in the window&quot; might be good. Type these two text strings into the Pro<U>m</U>pt box. The description 
comes first, followed by the newline character (<font color="#008000">\n</font>) and the ToolTip, as shown in Figure 10.5.</P>

<A HREF="Lfigs05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch10/Lfigs05.gif"><b>Fig. 10.5</b></A>

<P><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 that is already connected to some code. In those 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. In this application, the toolbar button does not mirror a menu item, so you associate the ID with a message-handler 
function which MFC automatically calls when the user clicks the button.</P>

<P>To do this, follow these steps:</P>

<ol> 

<li><P> Make sure the button for which you want to create a message hander is selected in the custom toolbar, and then bring up ClassWizard. </P>

<li><P> The MFC ClassWizard property sheet appears, with the button's ID already selected (see Figure 10.6). To add the message-response function, select in the Class <U>N</U>ame box the class to which you want to add the function (the sample application 
uses the view class). </P>

<li><P> Double-click the COMMAND selection in the Messa<U>g</U>es box. </P>

<li><P> Accept the function name that MFC suggests in the next message box, and you're all set. Click the OK button to finalize your changes.</P>

</ol>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>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&#151;in fact, for all intents and purposes, toolbar buttons <I>are</I> menu commands.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<A HREF="Lfigs06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch10/Lfigs06.gif"><b>Fig. 10.6</b></A>

<P><I>You can use ClassWizard to catch messages from your toolbar buttons.</I></P>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>Ordinarily, toolbar buttons duplicate menu commands, providing a quicker way for the user to select commonly used commands in the menus. In this 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 gets called whether the user selects the command from the menu bar or from the toolbar.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>If you compile and run the application now, you'll see the window shown in Figure 10.7. In the figure, you can see the new toolbar button, as well as its ToolTip and description line. The toolbar looks a little sparse in this example, but you can add 
as many buttons as you like.</P>

<A HREF="Lfigs07.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch10/Lfigs07.gif"><b>Fig. 10.7</b></A>

<P><I>The new toolbar button shows its ToolTip and description.</I></P>

<P>You can create as many buttons as you need; just follow the aforementioned procedures for each. After you have the buttons created, you're through with the toolbar resources and are 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 <font color="#008000">OnCircle()</font>, 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 10.1.</P>



<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing 10.1&#151;An Empty Message-Response Function</I></P>

<pre><font color="#008000">void CToolView::OnCircle() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    // TODO: Add your command handler code here</font></pre>

<pre><font color="#008000">    </font></pre>

<pre><font color="#008000">}</font></pre>

<P>Although the circle button is supposed to draw a red circle in the window, you can see that the <font color="#008000">OnCircle()</font> function is going to need a little help accomplishing that task. Add the lines shown in Listing 10.2 to the function 
so that circle button will actually do what it's supposed to do, as shown in Figure 10.8.</P>

<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing 10.2</I><I>&#151;</I>CToolView::OnCircle()</P>

<pre><font color="#008000">void CToolView::OnCircle() </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     CClientDC clientDC(this);</font></pre>

<pre><font color="#008000">     CBrush newBrush(RGB(255,0,0));</font></pre>

<pre><font color="#008000">     CBrush* oldBrush = clientDC.SelectObject(&amp;newBrush);</font></pre>

<pre><font color="#008000">     clientDC.Ellipse(20, 20, 200, 200);</font></pre>

<pre><font color="#008000">     clientDC.SelectObject(oldBrush);</font></pre>

<pre><font color="#008000">}</font></pre>

<A HREF="Lfigs08.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch10/Lfigs08.gif"><b>Fig. 10.8</b></A>

<P><I>After adding code to </I><I>OnCircle()</I><I>, the new toolbar button actually does something.</I></P>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>You can find the circle-drawing application in the CHAP11\TOOL directory of this book's CD-ROM.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P><A ID="I6" NAME="I6"><B>The </B><B><I>CToolBar</I></B><B> Class's Member Functions</B></A></P>

<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, there may be times when you want to change the toolbar's default behavior or appearance in some way. In those cases, you can call upon the <font color="#008000">CToolBar</font> class's member 
functions, which are listed in <A ID="I7" NAME="I7">Table</A> 10.1 along with their descriptions. The toolbar is accessible from the <font color="#008000">CMainFrame</font> class as the <font color="#008000">m_wndToolBar</font> member variable. Usually you 
change the toolbar behavior in <font color="#008000">CMainFrame::OnCreate()</font>.</P>

<P><I>Table 10.1&#151;Member Functions of the </I>CToolBar<I> Class</I></P>

<TABLE BORDER>

<TR>

<TD>

<P><B>Function</B></P>

<TD>

<P><B>Description</B></P>

<TR>

<TD>

<pre><font color="#008000">CommandToIndex()</font></pre>

<TD>

<P>Gets the index of a button given its ID.</P>

<TR>

<TD>

<pre><font color="#008000">Create()</font></pre>

<TD>

<P>Creates the toolbar.</P>

<TR>

<TD>

<pre><font color="#008000">GetButtonInfo()</font></pre>

<TD>

<P>Gets information about a button.</P>

<TR>

<TD>

<pre><font color="#008000">GetButtonStyle()</font></pre>

<TD>

<P>Gets a button's style.</P>

<TR>

<TD>

<pre><font color="#008000">GetButtonText()</font></pre>

<TD>

<P>Gets a button's text label.</P>

<TR>

<TD>

<pre><font color="#008000">GetItemID()</font></pre>

<TD>

<P>Gets the ID of a button given its index.</P>

<TR>

<TD>

<pre><font color="#008000">GetItemRect()</font></pre>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -