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

📄 chxe.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 5 页
字号:
color="#008000">}</font></pre>
<P>This code starts by creating a dialog object and initializing it, as discussed in <A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter 2</A>, &#147;Dialog Boxes and Controls.&#148; If the user exits the dialog box by clicking the 
OK button, the <font color="#008000">OnLButtonDown()</font> function checks the value of the dialog box's <font color="#008000">m_radio</font> data member. A value of 0 means that the first radio button (Set) is selected, 1 means that the second button 
(Insert) is selected, and 2 means that the third button (Add) is selected.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P><A HREF="index02.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index02.htm" target="text">Chapter 2</A>, &#147;Dialog Boxes and Controls,&#148; discusses displaying dialog boxes and getting 
values from them.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>If the user wants to set an array element, the program calls <font color="#008000">SetAtGrow()</font>, giving the array index and the new value as arguments. Unlike the regular <font 
color="#008000">SetAt()</font> function, which you can use only with a currently valid index number, <font color="#008000">SetAtGrow()</font> will enlarge the array as necessary in order to set the specified array element. That&#146;s how the extra array 
elements were added when you chose to set element 20.</P>
<P>When the user has selected the Insert radio button, the program calls the <font color="#008000">InsertAt()</font> function, giving the array index and new value as arguments. This causes MFC to 
create a new array element at the index specified, shoving the other array elements forward. Finally, when the user has selected the Add option, the program calls the <font color="#008000">Add()</font> function, which adds a new element to the end of the 
array. This function's single argument is the new value to place in the added element. The call to <font color="#008000">Invalidate()</font> forces the window to redraw the data display with the new information.</P>
<P><B>Reading Through the Array</B></P>

<P>So that you can see what's happening as you add, change, and delete array elements, the Array Demo application's <font color="#008000">OnDraw()</font> function reads through the array, displaying the values that it finds in each element. The code for 
this function is shown in Listing E.2.</P>
<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">
<P><A HREF="index06.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index06.htm" target="text">Chapter 6</A>, &#147;Drawing on the Screen,&#148; shows you how to write an <font color="#008000">OnDraw()</font> function and how it gets 
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.2&#151;</B><B><I>CArrayView::OnDraw()</I></B></P>
<pre><font color="#008000">void CArrayView::OnDraw(CDC* pDC)</font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font 
color="#008000">    CArrayDoc* pDoc = GetDocument();</font></pre>
<pre><font color="#008000">    ASSERT_VALID(pDoc);</font></pre>
<pre><font color="#008000">    // Get the current font's height.</font></pre>
<pre><font color="#008000">    TEXTMETRIC 
textMetric;</font></pre>
<pre><font color="#008000">    pDC-&gt;GetTextMetrics(&amp;textMetric);</font></pre>
<pre><font color="#008000">    int fontHeight = textMetric.tmHeight;</font></pre>
<pre><font color="#008000">    // Get the size of the 
array.</font></pre>
<pre><font color="#008000">    int count = array.GetSize();</font></pre>
<pre><font color="#008000">    int displayPos = 10;</font></pre>
<pre><font color="#008000">    // Display the array data.</font></pre>
<pre><font color="#008000">    
for (int x=0; x&lt;count; ++x)</font></pre>
<pre><font color="#008000">    {</font></pre>
<pre><font color="#008000">        UINT value = array.GetAt(x);</font></pre>
<pre><font color="#008000">        char s[81];</font></pre>
<pre><font color="#008000">        
wsprintf(s, &quot;Element %d contains the value %u.&quot;, x, value);</font></pre>
<pre><font color="#008000">        pDC-&gt;TextOut(10, displayPos, s);</font></pre>
<pre><font color="#008000">        displayPos += fontHeight;</font></pre>
<pre><font 
color="#008000">    }</font></pre>
<pre><font color="#008000">}</font></pre>
<P>Here, the program first gets the height of the current font so that it can properly space the lines of text that it displays in the window. It then gets the number of elements 
in the array, by calling the array object's <font color="#008000">GetSize()</font> function. Finally, the program uses the element count to control a <font color="#008000">for</font> loop, which calls the array object's <font color="#008000">GetAt()</font> 
member function to get the value of the currently indexed array element. The program converts this value to a string for display purposes.</P>
<P><B>Removing Elements from the Array</B></P>
<P>Because it is a right button click in the window that brings up 
the Remove From Array dialog box, it is the program's <font color="#008000">OnRButtonDown()</font> function that handles the element-deletion duties. That function is shown in Listing E.3.</P>
<P><B>Listing 
E.3&#151;</B><B><I>CArrayView::OnRButtonDown()</I></B></P>
<pre><font color="#008000">void CArrayView::OnRButtonDown(UINT nFlags, CPoint point) </font></pre>
<pre><font color="#008000">{</font></pre>
<pre><font color="#008000">    ArrayRemoveDlg 
dialog(this);</font></pre>
<pre><font color="#008000">    dialog.m_remove = 0;</font></pre>
<pre><font color="#008000">    dialog.m_removeAll = FALSE;</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_removeAll)</font></pre>
<pre><font color="#008000">            array.RemoveAll();</font></pre>
<pre><font 
color="#008000">        else</font></pre>
<pre><font color="#008000">            array.RemoveAt(dialog.m_remove);</font></pre>
<pre><font color="#008000">        Invalidate();</font></pre>
<pre><font color="#008000">    }</font></pre>
<pre><font 
color="#008000">    </font></pre>
<pre><font color="#008000">    CView::OnRButtonDown(nFlags, point);</font></pre>
<pre><font color="#008000">}</font></pre>
<P>In this function, after displaying the dialog box, the program checks the value of the dialog 
box's <font color="#008000">m_removeAll</font> data member. A value of <font color="#008000">TRUE</font> means that the user has checked this option and wants to delete all elements from the array. In this case, the program calls the array object's <font 
color="#008000">RemoveAll()</font> member function. Otherwise, the program calls <font color="#008000">RemoveAt()</font>, whose single argument specifies the index of the element to delete. The call to <font color="#008000">Invalidate()</font> forces the 
window to redraw the data display with the new information.</P>
<H3><B>The List Classes</B></H3>
<P>Lists are like fancy arrays. The MFC list classes implement <I>linked </I><I>lists</I>, which use pointers to link their elements (called <I>nodes</I>) 
rather than depending upon contiguous memory locations to order values, lists are a better data structure to use when you need to be able to insert and delete items quickly. However, finding items in a list can be slower than finding items in an array, 
because a list often needs to be traversed sequentially in order to follow the pointers from one item to the next.</P>
<P>When using lists, you need to know some new vocabulary. Specifically, you need to know that the <I>head</I> of a list is the first 
node in the list, and the <I>tail</I> of the list is the last node in the list (Figure E.7.) Each node knows how to reach the <I>next</I> node, the one after it in the list. You'll see these terms used often as you explore MFC's list classes.</P>
<b><a 
href="iifig07.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/chxe/iifig07.gif">Fig. E.7</a></b>
<P><I>A linked list has a head and a tail, with the remaining nodes in </I><I>between.</I></P>
<P>MFC provides three list classes that you can use to create your lists. These classes are <font 
color="#008000">CObList</font> (which represents a list of objects), <font color="#008000">CPtrList</font> (which represents a list of pointers), and <font color="#008000">CStringList</font> (which represents a list of strings). Each of these classes has 
similar member functions, and the classes differ in the type of data that they can hold in their lists. Table E.2 lists and describes the member functions of the list classes.</P>
<P><B>Table E.2&#151;Member Functions of the List Classes</B></P>
<TABLE 
BORDER>
<TR>
<TD>
<P><I>Function</I></P>
<TD>
<P><I>Description</I></P>
<TR>
<TD>
<pre><font color="#008000">AddHead()</font></pre>
<TD>
<P>Adds a node to the head of the list, making the node the new head.</P>
<TR>
<TD>
<pre><font 
color="#008000">AddTail()</font></pre>
<TD>
<P>Adds a node to the tail of the list, making the node the new tail.</P>
<TR>
<TD>
<pre><font color="#008000">Find()</font></pre>
<TD>
<P>Searches the list sequentially to find the given object pointer. Returns 
a <font color="#008000">POSITION</font> value.</P>
<TR>
<TD>
<pre><font color="#008000">FindIndex()</font></pre>
<TD>
<P>Scans the list sequentially, stopping at the node indicated by the given index. Returns a <font color="#008000">POSITION</font> value 
for the node.</P>
<TR>
<TD>
<pre><font color="#008000">GetAt()</font></pre>
<TD>
<P>Gets the node at the specified position.</P>
<TR>
<TD>
<pre><font color="#008000">GetCount()</font></pre>
<TD>
<P>Gets the number of nodes in the list.</P>
<TR>
<TD>

<pre><font color="#008000">GetHead()</font></pre>
<TD>
<P>Gets the list's head node.</P>
<TR>
<TD>
<pre><font color="#008000">GetHeadPosition()</font></pre>
<TD>
<P>Gets the head node's position.</P>
<TR>
<TD>
<pre><font 
color="#008000">GetNext()</font></pre>
<TD>
<P>Gets the next node in the list when iterating over a list.</P>
<TR>
<TD>
<pre><font color="#008000">GetPrev()</font></pre>
<TD>
<P>Gets the previous node in the list when iterating over a list.</P>
<TR>
<TD>

<pre><font color="#008000">GetTail()</font></pre>
<TD>
<P>Gets the list's tail node.</P>
<TR>
<TD>
<pre><font color="#008000">GetTailPosition()</font></pre>
<TD>
<P>Gets the tail node's position.</P>
<TR>
<TD>
<pre><font 
color="#008000">InsertAfter()</font></pre>
<TD>
<P>Inserts a new node after the specified position.</P>
<TR>
<TD>
<pre><font color="#008000">InsertBefore()</font></pre>
<TD>
<P>Inserts a new node before the specified position.</P>
<TR>
<TD>
<pre><font 
color="#008000">IsEmpty()</font></pre>
<TD>
<P>Returns <font color="#008000">TRUE</font> if the list is empty and returns <font color="#008000">FALSE</font> otherwise.</P>
<TR>
<TD>
<pre><font color="#008000">RemoveAll()</font></pre>
<TD>
<P>Removes all of 
a list's nodes.</P>
<TR>
<TD>
<pre><font color="#008000">RemoveAt()</font></pre>
<TD>
<P>Removes a single node from a list.</P>
<TR>
<TD>
<pre><font color="#008000">RemoveHead()</font></pre>

⌨️ 快捷键说明

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