📄 apf.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;CHARSET=iso-8859-1"><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 += '</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="/includes/stylesheets/ebooks.css"> <META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1.1"> <TITLE>Teach Yourself Visual C++ 6 in 21 Days -- Appendix F -- Using MFC's Helper Classes</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR>Teach Yourself Visual C++ 6 in 21 Days</H1><CENTER><P><A HREF="../ape/ape.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../index.htm"><IMGSRC="../button/contents.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Contents"BORDER="0"></A> <HR></CENTER><H1 ALIGN="CENTER">- F -<BR>Using MFC's Helper Classes</H1><H1></H1><UL> <LI><A HREF="#Heading1">Using the Collection Classes</A> <UL> <LI><A HREF="#Heading2">Using the Array Classes</A> <LI><A HREF="#Heading3">Using the List Classes</A> <LI><A HREF="#Heading4">Using the Map Classes</A> <LI><A HREF="#Heading5">ÂGetNext(pos)->m_dDiameter);</A> </UL> <LI><A HREF="#Heading6">Using the Coordinate-Handling Classes</A> <UL> <LI><A HREF="#Heading7">Using the CPoint Class</A> <LI><A HREF="#Heading8">Using the CRect Class</A> <LI><A HREF="#Heading9">ÂAfxMessageBox("Hit! - ptTest2");</A> <LI><A HREF="#Heading10">Using the CSize Class</A> </UL> <LI><A HREF="#Heading11">Using the Time-Handling Classes</A> <UL> <LI><A HREF="#Heading12">Using the COleDateTime Class</A> <LI><A HREF="#Heading13">Using the COleDateTimeSpan Class</A> </UL> <LI><A HREF="#Heading14">Using the String Manipulation Class</A> <UL> <LI><A HREF="#Heading15">Using the CString Class</A> <LI><A HREF="#Heading16">String Manipulation</A> <LI><A HREF="#Heading17">Searching Strings</A> <LI><A HREF="#Heading18">Formatting Text for Display</A> </UL></UL><P><HR SIZE="4"><BR><I>by Jon Bates</I></P><P><H2><A NAME="Heading1"></A>Using the Collection Classes</H2><P>You'll quickly find that in any object-oriented program you write, objects mustbe grouped and stored into collections of different types and sizes. Once again,the MFC comes to the rescue with sets of easy-to-use classes and templates to helpwith this common requirement.</P><P>The collection classes fall into three broad categories--arrays, maps, and lists--thathave their particular niches.</P><P>Arrays are the mainstay of collection classes and are useful for implementingobject containers. Each object in an array has a zero-based position or index, whichis used to locate and reference the object.</P><P>Lists are useful when you want to think of your data as being linked sequentially,each item to the next. They are useful when you must quickly add or remove itemsto the head or tail of the list. You can also traverse the list forward or backwardfrom one item to the next.</P><P>Maps are used to associate data against a key item such as a string or numberwhere the associations are sparse and random. For example, you might use a map toassociate objects with zip or postal codes. They are good at fast retrieval of objectsgiven the association key and can be used as short-term data caches for large databases.</P><P><H3><A NAME="Heading2"></A>Using the Array Classes</H3><P>MFC provides several predefined array classes and a generic array template sothat you can create arrays to hold your own customized objects. (The latter is coveredlater in this chapter, in the "Creating Custom Collection Classes" section.)</P><P>Several predefined array classes offer quick and easy array access for commontypes of variables and objects, as shown in Table F.1.</P><P><H4>TABLE F.1. PREDEFINED ARRAY-BASED CLASSES.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Array Class</I></TD> <TD ALIGN="LEFT"><I>Types of Variable Held</I></TD> <TD ALIGN="LEFT"><I>Numeric Range of Type</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CByteArray</TD> <TD ALIGN="LEFT">BYTE--8-bit</TD> <TD ALIGN="LEFT">0 to 255</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">unsigned values</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CWordArray</TD> <TD ALIGN="LEFT">WORD--16-bit</TD> <TD ALIGN="LEFT">0 to 65535</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">unsigned values</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CUIntArray</TD> <TD ALIGN="LEFT">UINT--32-bit</TD> <TD ALIGN="LEFT">0 to 4294967295</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">unsigned integer</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">values</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CDWordArray</TD> <TD ALIGN="LEFT">DWORD--32-bit</TD> <TD ALIGN="LEFT">0 to 4294967295</TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">unsigned integer</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">values</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CStringArray</TD> <TD ALIGN="LEFT">CString--text</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">string objects</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CObArray</TD> <TD ALIGN="LEFT">CObject--any</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">CObject-derived</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">objects</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CPtrArray</TD> <TD ALIGN="LEFT">void*--any object</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">pointers or memory</TD> <TD ALIGN="LEFT"></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"></TD> <TD ALIGN="LEFT">addresses</TD> <TD ALIGN="LEFT"></TD> </TR></TABLE></P><P>There are several member functions for each array class that differ only by thetype of variables they hold. Each function discussed can be used with any array classesto deal with variables of their corresponding type.</P><P>One of the most useful aspects of these array classes is their capability to growdynamically. Normal C/C++ arrays are predefined in size and can be extended onlyby lots of messy reallocations of memory. The collection classes hide these reallocationsso that you can simply call the Add() member of an array object to add a new value.For example, to add strings to a CStringArray, you can use code similar to this:</P><P><PRE>CStringArray myStringArray;myStringArray.Add("Red");myStringArray.Add("Green");myStringArray.Add("Blue");</PRE><P>You can then find the size of an array by calling the GetSize() function; forexample, the following line used after the previous lines would return three itemsinto nNumberOfItems:</P><P><PRE>int nNumberOfItems = myStringArray.GetSize();</PRE><P>You can also set the array to a specific size using the corresponding SetSize()function, which will extend or truncate the array to the specified size you pass.</P><P>Values can be set to the array by using the SetAt() function that passes a zero-basedindex and the value to be stored. SetAt() will assert whether the index is largerthan the current array size. You can then retrieve values from the array using theGetAt() function, which will return the value at the index position that you specify.You might use these functions with a CWordArray like this:</P><P><PRE>CWordArray myWordArray;myWordArray.SetSize(20);myWordArray.SetAt(0,200);myWordArray.SetAt(19,500);TRACE("Value at index position 19 is %d\n", myWordArray.GetAt(19));</PRE><P>These lines will set the first element of a 20-element array to 200 and the lastto 500 and display the value 500 when executed. You can still grow the array by callingthe Add() function and find the uppermost valid index by calling GetUpperBound(),which will return the zero-based index, or -1 if there are no elements present.</P><P>You can use the [ ] operators to set and get values at a specific index just likea normal C++ array. For example, the GetAt() and SetAt() functions in the previouslines could be replaced with the [ ] operators like this:</P><P><PRE>myWordArray[0] = 200;myWordArray[19] = 500;TRACE("Value at index position 19 is %d\n", myWordArray.GetAt[19]);</PRE><P>Using the InsertAt() and RemoveAt() functions, you can insert or remove itemsat a specific position, which results in all the items shifting up or down by oneor more elements.</P><P>The InsertAt() function has two forms; the first needs an index position and anelement to insert there. You can also optionally pass it a count to insert multiplecopies of the specified element. The second form lets you insert another whole arrayat a specified index position.</P><P>The RemoveAt() function needs only one parameter to specify the index value ofthe item to be removed, but you can also optionally pass a count as the second parameterto remove a number of elements. The remaining array elements will then be shifteddown to fill the gap.</P><P>You can remove all the elements of an array by calling the RemoveAll() function.</P><BLOCKQUOTE> <P><HR>MANAGING MEMORY WITH COBARRAY<I> </I>AND CPTRARRAY</P> <P>You must be careful to delete objects that you have allocated with new and stored in a CObArray or CPtrArray because these arrays only hold pointers to the elements (not elements themselves). Therefore, a RemoveAll() call will only remove the pointers to the objects and not free the memory used by the objects themselves. <HR></BLOCKQUOTE><H3><A NAME="Heading3"></A>Using the List Classes</H3><P>There are only three categories of lists as shown in Table F.2 and a templatefor your own types (discussed later). There is seldom any need to have a list ofsimple integer values. Instead, you would probably need a linked list of your ownCObject-derived classes or pointers to a number of C++ classes or structures.</P><P><H4>TABLE F.2. THE LIST-BASED COLLECTION CLASSES.</H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT"><I>Class Name</I></TD> <TD ALIGN="LEFT"><I>Type of Variable Held</I></TD> </TR> <TR ALIGN="LEFT" VALIGN="TOP"> <TD ALIGN="LEFT">CObList</TD> <TD ALIGN="LEFT">CObject--Pointers to any CObject-derived objects.</TD> </TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -