📄 ch03.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Chapter 3</TITLE>
<LINK REL="Next" HREF="ch04.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch04.htm">
<LINK REL="Previous" HREF="ch02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch02.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><A ID="I1" NAME="I1"><B>Chapter 3</B></A></H2>
<H2><A ID="I2" NAME="I2"><B>Windows 95 Common Controls</B></A></H2>
<hr>
<P>As a Windows user, you're accustomed to seeing controls like buttons, list boxes, menus, and edit boxes. As Windows developed, however, Microsoft noticed that developers routinely created other types of controls in their programs. These controls
included toolbars, status bars, progress bars, tree views, and others. To make life easier for Windows programmers, Microsoft included these popular controls as part of the Windows 95 operating environment (and the latest version of Windows NT). Now,
Windows programmers no longer need to create their own versions of these controls from scratch. This chapter introduces you to many of Windows 95's common controls. The toolbar and status bar controls are covered in <A HREF="index10.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index10.htm"
target="text">Chapter 10</A>, "Status Bars and Toolbars," and property sheets and wizards are covered in <A HREF="index12.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index12.htm" target="text">Chapter 12</A>, "Property Pages and Sheets and Wizards."</P>
<ul>
<li> <B>About progress bar controls</B></P>
<P> Progress bars provide feedback to the user about the status of a long operation.</P>
<li> <B>About slider controls</B></P>
<P> Slider controls (also known as trackbars) enable users to select values within a given range.</P>
<li> <B>About up-down controls</B></P>
<P> Up-Down controls (or spinners) are another way a user can specify a value from a given range.</P>
<li> <B>About image list controls</B></P>
<P> Many Windows 95 controls use a set of images. An Image List holds those images.</P>
<li> <B>About list view controls</B></P>
<P> List View controls provide a configurable view of items in a data set.</P>
<li> <B>About tree view controls</B></P>
<P> Tree View controls display a hierarchical view of data elements.</P>
<li> <B>About rich edit controls</B></P>
<P> Rich Edit controls enable you quickly to add word-processing capabilities to your application.</P>
</ul>
<H3><A ID="I3" NAME="I3"><B>The Win95 Controls Application</B></A></H3>
<P>This chapter's sample program is called Win95 Controls App. It demonstrates six of the Windows 95 common controls: the progress bar, the slider, the up-down control, the list view, the tree view, and the rich edit control, all of which are shown in
Figure 3.1. In the following sections, you learn the basics of creating and using these controls in your own applications. Because this sample application uses some techniques that have not yet been covered, the sample program is presented as a
demonstration rather than as a build-along project. You should open the code in Developer Studio and look through it as you read.</P>
<A HREF="Dfigs01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch03/Dfigs01.gif"><b>Fig. 3.1</b></A>
<P><I>The Win95 sample application demonstrates six Windows 95 common controls.</I></P>
<P>The controls themselves are declared as data members of the view class, by adding the lines in Listing 3.1 to Win95View.h. As you can see, the progress bar is an object of the <font color="#008000">CProgressCtrl</font> class. It is discussed in the
next section. The other controls will be discussed in later sections of this chapter.</P>
<P><I>Listing 3.1— </I><I>Win95View.h—Declaring the Controls</I></P>
<pre><font color="#008000">protected:</font></pre>
<pre><font color="#008000"> </font><font color="#008000">//Progress Bar</font></pre>
<pre><font color="#008000"> CProgressCtrl m_progressBar;</font></pre>
<pre><font color="#008000"> </font><font color="#008000">//Trackbar or Slider</font></pre>
<pre><font color="#008000"> CSliderCtrl m_trackbar;</font></pre>
<pre><font color="#008000"> BOOL m_timer;</font></pre>
<pre><font color="#008000"> </font><font color="#008000">// Up-Down or Spinner</font></pre>
<pre><font color="#008000"> CSpinButtonCtrl m_upDown;</font></pre>
<pre><font color="#008000"> CEdit m_buddyEdit;</font></pre>
<pre><font color="#008000"> </font><font color="#008000">// List View</font></pre>
<pre><font color="#008000"> CListCtrl m_listView;</font></pre>
<pre><font color="#008000"> CImageList m_smallImageList;</font></pre>
<pre><font color="#008000"> CImageList m_largeImageList;</font></pre>
<pre><font color="#008000"> CButton m_smallButton;</font></pre>
<pre><font color="#008000"> CButton m_largeButton;</font></pre>
<pre><font color="#008000"> CButton m_listButton;</font></pre>
<pre><font color="#008000"> CButton m_reportButton;</font></pre>
<pre><font color="#008000"> </font><font color="#008000">// Tree View</font></pre>
<pre><font color="#008000"> CTreeCtrl m_treeView;</font></pre>
<pre><font color="#008000"> CImageList m_treeImageList;</font></pre>
<pre><font color="#008000"> </font><font color="#008000">// Rich Edit</font></pre>
<pre><font color="#008000"> CRichEditCtrl m_richEdit;</font></pre>
<pre><font color="#008000"> CButton m_boldButton;</font></pre>
<pre><font color="#008000"> CButton m_leftButton;</font></pre>
<pre><font color="#008000"> CButton m_centerButton;</font></pre>
<pre><font color="#008000"> CButton m_rightButton;</font></pre>
<H3><A ID="I4" NAME="I4"><B>The Progress Bar Control</B></A></H3>
<P>Probably the new common control that is easiest to use is the progress bar, which is nothing more than a rectangle that slowly fills in with colored blocks. The more colored blocks filled in, the closer the task is to being complete. When the progress
bar is completely filled in, the task associated with the progress bar is also complete. You might use a progress bar to show the status of a sorting operation or to give the user visual feedback about a large file that's being loaded.</P>
<P>To see a progress bar in action, click anywhere in the background of Win95 Controls App's window. When you do, the progress bar begins filling with colored blocks. When the progress bar is completely filled, it starts over again. This continues until
you click the window again or exit the program. Of course, in this program, the progress bar isn't tracking a real task in progress. It's simply responding to timer messages. The program does, however, still demonstrate how you might use a progress bar in
your own applications.</P>
<P><B><I>Creating the Progress Bar</I></B></P>
<P>Before you can use a progress bar, you must create it. Often in an MFC program, the controls are created as part of a dialog box. However, Win95 Controls App displays its controls in the application's main window, the <I>view</I> of this Single
Document Interface (SDI) application. You will learn more about documents and views in <A HREF="index05.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index05.htm" target="text">Chapter 5</A>, “Documents and Views.” All of the controls are created in the view class <font
color="#008000">OnCreate()</font> function, which responds to the <font color="#008000">WM_CREATE</font> Windows message. </P>
<P>Because creating and initializing each of the six controls requires quite a bit of code, the application delegates the details to separate functions. For example, to create the progress bar control, <font color="#008000">OnCreate()</font> calls the
<font color="#008000">CreateProgressBar()</font> local member function, shown in Listing 3.2, like this:</P>
<pre><font color="#008000">CreateProgressBar();</font></pre>
<P><I>Listing 3.2— </I><I>Win95View.cpp—</I>CWin95View::CreateProgressBar()</P>
<pre><font color="#008000">void CWin95View::CreateProgressBar()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> m_progressBar.Create(WS_CHILD | WS_VISIBLE | WS_BORDER,</font></pre>
<pre><font color="#008000"> CRect(20, 40, 250, 80), this, IDC_PROGRESSBAR);</font></pre>
<pre><font color="#008000"> m_progressBar.SetRange(1, 100);</font></pre>
<pre><font color="#008000"> m_progressBar.SetStep(10);</font></pre>
<pre><font color="#008000"> m_progressBar.SetPos(50);</font></pre>
<pre><font color="#008000"> m_timer = FALSE;</font></pre>
<pre><font color="#008000">}</font></pre>
<p><font color="#008000">CreateProgressBar()</font> first creates the progress bar control by calling the control's <font color="#008000">Create()</font> function. This function's four arguments are the control's style flags, the control's size (as a
<font color="#008000">CRect</font> object), a pointer to the control's parent window, and the control's ID. The resource ID, <font color="#008000">IDC_PROGRESSBAR</font>, is added by hand. To add resource symbols to your own applications, choose
<U>V</U>iew, Resource S<U>y</U>mbols and click the New button. Type in a resource ID name, such as <font color="#008000">IDC_PROGRESSBAR</font>, and use the default number Developer Studio provides.</P>
<P>The style constants are the same constants that you use for creating any type of window (a control is really nothing more than a special kind of window, after all). In this case, you need at least the following:</P>
<ul>
<li> <font color="#008000">WS_CHILD</font>—Indicates that the control is a child window</P>
<li> <font color="#008000">WS_VISIBLE</font>—Ensures that the user can see the control</P>
</ul>
<P>The <font color="#008000">WS_BORDER</font> is a nice addition because it adds a dark border around the control, setting it off from the rest of the window.</P>
<P><B><I>Initializing the Progress Bar</I></B></P>
<P>After the progress bar control is created, it must be initialized. The <font color="#008000">CProgressCtrl</font> class features a number of member functions that enable you to initialize and manipulate the control, as listed in Table 3.1.</P>
<P><I>Table 3.1—Member Functions of the </I>CProgressCtrl<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">Create()</font></pre>
<TD>
<P>Creates the progress bar control</P>
<TR>
<TD>
<pre><font color="#008000">OffsetPos()</font></pre>
<TD>
<P>Advances the control the given number of blocks</P>
<TR>
<TD>
<pre><font color="#008000">SetPos()</font></pre>
<TD>
<P>Sets the control's current value</P>
<TR>
<TD>
<pre><font color="#008000">SetRange()</font></pre>
<TD>
<P>Sets the control's minimum and maximum values</P>
<TR>
<TD>
<pre><font color="#008000">SetStep()</font></pre>
<TD>
<P>Sets the value by which the control advances</P>
<TR>
<TD>
<pre><font color="#008000">StepIt()</font></pre>
<TD>
<P>Advances the control by a one-step unit</P></TABLE>
<P>To initialize the control, <font color="#008000">CWin95View::CreateProgressBar()</font> calls <font color="#008000">SetRange()</font>, <font color="#008000">SetStep()</font>, and <font color="#008000">SetPos()</font>.Because the range and the step rate
are related, a control with a range of 1-10 and a step rate of 1 works almost identically to a control with a range of 1-100 and a step rate of 10.</P>
<P>When this demonstration application starts, the progress bar is already half filled with colored blocks. (This is purely for aesthetic reasons. Usually a progress bar begins its life empty.) It's half full because <font
color="#008000">CreateProgressBar()</font> calls <font color="#008000">SetPos()</font> with the value of 50, which is the midpoint of the control's range.</P>
<P><B><I> Manipulating the Progress Bar</I></B></P>
<P>In Win95 Controls App, the progress bar starts counting forward when you click the window's background. This is because the program responds to the mouse click by starting a timer that sends <font color="#008000">WM_TIMER</font> messages to the program
two times per second. In the view class's <font color="#008000">OnTimer()</font> function, the program makes the following function call:</P>
<pre><font color="#008000">m_progressBar.StepIt();</font></pre>
<P>The <font color="#008000">StepIt()</font> function increments the progress bar control's value by the step rate, causing new blocks to be displayed in the control as the control's value setting counts upward. When the control reaches its maximum, it
automatically starts over.</P>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>Notice that there are no <font color="#008000">CProgressCtrl</font> member functions that control the size or number of blocks that will fit into the control. This attribute is controlled indirectly by the size of the control.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<H3><A ID="I5" NAME="I5"><B>The Slider Control</B></A></H3>
<P>Many times in a program you might need the user to enter a value within a specific range. For this sort of task, you use MFC's <font color="#008000">CSliderCtrl</font> class to create a slider (also called trackbar) control. For example, suppose you
need the user to enter a percentage. In this case, you want the user to enter values only in the range from 0 to 100. Other values would be invalid and could cause problems in your program.</P>
<P>By using the slider control, you can force the user to enter a value in the specified range. Although the user can accidentally enter a wrong value (a value that doesn't accomplish what the user wants to do), there is no way to enter an invalid value
(one that brings your program crashing down like a stone wall in an earthquake).</P>
<P>For a percentage, you create a slider control with a minimum value of 0 and a maximum value of 100. Moreover, to make the control easier to position, you might want to place tick marks at each setting that's a multiple of 10, providing 11 tick marks in
all (including the one at 0). Win95 Controls App creates exactly this type of slider.</P>
<P>To see the slider work, click the slider's slot. When you do, the slider moves forward or backward, and the selected value appears to the right of the control's caption, as seen in Figure 3.1. As soon as the slider has the focus, you can also control
it with your keyboard's Up and Down arrow keys, as well as with the Page Up and Page Down keys.</P>
<P><B><I> Creating the Trackbar</I></B></P>
<P>In CWin95View, the slider is created in the <font color="#008000">CreateTrackbar()</font> local member function shown in Listing 3.3. Like <font color="#008000">CreateProgressBar()</font>, this function is called from <font
color="#008000">CWin95View::OnCreate()</font>.</P>
<P><I>Listing 3.3—</I><I>Win95View.cpp—</I>CWin95View::CreateTrackBar()</P>
<pre><font color="#008000">void CWin95View::CreateTrackbar()</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000"> m_trackbar.Create(WS_CHILD | WS_VISIBLE | WS_BORDER |</font></pre>
<pre><font color="#008000"> TBS_AUTOTICKS | TBS_BOTH | TBS_HORZ,</font></pre>
<pre><font color="#008000"> CRect(270, 40, 450, 80), this, IDC_TRACKBAR);</font></pre>
<pre><font color="#008000"> m_trackbar.SetRange(0, 100, TRUE);</font></pre>
<pre><font color="#008000"> m_trackbar.SetTicFreq(10);</font></pre>
<pre><font color="#008000"> m_trackbar.SetLineSize(1);</font></pre>
<pre><font color="#008000"> m_trackbar.SetPageSize(10);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>As with the progress bar, the first step is to create the slider control by calling its <font color="#008000">Create()</font> member function. This function's four arguments are the control's style flags, the control's size (as a <font
color="#008000">CRect</font> object), a pointer to the control's parent window, and the control's ID. The style constants include the same constants that you would use for creating any type of window, with the addition of special styles used with sliders.
Table 3.2 lists these special styles.</P>
<P><I>Table 3.2—Slider Styles</I></P>
<TABLE BORDER>
<TR>
<TD>
<P><B>Style</B></P>
<TD>
<P><B>Description</B></P>
<TR>
<TD>
<pre><font color="#008000">TBS_AUTOTICKS</font></pre>
<TD>
<P>Enables the slider to automatically draw its tick marks</P>
<TR>
<TD>
<pre><font color="#008000">TBS_BOTH</font></pre>
<TD>
<P>Draws tick marks on both sides of the slider</P>
<TR>
<TD>
<pre><font color="#008000">TBS_BOTTOM</font></pre>
<TD>
<P>Draws tick marks on the bottom of a horizontal slider</P>
<TR>
<TD>
<pre><font color="#008000">TBS_ENABLESELRANGE</font></pre>
<TD>
<P>Enables a slider to display a sub-range of values</P>
<TR>
<TD>
<pre><font color="#008000">TBS_HORZ</font></pre>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -