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

📄 chxe.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Appendix E</TITLE>
<LINK REL="Next" HREF="chxg.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/chxg.htm">
<LINK REL="Previous" HREF="chxd.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/chxd.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">

<H2><B>Appendix E</B></H2>
<H2><B>Useful Classes</B></H2>

<hr>
<ul>
<li> <B>How to create array, list, and map collection objects</B></P>
<P>  By using MFC's ready-to-use collection classes, you gain more power over mundane data structures.</P>
<li> <B>How to add, remove, and modify collection elements</B></P>

<P>  The MFC collection classes provide member functions for modifying the contents of a collection.</P>
<li> <B>How to iterate over collections</B></P>
<P>  When you need to search through a collection, use these iteration member functions.</P>
<li> 
<B>How to create your own type of collection class</B></P>
<P>  You're not limited to MFC's collection classes. You can roll your own any time you need them.</P>
<li> <B>How to use CString</B> </P>
<P>  When you need to work with character strings, MFC's 
<font color="#008000">CString</font> class is the one for you!</P>
<li> <B>How to use the time classes</B></P>
<P>  Many programs need to manage the time and date. MFC's <font color="#008000">Date</font> class is perfect for this task.</P>
</ul>
<P>MFC 
includes a lot more than classes for programming the Windows graphical user interface. It also features many utility classes for handling such things as lists, arrays, times and dates, and mapped collections. By using these classes, you can gain extra 
power over data in your programs, as well as simplify many operations involved in using complex data structures such as lists.</P>
<P>For example, because MFC's array classes can change their size dynamically, you are relieved of creating oversized arrays 
in an attempt to ensure that the arrays are large enough for the application. In this way, you save memory. The other collection classes provide many other similar conveniences.</P>
<H3><B>The Array Classes</B></H3>
<P>MFC's array classes enable you to 
create and manipulate one-dimensional array objects that can hold virtually any type of data. These array objects work much like the standard arrays that you're used to using in your programs, except that MFC can enlarge or shrink an array object 
dynamically at runtime. This means that you don't have to be concerned with dimensioning your array just right when it's declared. Because MFC's arrays can grow dynamically, you can forget about the memory waste that often occurs with conventional arrays, 
which must be dimensioned to hold the maximum number of elements that may be needed in the program, whether or not you actually use every element.</P>
<P>The array classes include <font color="#008000">CByteArray</font>, <font 
color="#008000">CDWordArray</font>, <font color="#008000">CObArray</font>, <font color="#008000">CPtrArray</font>, <font color="#008000">CUIntArray</font>, <font color="#008000">CWordArray</font>, and <font color="#008000">CStringArray</font>. As you can 
tell from the class names, each class is designed to hold a specific type of data. For example, the <font color="#008000">CUIntArray</font>, which will be used in this section's examples, is an array class that can hold unsigned integers. The <font 
color="#008000">CPtrArray</font> class, on the other hand, represents an array of pointers to <font color="#008000">void</font>, and the <font color="#008000">CObArray</font> class represents an array of objects. The array classes are all almost identical, 
differing only in the type of data that they store. Once you've learned to use one of the array classes, you've learned to use them all. Table E.1 lists the member functions of the array classes and their descriptions.</P>
<P><B>Table E.1&#151;Member 
Functions of the Array Classes</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Function</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>
<pre><font color="#008000">Add()</font></pre>
<TD>
<P>Appends a value to the end of the array, increasing the size of the array 
as needed.</P>
<TR>
<TD>
<pre><font color="#008000">ElementAt()</font></pre>
<TD>
<P>Gets a reference to an array element's pointer.</P>
<TR>
<TD>
<pre><font color="#008000">FreeExtra()</font></pre>
<TD>
<P>Releases unused array memory.</P>
<TR>
<TD>

<pre><font color="#008000">GetAt()</font></pre>
<TD>
<P>Gets the value at the specified array index.</P>
<TR>
<TD>
<pre><font color="#008000">GetSize()</font></pre>
<TD>
<P>Gets the number of elements in the array.</P>
<TR>
<TD>
<pre><font 
color="#008000">GetUpperBound()</font></pre>
<TD>
<P>Gets the array's <I>upper bound,</I> which is the highest valid index at which a value can be stored.</P>
<TR>
<TD>
<pre><font color="#008000">InsertAt()</font></pre>
<TD>
<P>Inserts a value at the 
specified index, shifting existing elements upward as necessary to accommodate the insert.</P>
<TR>
<TD>
<pre><font color="#008000">RemoveAll()</font></pre>
<TD>
<P>Removes all the array's elements.</P>
<TR>
<TD>
<pre><font 
color="#008000">RemoveAt()</font></pre>
<TD>
<P>Removes the value at the specified index.</P>
<TR>
<TD>
<pre><font color="#008000">SetAt()</font></pre>
<TD>
<P>Places a value at the specified index. Because this function will not increase the size of the 
array, the index must be currently valid.</P>
<TR>
<TD>
<pre><font color="#008000">SetAtGrow()</font></pre>
<TD>
<P>Places a value at the specified index, increasing the size of the array as needed.</P>
<TR>
<TD>
<pre><font 
color="#008000">SetSize()</font></pre>
<TD>
<P>Sets the array's initial size and the amount by which it grows when needed. By allocating more than one element&#146;s worth of space at a time, you save time but could waste memory.</P></TABLE>

