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

📄 chxe.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<P>Much of this <font color="#008000">OnDraw()</font> function is similar to other versions that you've seen in this chapter. The map iteration, however, begins when the program 
calls the map object's <font color="#008000">GetStartPosition()</font> member function, which returns a position value for the first entry in the map (not necessarily the first entry that you added to the map). Inside a <font color="#008000">while</font> 
loop, the program calls the map object's <font color="#008000">GetNextAssoc()</font> member function, giving the position returned from <font color="#008000">GetStartPosition()</font> as the single argument. <font color="#008000">GetNextAssoc()</font> 
retrieves the key and value at the given position and then updates the position to the next element in the map. When the position value becomes <font color="#008000">NULL</font>, the program has reached the end of the map.</P>
<H3><B>Collection Class 
Templates</B></H3>
<P>MFC includes class templates that you can use to create your own special types of collection classes. (For more information on templates, please refer to the section &quot;Templates&quot; in <A HREF="index26.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index26.htm" target="text">Chapter 
26</A>, &quot; Exceptions, Templates, and the Latest Additions to C++.&quot;) Although the subject of templates can be complex, using the collection class templates is easy enough. For example, suppose that you want to create an array class that can hold 
structures of the type shown in Listing E.13.</P>
<P><B>Listing E.13&#151;A Sample Structure</B></P>
<pre><font color="#008000">struct MyValues</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">    int value1;</font></pre>

<pre><font color="#008000">    int value2;</font></pre>
<pre><font color="#008000">    int value3;</font></pre>
<pre><font color="#008000">};</font></pre>
<P>The first step is to use the template to create your class, like this:</P>
<pre><font 
color="#008000">CArray&lt;MyValues, MyValues&amp;&gt; myValueArray;</font></pre>
<P>Here, <font color="#008000">CArray</font> is the template you use for creating your own array classes. The template's two arguments are the type of data to store in the 
array and the type of data that the new array class's member functions should use as arguments where appropriate. In this case, the type of data to store in the array is structures of the <font color="#008000">MyValues</font> type. The second argument 
specifies that class member functions should expect references to <font color="#008000">MyValues</font> structures as arguments where needed.</P>
<P>To build your array, you optionally set the array's initial size:</P>
<pre><font 
color="#008000">myValueArray.SetSize(10, 5);</font></pre>
<P>Then you can start adding elements to the array, like this:</P>
<pre><font color="#008000">MyValues myValues;</font></pre>
<pre><font color="#008000">myValueArray.Add(myValues);</font></pre>

<P>Once you create your array class from the template, you use the array just as you do any of MFC's array classes, as described earlier in this chapter. Other collection class templates you can use are <font color="#008000">CList</font> and <font 
color="#008000">Cmap</font>. This means you can take advantage of all the design work put in by the MFC team to create an array of Employee objects, or a linked list of Order objects, or a map linking names to Customer objects.</P>
<H3><B>The String 
Class</B></H3>
<P>There are few programs that don't have to deal with text strings of one sort or another. Unfortunately, C++ is infamous for its weak string-handling capabilities, while languages like BASIC and Pascal have always enjoyed superior power 
when it came to these ubiquitous data types. MFC's <font color="#008000">CString</font> class addresses C++'s string problems by providing member functions that are as handy to use as those found in other languages. Table E.4 lists the commonly used member 
functions of the <font color="#008000">CString</font> class.</P>
<P><B>Table E.4&#151;Commonly Used Member Functions of the </B><B><I>CString</I></B><B> Class</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Function</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>

<pre><font color="#008000">Compare()</font></pre>
<TD>
<P>Case-sensitive compare of two strings.</P>
<TR>
<TD>
<pre><font color="#008000">CompareNoCase()</font></pre>
<TD>
<P>Case-insensitive compare of two strings.</P>
<TR>
<TD>
<pre><font 
color="#008000">Empty()</font></pre>
<TD>
<P>Clears a string.</P>
<TR>
<TD>
<pre><font color="#008000">Find()</font></pre>
<TD>
<P>Locates a substring.</P>
<TR>
<TD>
<P>Format()</P>
<TD>
<P>&#147;Print&#148; variables into a CString much like the C <font 
color="#008000">sprintf</font> function.</P>
<TR>
<TD>
<pre><font color="#008000">GetAt()</font></pre>
<TD>
<P>Gets a character at a specified position in the string.</P>
<TR>
<TD>
<pre><font color="#008000">GetBuffer()</font></pre>
<TD>
<P>Gets a pointer 
to the string's contents.</P>
<TR>
<TD>
<pre><font color="#008000">GetLength()</font></pre>
<TD>
<P>Gets the number of characters in the string.</P>
<TR>
<TD>
<pre><font color="#008000">IsEmpty()</font></pre>
<TD>
<P>Returns <font 
color="#008000">TRUE</font> if the string holds no characters.</P>
<TR>
<TD>
<pre><font color="#008000">Left()</font></pre>
<TD>
<P>Gets the left segment of a string.</P>
<TR>
<TD>
<pre><font color="#008000">MakeLower()</font></pre>
<TD>
<P>Lowercases a 
string.</P>
<TR>
<TD>
<pre><font color="#008000">MakeReverse()</font></pre>
<TD>
<P>Reverses the contents of a string.</P>
<TR>
<TD>
<pre><font color="#008000">MakeUpper()</font></pre>
<TD>
<P>Uppercases a string.</P>
<TR>
<TD>
<pre><font 
color="#008000">Mid()</font></pre>
<TD>
<P>Gets the middle segment of a string.</P>
<TR>
<TD>
<pre><font color="#008000">Right()</font></pre>
<TD>
<P>Gets the right segment of a string.</P>
<TR>
<TD>
<pre><font color="#008000">SetAt()</font></pre>
<TD>

<P>Sets a character at a specified position in the string.</P>
<TR>
<TD>
<pre><font color="#008000">TrimLeft()</font></pre>
<TD>
<P>Removes leading white-space characters from a string.</P>
<TR>
<TD>
<pre><font color="#008000">TrimRight()</font></pre>
<TD>

<P>Removes trailing white-space characters from a string.</P></TABLE>
<P>Besides the functions listed in the table, the <font color="#008000">CString</font> class also defines a full set of operators for dealing with strings. Using these operators, you can 
do things like <I>concatenate</I> (join together) strings with the plus sign (<font color="#008000">+</font>), assign values to a string object with the equal sign (<font color="#008000">=</font>), access the string as a C-style string with the <font 
color="#008000">LPCTSTR</font> operator, and more.</P>
<P>Creating a string object is quick and easy, like this:</P>
<pre><font color="#008000">CString str = &quot;This is a test string&quot;;</font></pre>
<P>Of course, there are lots of ways to construct 
your string object. The previous example is only one possibility. You can create an empty string object and assign characters to it later, you can create a string object from an existing string object, and you can even create a string from a repeating 
character.</P>
<P>Once you have the string object created, you can call its member functions and manipulate the string in a number of ways. For example, to convert all the characters in the string to uppercase, you'd make a function call like this:</P>

<pre><font color="#008000">str.MakeUpper();</font></pre>
<P>To lengthen a string, use the <font color="#008000">+</font> or <font color="#008000">+=</font> operators, like this:</P>
<pre><font color="#008000">CString sentence = &quot;hello &quot; + 
str;</font></pre>
<pre><font color="#008000">sentence += &quot; there.&quot;</font></pre>
<P>Or, to compare two strings, you'd make a function call something like this:</P>
<pre><font color="#008000">str.Compare(&quot;Test String&quot;);</font></pre>

<P>You can also compare two <font color="#008000">CString</font> objects:</P>
<pre><font color="#008000">CString testStr = &quot;Test String&quot;;</font></pre>
<pre><font color="#008000">str.Compare(testStr);</font></pre>
<P>Or neater still:</P>

<pre><font color="#008000">if (testStr == str)</font></pre>
<P>If you peruse your online documentation, you'll find that most of the other <font color="#008000">CString</font> member functions are equally easy to use.</P>
<H3><B>The Time Classes</B></H3>

<P>If you've ever tried to manipulate time values returned from a computer, you'll be pleased to learn about MFC's <font color="#008000">CTime</font> and <font color="#008000">CTimeSpan</font> classes, which represent absolute times and elapsed times, 
respectively. The use of these classes is pretty straightforward, so there's no sample program for this section. However, the following sections get you started with these handy classes. Before you start working with the time classes, however, look over 
Table E.5, which lists the member functions of the <font color="#008000">CTime</font> class, and Table E.6, which lists the member functions of the <font color="#008000">CTimeSpan</font> class..</P>
<P><B>Table E.5&#151;Member Functions of the 
</B><B><I>CTime</I></B><B> Class</B></P>
<TABLE BORDER>
<TR>
<TD>
<P><I>Function</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>
<pre><font color="#008000">Format()</font></pre>
<TD>
<P>Constructs a string representing the time object's time.</P>
<TR>

<TD>
<pre><font color="#008000">FormatGmt()</font></pre>
<TD>
<P>Constructs a string representing the time object's GMT (or UTC) time. This is the Greenwich mean time.</P>
<TR>
<TD>
<pre><font color="#008000">GetCurrentTime()</font></pre>
<TD>
<P>Creates a 

⌨️ 快捷键说明

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