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

📄 ch05.htm

📁 VC使用大全。里面集合了VC使用的各种使用技巧。非常有用。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<pre><font color="#008000">     DECLARE_DYNCREATE(CApp1View)</font></pre>

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

<pre><font color="#008000">public:</font></pre>

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

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

<pre><font color="#008000">public:</font></pre>

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

<pre><font color="#008000">     // ClassWizard generated virtual function overrides</font></pre>

<pre><font color="#008000">     //{{AFX_VIRTUAL(CApp1View)</font></pre>

<pre><font color="#008000">     public:</font></pre>

<pre><font color="#008000">     virtual void OnDraw(CDC* pDC);  // overridden to draw this view</font></pre>

<pre><font color="#008000">virtual BOOL PreCreateWindow(CREATESTRUCT&amp; cs);</font></pre>

<pre><font color="#008000">     protected:</font></pre>

<pre><font color="#008000">     virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);</font></pre>

<pre><font color="#008000">     virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);</font></pre>

<pre><font color="#008000">     virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);</font></pre>

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

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

<pre><font color="#008000">public:</font></pre>

<pre><font color="#008000">     virtual ~CApp1View();</font></pre>

<pre><font color="#008000">#ifdef _DEBUG</font></pre>

<pre><font color="#008000">     virtual void AssertValid() const;</font></pre>

<pre><font color="#008000">     virtual void Dump(CDumpContext&amp; dc) const;</font></pre>

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

<pre><font color="#008000">protected:</font></pre>

<pre><font color="#008000">// Generated message map functions</font></pre>

<pre><font color="#008000">protected:</font></pre>

<pre><font color="#008000">     //{{AFX_MSG(CApp1View)</font></pre>

<pre><font color="#008000">          // NOTE - the ClassWizard will add and remove member functions here.</font></pre>

<pre><font color="#008000">          //    DO NOT EDIT what you see in these blocks of generated code !</font></pre>

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

<pre><font color="#008000">     DECLARE_MESSAGE_MAP()</font></pre>

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

<pre><font color="#008000">#ifndef _DEBUG  // debug version in App1View.cpp</font></pre>

<pre><font color="#008000">inline CApp1Doc* CApp1View::GetDocument()</font></pre>

<pre><font color="#008000">   { return (CApp1Doc*)m_pDocument; }</font></pre>

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

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

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

<pre><font color="#008000">// Microsoft Developer Studio will insert additional declarations immediately before the </font><font color="#008000">previous line.</font></pre>

<pre><font color="#008000">#endif // !defined(AFX_APP1VIEW_H__43BB481F_64AE_11D0_9AF3_0080C81A397C__INCLUDED_)</font></pre>

<P>Near the top of the listing, you can see the class's public attributes, where it declares the <font color="#008000">GetDocument()</font> function as returning a pointer to a <font color="#008000">CApp1Doc</font> object. Anywhere in the view class that 
you need to access the document's data, you can call <font color="#008000">GetDocument()</font> to obtain a pointer to the document. For example, to add a <font color="#008000">CPoint</font> object to the aforementioned array of <font 
color="#008000">CPoint</font> objects stored as the document's data, you might use the following line:</P>

<pre><font color="#008000">GetDocument()-&gt;m_points[x] = point;</font></pre>

<P>You also can do this a little differently, of course, by storing the pointer returned by GetDocument() in a local pointer variable and then using that pointer variable to access the document's data, like this:</P>

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

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

<P>The second version is more convenient when you need to use the document pointer in several places in the function, or if using the less clear <font color="#008000">GetDocument()-&gt;variable</font> version makes the code hard to understand. </P>

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

<P>In release versions of your program, the <font color="#008000">GetDocument()</font> function is inline, which means there is no performance advantage to saving the pointer like this, but it does improve readability. Inline functions are expanded into 
your code like macros, but offer type checking and other advantages, as discussed in the electronic book C++ By Example, included on the CD with this book.</P>

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

<P>Notice that the view class, like the document class, overrides a number of virtual functions from its base class. As you'll soon see, the <font color="#008000">OnDraw()</font> function, which is the most important of these virtual functions, is where 
you paint your window's display. As for the other functions, MFC calls <font color="#008000">PreCreateWindow()</font> before the window element (that is, the actual Windows window) is created and attached to the MFC window class, giving you a chance to 
modify the window's attributes (such as size and position). These two functions are discussed in more detail in <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; <font color="#008000">OnPreparePrinting()</font> is used 
to modify the Print dialog box before it displays for the user; the <font color="#008000">OnBeginPrinting()</font> function gives you a chance to create GDI objects like pens and brushes that you need to handle the print job; and <font 
color="#008000">OnEndPrinting()</font> is where you can destroy any objects you may have created in <font color="#008000">OnBeginPrinting()</font>. These three functions are discussed in <A HREF="index07.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index07.htm" target="text">Chapter 7</A>, &quot;Printing and 
Print Preview.&quot;</P>

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

<P>When you first start using an application frameworklike MFC, it's easy to get confused about the difference between an object instantiated from an MFC class and the Windows element it represents. For example, when you create an MFC frame-window object, 
you're actually creating two things: the MFC object that has member functions and member variables, and a Windows window that you can manipulate using the functions of the MFC object. The window element is associated with the MFC class, but is also an 
entity unto itself.</P>

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

<H3><A ID="I8" NAME="I8"><A ID="I9" NAME="I9"><B>Creating the Rectangles Application</B></A></A></H3>

<P>Now that you've had an introduction to documents and views, a little hands-on experience should help you better understand how these classes work. In the steps that follow, you build the Rectangles application, which demonstrates the manipulation of 
documents and views. When you first run this application, it will draw an empty window. Wherever you click in the window, a small rectangle will be drawn. You can resize the window, or minimize and restore it, and the rectangles will be redrawn at all the 
coordinates where you clicked. This is accomplished by keeping an array of coordinate points in the document and using that array in the view.</P>

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

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

<P>The complete source code and executable file for the Rectangles application can be found in the <font color="#008000">CHAP06\RECS</font> directory of this book's CD-ROM.</P>

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

<P>First, use AppWizard to create the basic files for the Rectangles program, selecting the options listed in the following table. (AppWizard is first discussed in <A HREF="index01.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index01.htm" target="text">Chapter 1</A>, &quot;Building Your First 
Application.&quot; When you're done, the New Project Information dialog box appears; it should look like Figure 5.1. Click the OK button to create the project files.</P>

<TABLE BORDER>

<TR>

<TD>

<P><B>Dialog Box Name</B></P>

<TD>

<P><b>Options to Select</b></P>

<TR>

<TD>

<P>New Project</P>

<TD>

<P>Name the project <B>recs</B>, and set the project path to the directory into which you want to store the project's files. Leave the other options set to their defaults.</P>

<TR>

<TD>

<P>Step 1</P>

<TD>

<P>Select Single Document.</P>

<TR>

<TD>

<P>Step 2 of 6</P>

<TD>

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

<TR>

<TD>

<P>Step 3 of 6</P>

<TD>

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

<TR>

<TD>

<P>Step 4 of 6</P>

<TD>

<P>Turn off all application features except Printing and Print Preview.</P>

<TR>

<TD>

<P>Step 5 of 6</P>

<TD>

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

<TR>

<TD>

⌨️ 快捷键说明

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