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

📄 ch05.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<P>Step 6 of 6</P>

<TD>

<P>Leave set to defaults.</P></TABLE><br>

<A HREF="Gfig01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch05/Gfig01.gif"><b>Fig. 5.1</b></A>

<P><I>When you create an SDI application with AppWizard, the project information summary confirms </I><I>your settings</I></P>

<P>Now that you have a starter application, it's time to add code to the document and view classes in order to create an application that actually does something. This application will draw many rectangles in the view and save the coordinates of the 
rectangles in the document.</P>

<P>Follow these steps to add the code that modifies the document class to handle the application's data, which is an array of <font color="#008000">CPoint</font> objects that determine where rectangles should be drawn in the view window:</P>

<ol> 

<li><P> Click the ClassView tab to display the ClassView in the project workspace window at the left of the screen.</P>

<li><P> Expand the <font color="#008000">Recs</font> classes by clicking on the + sign before them.</P>

<li><P> Right-click the <font color="#008000">CRecsDoc</font> class, and choose Add Member <U>V</U>ariable from the shortcut menu that appears.</P>

<li><P> Fill in the Add Member Variable dialog box. For Variable <U>T</U>ype, enter <B><font color="#008000">CPoint</font></B>. For Variable <U>D</U>eclaration, enter <B><font color="#008000">m_points[100]</font></B>. Make sure the <U>P</U>ublic radio 
button is selected. Click OK.</P>

<li><P> Again, right-click the <font color="#008000">CRecsDoc</font> class, and choose Add Member <U>V</U>ariable.</P>

<li><P> For Variable <U>T</U>ype, enter <B><font color="#008000">UINT</font></B>. For Variable <U>D</U>eclaration, enter <B><font color="#008000">m_pointIndex</font></B>. Make sure the <U>P</U>ublic radio button is selected. Click OK.</P>

<li><P> Click the + next to <font color="#008000">CRecsDoc</font> in ClassView to see the member variables and functions. The two member variables you added are now listed.</P>

</ol>

<P>The <font color="#008000">m_points[]</font> array holds the locations of rectangles displayed in the view window. The <font color="#008000">m_pointIndex</font> data member holds the index of the next empty element of the array.</P>

<blockquote><p><img src="tip.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/tip.gif">

<P>If you've programmed in C++ before and are not used to the ClassView, you can open <font color="#008000">RecsDoc.h</font> from the FileView and add (after a public: specifier) the two lines of code that declare these variables:</P>

<pre><font color="#008000">UINT m_pointIndex;</font></pre>

<pre><font color="#008000">     CPoint m_points[100];</font></pre>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>Now you need to get these variables initialized to appropriate values and then use them to draw the view. MFC applications that use the document/view paradigm initialize document data in a function called <font color="#008000">OnNewDocument()</font>, 
which is called automatically when the application first runs and whenever the user chooses <U>F</U>ile, <U>N</U>ew.</P>

<P>The list of member variables and functions of <font color="#008000">CRecsDoc</font> should still be displayed in ClassView. Double-click <font color="#008000">OnNewDocument()</font> in that list to edit the code. Using Listing 5.3 as a guide, remove 
the comments left by AppWizard and initialize <font color="#008000">m_pointIndex</font> to zero.</P>

<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing </I><I>5.3&#151;RecsDoc.cpp&#151;</I>CRecsDoc::OnNewDocument()</P>

<pre><font color="#008000">BOOL CRecsDoc::OnNewDocument()</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">     if (!CDocument::OnNewDocument())</font></pre>

<pre><font color="#008000">          return FALSE;</font></pre>

<pre><font color="#008000">    m_pointIndex = 0;</font></pre>

<pre><font color="#008000">     return TRUE;</font></pre>

<pre><font color="#008000">}</font></pre>

<P>There is no need to initialize the array of points, since the index into the array will be used to ensure no code tries to use an uninitialized element of the array. At this point your modifications to the document class are complete. As you'll see in 
<A HREF="index09.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index09.htm" target="text">Chapter 9</A>, &quot;Persistence and File I/O,&quot; there are a few simple changes to make if you want this information actually saved in the document. In order to focus on the way documents and views work together, you 
will not be making those changes to the Recs application.</P>

<P>Now turn your attention to the view class. It will use the document data to draw rectangles on-screen. A full discussion of the way that drawing works must wait for <A HREF="index06.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index06.htm" target="text">Chapter 6</A>, &quot;Drawing on the Screen.&quot; 
For now it is enough to know that the <font color="#008000">OnDraw()</font> function of your view class does the drawing. Expand the <font color="#008000">CRecsView</font> class in ClassView and double-click <font color="#008000">OnDraw()</font>. Using 
Listing 5.4 as a guide, remove the comments left by AppWizard and add code to draw a rectangle at each point in the array.</P>

<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing </I><I>5.4&#151;</I>RECSVIEW.CPP<I>&#151;</I>CRecsView::OnDraw()</P>

<pre><font color="#008000">void CRecsView::OnDraw(CDC* pDC)</font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    CRecsDoc* pDoc = GetDocument();</font></pre>

<pre><font color="#008000">    ASSERT_VALID(pDoc);</font></pre>

<pre><font color="#008000">    UINT pointIndex = pDoc-&gt;m_pointIndex;</font></pre>

<pre><font color="#008000">    for (UINT i=0; i&lt;pointIndex; ++i)</font></pre>

<pre><font color="#008000">    {</font></pre>

<pre><font color="#008000">        UINT x = pDoc-&gt;m_points[i].x;</font></pre>

<pre><font color="#008000">        UINT y = pDoc-&gt;m_points[i].y;</font></pre>

<pre><font color="#008000">        pDC-&gt;Rectangle(x, y, x+20, y+20);</font></pre>

<pre><font color="#008000">    }</font></pre>

<pre><font color="#008000">}</font></pre>

<P>Your modifications to the starter application generated by AppWizard are almost complete. You have added member variables to the document, initialized those variables in the document's <font color="#008000">OnNewDocument()</font> function, and used 
those variables in the view's <font color="#008000">OnDraw()</font> function. All that remains is to enable the user to add points to the array. You catch the mouse message with ClassWizard and then add code to the message handler. Follow these steps:</P>

<ol> 

<li><P> Choose <U>V</U>iew, Class<U>W</U>izard. The ClassWizard dialog box appears.</P>

<li><P> Make sure that <font color="#008000">CRecsView</font> is selected in the Class <U>N</U>ame and Object <U>I</U>Ds boxes. Then, double-click <font color="#008000">WM_LBUTTONDOWN</font> in the Messages box to add the <font 
color="#008000">OnLButtonDown()</font> message-response function to the class. Whenever the application receives a <font color="#008000">WM_LBUTTONDOWN</font> message, it will call <font color="#008000">OnLButtonDown()</font>.</P>

<li><P> Click the Edit Code button to jump to the <font color="#008000">OnLButtonDown()</font> function in your code. Then, add the code shown in Listing 5.5 to the function.</P>

</ol>

<p><img src="cd_rom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/cd_rom.gif" hspace=10>

<P><I>Listing </I><I>5.5&#151;</I>RECSVIEW.CPP<I>&#151;</I>CRecsView::OnLButtonDown()</P>

<pre><font color="#008000">void CRecsView::OnLButtonDown(UINT nFlags, CPoint point) </font></pre>

<pre><font color="#008000">{</font></pre>

<pre><font color="#008000">    CRecsDoc *pDoc = GetDocument();</font></pre>

<pre><font color="#008000">    // don't go past the end of the 100 points allocated</font></pre>

<pre><font color="#008000">    if (pDoc-&gt;m_pointIndex == 100) </font></pre>

<pre><font color="#008000">       return;</font></pre>

<pre><font color="#008000">    //store the click location</font></pre>

<pre><font color="#008000">    pDoc-&gt;m_points[pDoc-&gt;m_pointIndex] = point; </font></pre>

<pre><font color="#008000">    pDoc-&gt;m_pointIndex++; </font></pre>

<pre><font color="#008000">    pDoc-&gt;SetModifiedFlag();</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 color="#008000">}</font></pre>

<P>The new OnLButtonDown<font color="#008000">()</font> adds a point to the document's point array each time the user clicks the left mouse button over the view window. It increments <font color="#008000">m_pointIndex</font> so that the next click goes 
into the point on the array after this one.</P>

<P>The call to <font color="#008000">SetModifiedFlag()</font> marks this document as modified, or &quot;dirty.&quot; MFC automatically prompts the user to save any dirty files on exit. (The details are in <A HREF="index08.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index08.htm" target="text">Chapter 8</A>, 
&quot;Persistence and File I/O.&quot;) Any code you write that changes any document variables should call <font color="#008000">SetModifiedFlag()</font>.</P>

<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">

<P>Earlier in this chapter you were reminded that public access functions in the document had some advantages. One such advantage: any document member function that changed a variable also could call <font color="#008000">SetModifiedFlag()</font>, thus 
guaranteeing no programmer could forget it.</P>

<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>

<P>Finally, the call to <font color="#008000">Invalidate()</font> causes MFC to call the <font color="#008000">OnDraw()</font> function, where the window's display is redrawn with the new data. <font color="#008000">Invalidate()</font> takes a single 
parameter (with the default value <font color="#008000">TRUE</font>) that determines if the background is erased before calling <font color="#008000">OnDraw()</font>. On rare occasions you may choose to call <font color="#008000">Invalidate(FALSE)</font>, 
so that <font color="#008000">OnDraw()</font> draws over whatever was already on the screen.</P>

<P>Finally, a call to the base class OnLButtonDown() takes care of the rest of the work involved in handling a mouse click.</P>

<P>You've now finished the complete application. Click the toolbar's Build button, or choose <U>B</U>uild, <U>B</U>uild command from the menu bar, to compile and link the application. After you have the Rectangles application compiled and linked, run it 
by choosing <U>B</U>uild, E<U>x</U>ecute. When you do, you see the application's main window. Place your mouse pointer over the window's client area and click. A rectangle appears. Go ahead and keep clicking. You can place up to 100 rectangles in the 
window (see Figure 5.2).</P>

<A HREF="Gfig02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch05/Gfig02.gif"><b>Fig. 5.2</b></A>

<P><I>The Rectangles application draws rectangles wherever you click.</I></P>

<H3><A ID="I10" NAME="I10"><A ID="I11" NAME="I11"><B>Other View Classes</B></A></A></H3>

<P>The view classes generated by AppWizard in this chapter's sample applications have been derived from MFC's <font color="#008000">CView</font> class. There are cases, however, when it is to your advantage to derive your view class from one of the other 
MFC view classes that are themselves derived from <font color="#008000">CView</font>. These additional classes provide your view window with special capabilities such as scrolling and text editing. Table 5.1 lists the various view classes along with their 
descriptions.</P>

<P><I>Table 5.1&#151;View classes</I></P>

<TABLE BORDER>

<TR>

<TD>

<P><B>Class</B></P>

<TD>

<P><b>Description</b></P>

<TR>

<TD>

<pre><font color="#008000">CView</font></pre>

<TD>

<P>The base view class from which the specialized view classes are derived</P>

<TR>

⌨️ 快捷键说明

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