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

📄 ch09.htm

📁 Learning language of Visual C++6
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<P>
<H4>Table 9.1&#160;&#160;Member Functions of the CToolBar Class</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><B>Function</B></TD>
		<TD ALIGN="LEFT"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CommandToIndex()</TD>
		<TD ALIGN="LEFT">Obtains the index of a button, given its ID</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">Create()</TD>
		<TD ALIGN="LEFT">Creates the toolbar</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetButtonInfo()</TD>
		<TD ALIGN="LEFT">Obtains information about a button</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetButtonStyle()</TD>
		<TD ALIGN="LEFT">Obtains a button's style</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetButtonText()</TD>
		<TD ALIGN="LEFT">Obtains a button's text label</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetItemID()</TD>
		<TD ALIGN="LEFT">Obtains the ID of a button, given its index</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetItemRect()</TD>
		<TD ALIGN="LEFT">Obtains an item's display rectangle, given its index</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetToolBarCtrl()</TD>
		<TD ALIGN="LEFT">Obtains a reference to the CToolBarCtrl object represented by the CToolBar object</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">LoadBitmap()</TD>
		<TD ALIGN="LEFT">Loads the toolbar's button images</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">LoadToolBar()</TD>
		<TD ALIGN="LEFT">Loads a toolbar resource</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetBitmap()</TD>
		<TD ALIGN="LEFT">Sets a new toolbar button bitmap</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetButtonInfo()</TD>
		<TD ALIGN="LEFT">Sets a button's ID, style, and image number</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetButtons()</TD>
		<TD ALIGN="LEFT">Sets the IDs for the toolbar buttons</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetButtonStyle()</TD>
		<TD ALIGN="LEFT">Sets a button's style</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetButtonText()</TD>
		<TD ALIGN="LEFT">Sets a button's text label</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetHeight()</TD>
		<TD ALIGN="LEFT">Sets the toolbar's height</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetSizes()</TD>
		<TD ALIGN="LEFT">Sets the button sizes</TD>
	</TR>
</TABLE>
</P>
<P>Normally, you don't need to call the toolbar's methods, but you can achieve some
unusual results when you do, such as the extra high toolbar shown in Figure 9.9.
(The buttons are the same size, but the toolbar window is bigger.) This toolbar resulted
from a call to the toolbar object's SetHeight() member function. The CToolBar class's
member functions enable you to perform this sort of toolbar trickery, but use them
with great caution.</P>
<P><A HREF="javascript:popUp('09uvc09.gif')"><B>FIG. 9.9</B></A><B> </B><I>You can
use a toolbar object's member functions to change how the toolbar looks and acts.</I></P>
<P>
<H2><A NAME="Heading5"></A>Working with Status Bars</H2>
<P>Status bars are mostly benign objects that sit at the bottom of your application's
window, doing whatever MFC instructs them to do. This consists of displaying command
descriptions and the status of various keys on the keyboard, including the Caps Lock
and Scroll Lock keys. In fact, status bars are so mundane from the programmer's point
of view (at least they are in an AppWizard application) that they aren't even represented
by a resource that you can edit like a toolbar. When you tell AppWizard to incorporate
a status bar into your application, there's not much left for you to do.</P>
<P>Or is there? A status bar, just like a toolbar, must reflect the interface needs
of your specific application. For that reason, the CStatusBar class features a set
of methods with which you can customize the status bar's appearance and operation.
Table 9.2 lists the methods along with brief descriptions.</P>
<P>
<H4>Table 9.2&#160;&#160;Methods of the CStatusBar Class</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><B>Method</B></TD>
		<TD ALIGN="LEFT"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CommandToIndex()</TD>
		<TD ALIGN="LEFT">Obtains an indicator's index, given its ID</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">Create()</TD>
		<TD ALIGN="LEFT">Creates the status bar</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetItemID()</TD>
		<TD ALIGN="LEFT">Obtains an indicator's ID, given its index</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetItemRect()</TD>
		<TD ALIGN="LEFT">Obtains an item's display rectangle, given its index</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetPaneInfo()</TD>
		<TD ALIGN="LEFT">Obtains information about an indicator</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetPaneStyle()</TD>
		<TD ALIGN="LEFT">Obtains an indicator's style</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetPaneText()</TD>
		<TD ALIGN="LEFT">Obtains an indicator's text</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">GetStatusBarCtrl()</TD>
		<TD ALIGN="LEFT">Obtains a reference to the CStatusBarCtrl object represented by the CStatusBar object</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetIndicators()</TD>
		<TD ALIGN="LEFT">Sets the indicators' IDs</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetPaneInfo()</TD>
		<TD ALIGN="LEFT">Sets the indicators' IDs, widths, and styles</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetPaneStyle()</TD>
		<TD ALIGN="LEFT">Sets an indicator's style</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">SetPaneText()</TD>
		<TD ALIGN="LEFT">Sets an indicator's text</TD>
	</TR>