<P><B>Introducing the Array Application</B></P>
<P>To illustrate how the array classes work, this chapter includes the Array application. After a brief tour of the application, you&#146;ll see how to build it yourself in the sections that follow. When you 
run the program, you see the window shown in Figure E.1. The window displays the current contents of the array. Because the application's array object (which is an instance of <font color="#008000">CUIntArray</font>) starts off with ten elements, the 
values for these elements (indexed as 0 through 9) are displayed on the screen. The application enables you to change, add, or delete elements in the array and see the results.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P>This application is on the CD in the 
RefE folder.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<A HREF="IIfig01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig01.gif"><b>Fig. E.1</b></A>
<P><I>The Array Demo application enables you to experiment with MFC's array </I><I>classes.</I></P>
<P>You can add an element to the array in 
several ways. To see these choices, click in the application's window. The dialog box shown in Figure E.2 appears. Type an array index in the Index box and the new value in the Value box. Then select whether you want to set, insert, or add the element. 
When you choose Set, the value of the element you specify in the Index field gets changed to the value in the Value field. The Insert operation creates a new array element at the location specified by the index, pushing succeeding elements forward. 
Finally, the Add operation just tacks the new element onto the end of the array. In this case, the program ignores the Index field of the dialog box.</P>
<A HREF="IIfig02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig02.gif"><b>Fig. E.2</b></A>
<P><I>The Add to Array dialog box enables you to 
add elements to the array.</I></P>
<P>Suppose, for example, that you enter 3 into the dialog box's Index field and 15 into the Value field, leaving the Set radio button selected. Figure E.3 shows the result: the program has placed the value 15 into element 
3 of the array, overwriting the value that was there previously. Now type 2 into Index, 25 into Value, select the Insert radio button, and click OK. Figure E.4 shows the result: the program stuffs a new element into the array, shoving the other elements 
forward.</P>
<A HREF="IIfig03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig03.gif"><b>Fig. E.3</b></A>
<P><I>The value of 15 has been placed into array element 3.</I></P>
<A HREF="IIfig04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig04.gif"><b>Fig. E.4</b></A>
<P><I>The screen now shows the new array element, giving 
</I><I>11</I><I> elements in all.</I></P>
<P>An interesting thing to try&#151;something that really shows how dynamic MFC's arrays are&#151;is to set an array element beyond the end of the array. For example, given the program's state shown in Figure E.4, 
if you type 20 in Index and 45 in Value and then choose the Set radio button, you get the results shown in Figure E.5. Because there was no element 20, the array class created the new elements that it needed to get to 20. You don&#146;t need to keep track 
of how many elements are in the array. Try that with an old-fashioned array.</P>
<A HREF="IIfig05.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig05.gif"><b>Fig. E.5</b></A>
<P><I>The array class has added the elements needed in order to set element </I><I>20.</I></P>
<P>Besides adding new 
elements to the array, you can also delete elements in one of two ways. To do this, first right-click in the window. When you do, you see the dialog box shown in Figure E.6. If you type an index into the Remove field and then click OK, the program deletes 
the selected element from the array. This is the opposite of the effect of Insert command, because the Remove command shortens the array rather than lengthening it. If you want, you can select the Remove All option in the dialog box. Then the program 
deletes all elements from the array, leaving it empty.</P>
<A HREF="IIfig06.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/IIfig06.gif"><b>Fig. E.6</b></A>
<P><I>The Remove from Array dialog box enables you to delete elements from the </I><I>array.</I></P>
<P><B>Declaring and Initializing the 
Array</B></P>
<P>Now you'd probably like to see how all this array trickery works. It's really pretty simple. First, the program declares the array object as a data member of the view class, like this:</P>
<pre><font color="#008000">CUIntArray 
array;</font></pre>
<P>Then, in the view class's constructor, the program initializes the array to ten elements:</P>
<pre><font color="#008000">array.SetSize(10, 5);</font></pre>
<P>The <font color="#008000">SetSize()</font> function takes as parameters 
the number of elements to give the array initially and the number of elements by which the array should grow whenever it needs to. You don't need to call <font color="#008000">SetSize()</font> in order to use the array class. However, if you fail to do so, 
MFC adds elements to the array one at a time, as needed, which is a slow process (although, unless you're doing some heavy processing, you're not likely to notice any difference in speed.) If your application doesn&#146;t add elements to its arrays often, 
and you are concerned about memory consumption, don&#146;t use <font color="#008000">SetSize()</font>. If your application repeatedly adds elements, and you have lots of memory available, using <font color="#008000">SetSize()</font> to arrange for many 
elements to be allocated at once will reduce the number of allocations performed, giving you a faster application.</P>
<P><B>Adding Elements to the Array</B></P>
<P>After setting the array size, the program waits for the user to click the left or right 
mouse buttons in the window. When the user does, the program springs into action, displaying the appropriate dialog box and processing the values entered into the dialog box. Listing E.1 shows the Array Demo application's <font 
color="#008000">OnLButtonDown()</font> function, which handles the left mouse button clicks.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P><A HREF="index04.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index04.htm" target="text">Chapter 4</A>, &#147;Messages and Commands,&#148; shows you how to catch mouse clicks 
and arrange for a message-handler like <font color="#008000">OnLButtonDown()</font> to be called.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P><B>Listing E.1&#151;</B><B><I>CArrayView::OnLButtonDown()</I></B></P>
<pre><font color="#008000">void 
CArrayView::OnLButtonDown(UINT nFlags, CPoint point) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">    ArrayAddDlg dialog(this);</font></pre>
<pre><font color="#008000">    dialog.m_index = 0;</font></pre>
<pre><font 
color="#008000">    dialog.m_value = 0;</font></pre>
<pre><font color="#008000">    dialog.m_radio = 0;</font></pre>
<pre><font color="#008000">    int result = dialog.DoModal();</font></pre>
<pre><font color="#008000">    if (result == IDOK)</font></pre>

<pre><font color="#008000">    {</font></pre>
<pre><font color="#008000">        if (dialog.m_radio == 0)</font></pre>
<pre><font color="#008000">            array.SetAtGrow(dialog.m_index, dialog.m_value);</font></pre>
<pre><font color="#008000">        
else if (dialog.m_radio == 1)</font></pre>
<pre><font color="#008000">            array.InsertAt(dialog.m_index, dialog.m_value, 1);</font></pre>
<pre><font color="#008000">        else</font></pre>
<pre><font color="#008000">            
array.Add(dialog.m_value);</font></pre>
<pre><font color="#008000">        Invalidate();</font></pre>
<pre><font color="#008000">    }</font></pre>
<pre><font color="#008000">    CView::OnLButtonDown(nFlags, point);</font></pre>
<pre><font 

⌨️ 快捷键说明

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