</TABLE>
</P>
<P>When you create a status bar as part of an AppWizard application, you see a window
similar to that shown in Figure 9.10. (To make your own, create a project called
<B>Status</B> and accept all the defaults, as you did for the Tool application.)
The status bar has several parts, called <I>panes</I>, that display certain information
about the status of the application and the system. These panes, which are marked
in Figure 9.10, include indicators for the Caps Lock, Num Lock, and Scroll Lock keys,
as well as a message area for showing status text and command descriptions. To see
a command description, place your mouse pointer over a button on the toolbar (see
Figure 9.11).</P>
<P>The most common way to customize a status bar is to add new panes. To add a pane
to a status bar, complete these steps:</P>

<DL>
	<DT></DT>
	<DD><B>1. </B>Create a command ID for the new pane.
	<P>
	<DT></DT>
	<DD><B>2. </B>Create a default string for the pane.
	<P>
	<DT></DT>
	<DD><B>3. </B>Add the pane's command ID to the status bar's indicators array.
	<P>
	<DT></DT>
	<DD><B>4. </B>Create a command-update handler for the pane.
	<P>
</DL>

<P><A HREF="javascript:popUp('09uvc10.gif')"><B>FIG. 9.10</B></A><B> </B><I>The default
MFC status bar contains a number of informative panes.</I></P>
<P>The following sections cover these steps in detail.</P>
<P><A HREF="javascript:popUp('09uvc11.gif')"><B>FIG. 9.11</B></A><B> </B><I>The message
area is mainly used for command descriptions.</I></P>
<H3><A NAME="Heading6"></A>Creating a New Command ID</H3>
<P>This step is easy, thanks to Visual C++'s symbol browser. To add the command ID,
start by choosing View, Resource Symbols. When you do, you see the Resource Symbols
dialog box (see Figure 9.12), which displays the currently defined symbols for your
application's resources. Click the New button, and the New Symbol dialog box appears.
Type the new ID, <B>ID_MYNEWPANE</B>, into the Name box (see Figure 9.13). Usually,
you can accept the value that MFC suggests for the ID.</P>
<P><A HREF="javascript:popUp('09uvc12.gif')"><B>FIG. 9.12</B></A><B> </B><I>Use the
Resource Symbols dialog box to add new command IDs to your application.</I></P>
<P><A HREF="javascript:popUp('09uvc13.gif')"><B>FIG. 9.13</B></A><B> </B><I>Type
the new ID's name and value into the New Symbol dialog box.</I></P>
<P>Click the OK and Close buttons to finalize your selections, and your new command
ID is defined.</P>
<P>
<H3><A NAME="Heading7"></A>Creating the Default String</H3>
<P>You have now defined a resource ID, but it isn't being used. To represent a status
bar pane, the ID must have a default string defined for it. To define the string,
first go to the ResourceView window (by clicking the ResourceView tab in the workspace
pane) and double-click the String Table resource to open it in the string table editor,
as shown in Figure 9.14.</P>
<P>Now, choose Insert, New String to open the String Properties dialog box. Type
the new pane's command ID <B>ID_MYNEWPANE</B> into the ID box (or choose it from
the drop-down list) and the default string (<B>Default string</B> in this case) into
the Caption box (see Figure 9.15).</P>
<P>
<H3><A NAME="Heading8"></A>Adding the ID to the Indicators Array</H3>
<P>When MFC constructs your status bar, it uses an array of IDs to determine which
panes to display and where to display them. This array of IDs is passed as an argument
to the status bar's SetIndicators() member function, which is called in the CMainFrame
class's OnCreate() function. You find this array of IDs, shown in Listing 9.3, near
the top of the MainFrm.cpp file. One way to reach these lines in the source code
editor is to switch to ClassView, expand CMainFrame, double-click OnCreate(), and
scroll up one page. Alternatively, you could use FileView to open MainFrm.cpp and
scroll down to this code.</P>
<P><A HREF="javascript:popUp('09uvc14.gif')"><B>FIG. 9.14</B></A><B> </B><I>Define
the new pane's default string in the string table.</I></P>
<P><A HREF="javascript:popUp('09uvc15.gif')"><B>FIG. 9.15</B></A><B> </B><I>Use the
String Properties dialog box to define the new pane's default string.</I></P>
<P>
<H4>Listing 9.3&#160;&#160;MainFrm.cpp--The Indicator Array</H4>
<PRE>static UINT indicators[] =
{
    ID_SEPARATOR,           // status line indicator
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
</PRE>
<PRE>};
</PRE>
<P>To add your new pane to the array, type the pane's ID into the array at the position
in which you want it to appear in the status bar, followed by a comma. (The first
pane, ID_SEPARATOR, should always remain in the first position.) Listing 9.4 shows
the indicator array with the new pane added.</P>
<P>
<H4>Listing 9.4&#160;&#160;MainFrm.cpp--The Expanded Indicator Array</H4>
<PRE>static UINT indicators[] =
{
    ID_SEPARATOR,           // status line indicator
    ID_MYNEWPANE,
    ID_INDICATOR_CAPS,
    ID_INDICATOR_NUM,
    ID_INDICATOR_SCRL,
</PRE>
<PRE>};
</PRE>
<H3><A NAME="Heading9"></A>Creating the Pane's Command-Update Handler</H3>
<P>MFC doesn't automatically enable new panes when it creates the status bar. Instead,
you must create a command-update handler for the new pane and enable the pane yourself.
(You first learned about command-update handlers in Chapter 4, &quot;Messages and
Commands.&quot;) Also, for most applications, the string displayed in the pane is
calculated on-the-fly--the default string you defined in an earlier step is only
a placeholder.</P>
<P>Normally, you use ClassWizard to arrange for messages to be caught, but ClassWizard
doesn't help you catch status bar messages. You must add the handler entries to the
message map yourself and then add the code for the handler. You add entries to the
message map in the header file and the map in the source file, and you add them outside
the special AFX_MSG_MAP comments used by ClassWizard.</P>
<P>Double-click CMainFrame in ClassView to open the header file, and scroll to the
bottom. Edit the message map so that it resembles Listing 9.5. When you write your
own applications, you will use a variety of function names to update status bar panes,
but the rest of the declaration will always be the same.</P>
<P>
<H4>Listing 9.5&#160;&#160;MainFrm.h--Message Map</H4>
<PRE>// Generated message map functions
protected:
     //{{AFX_MSG(CMainFrame)
     afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
          // NOTE - the ClassWizard will add and remove member functions here.
          //    DO NOT EDIT what you see in these blocks of generated code!
     //}}AFX_MSG
     afx_msg void OnUpdateMyNewPane(CCmdUI *pCmdUI);
</PRE>
<PRE>     DECLARE_MESSAGE_MAP()

⌨️ 快捷键说明

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