📄 ch05.htm
字号:
<TD>
<pre><font color="#008000">CScrollView</font></pre>
<TD>
<P>A view class that provides scrolling capabilities</P>
<TR>
<TD>
<pre><font color="#008000">CCtrlView</font></pre>
<TD>
<P>A base class from which view classes that implement new Windows 95 common controls (such as the ListView, TreeView, and RichEdit controls) are derived</P>
<TR>
<TD>
<pre><font color="#008000">CEditView</font></pre>
<TD>
<P>A view class that provides basic text-editing features</P>
<TR>
<TD>
<pre><font color="#008000">CRichEditView</font></pre>
<TD>
<P>A view class that provides more sophisticated text-editing capabilities by using the Windows 95 RichEdit control</P>
<TR>
<TD>
<pre><font color="#008000">CListView</font></pre>
<TD>
<P>A view class that displays a Windows 95 ListView control in its window</P>
<TR>
<TD>
<pre><font color="#008000">CTreeView</font></pre>
<TD>
<P>A view class that displays a Windows 95 TreeView control in its window</P>
<TR>
<TD>
<pre><font color="#008000">CFormView</font></pre>
<TD>
<P>A view class that implements a form-like window using a dialog box resource</P>
<TR>
<TD>
<pre><font color="#008000">CRecordView</font></pre>
<TD>
<P>A view class that can display database records along with controls for navigating the database</P>
<TR>
<TD>
<pre><font color="#008000">CDaoRecordView</font></pre>
<TD>
<P>Same as <font color="#008000">CRecordView</font>, except used with the new DAO database classes</P></TABLE>
<P>To use one of these classes, you just substitute the desired class for the <font color="#008000">CView</font> class in the application's project. When using AppWizard to generate your project, you can specify the view class you want in the wizard's
Step 6 of 6 dialog box, as shown in Figure 5.3. Once you have the desired class installed as the project's view class, you can use the specific class's member functions to control the view window. <A HREF="index06.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index06.htm" target="text">Chapter 6</A>,
"Drawing on the Screen," demonstrates using the <font color="#008000">CScrollView</font> class to implement a scrolling view.</P>
<A HREF="Gfig03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch05/Gfig03.gif"><b>Fig. 5.3</b></A>
<P><I>You can use AppWizard to select your application's base view class.</I></P>
<P>A <font color="#008000">CEditView</font> object, on the other hand, gives you all the features of a Windows edit control in your view window. Using this class, you can handle various editing and printing tasks, including find-and-replace. You can
retrieve or set the current printer font by calling the <font color="#008000">GetPrinterFont()</font> or <font color="#008000">SetPrinterFont()</font> member function or get the currently selected text by calling <font
color="#008000">GetSelectedText()</font>. Moreover, the <font color="#008000">FindText()</font> member function locates a given test string, and <font color="#008000">OnReplaceAll()</font> replaces all occurrences of a given text string with another
string.</P>
<P>The <font color="#008000">CRichEditView</font> class adds many features to an edit view, including paragraph formatting (such as centered, right-aligned, and bulleted text), character attributes (including underlined, bold, and italic), and the
capability to set margins, fonts, and paper size. As you may have guessed, the <font color="#008000">CRichEditView</font> class features a rich set of methods you can use to control your application's view object.</P>
<P>Figure 5.4 shows how the view classes fit into MFC's class hierarchy. Describing these various view classes fully is beyond the scope of this chapter. However, you can find plenty of information about them in your Visual C++ online documentation.</P>
<b><A HREF="Gfig04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch05/Gfig04.gif">Fig. 5.4</A></b>
<P><I>The view classes all trace their ancestry back to </I><I><font color="#008000">CView</font></I><I>.</I></P>
<H3><A ID="I12" NAME="I12"><A ID="I13" NAME="I13"><B>Document Templates, Views, and Frame Windows</B></A></A></H3>
<P>Because you've been working with AppWizard-generated applications in this chapter, you've taken for granted a lot of what goes on in the background of an MFC document/view program. That is, much of the code that enables the frame window (your
application's main window), the document, and the view window to work together is automatically generated by AppWizard and manipulated by MFC.</P>
<P>For example, if you look at the <font color="#008000">InitInstance()</font> method of the Rectangles application's <font color="#008000">CRecsApp</font> class, you see (among other things) the lines shown in Listing 5.6.</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.6—</I>RECS.CPP<I>—Initializing an </I><I>Application's Document</I></P>
<pre><font color="#008000">CSingleDocTemplate* pDocTemplate;</font></pre>
<pre><font color="#008000">pDocTemplate = new CSingleDocTemplate(</font></pre>
<pre><font color="#008000"> IDR_MAINFRAME,</font></pre>
<pre><font color="#008000"> RUNTIME_CLASS(CRecsDoc),</font></pre>
<pre><font color="#008000"> RUNTIME_CLASS(CMainFrame),</font></pre>
<pre><font color="#008000"> RUNTIME_CLASS(CRecsView));</font></pre>
<pre><font color="#008000">AddDocTemplate(pDocTemplate);</font></pre>
<P>In Listing 5.6, you discover one of the secrets that makes the document/view system work. In that code, the program creates a document-template object. These document templates have nothing to do with C++ templates. They pull together the following
objects:</P>
<ul>
<li> <B>A resource ID identifying a menu resource:</B> <font color="#008000">IDR_MAINFRAME</font> in this case</P>
<li> <B>A document class:</B> <font color="#008000">CRecsDoc</font> in this case</P>
<li> <B>A frame window class:</B> always <font color="#008000">CMainFrame</font></pre>
<li> <B>A view class:</B> <font color="#008000">CRecsView</font> in this case</P>
</ul>
<P>Notice that you are not passing an object, or a pointer to an object. You are passing the <I>name</I> of the class to a macro called <font color="#008000">RUNTIME_CLASS</font>. It enables the framework to create instances of a class at runtime, which
the application object must be able to do in a program that uses the document/view architecture. In order for this macro to work, the classes that will be created dynamically must be declared and implemented as such. To do this, the class must have the
<font color="#008000">DECLARE_DYNCREATE</font> macro in its declaration (in the header file) and the <font color="#008000">IMPLEMENT_DYNCREATE</font> macro in its implementation. AppWizard takes care of this for you.</P>
<P>For example, If you look at the header file for the Rectangles application's <font color="#008000">CMainFrame</font> class, you see the following line near the top of the class's declaration:</P>
<pre><font color="#008000">DECLARE_DYNCREATE(CMainFrame)</font></pre>
<P>As you can see, the <font color="#008000">DECLARE_DYNCREATE</font> macro requires the class's name as its single argument.</P>
<P>Now, if you look near the top of <font color="#008000">CMainFrame</font>'s implementation file (<font color="#008000">MAINFRM.CPP</font>), you see this line:</P>
<pre><font color="#008000">IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)</font></pre>
<P>The <font color="#008000">IMPLEMENT_DYNCREATE</font> macro requires as arguments the name of the class and the name of the base class.</P>
<P>If you explore the application's source code further, you find that the document and view classes also contain the <font color="#008000">DECLARE_DYNCREATE</font> and <font color="#008000">IMPLEMENT_DYNCREATE</font> macros.</P>
<P>If you haven't heard of frame windows before, you should know that they contain all the windows involved in the applications—this means control bars as well as views. They also route messages and commands to views and documents, as discussed in <A
HREF="index04.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index04.htm" target="text">Chapter 4</A>, "Messages and Commands."</P>
<P>The last line of Listing 5.6 calls <font color="#008000">AddDocTemplate()</font> in order to pass the object on to the application object, <font color="#008000">CRecsApp</font>, which keeps a list of documents. <font
color="#008000">AddDocTemplate()</font> adds this document to this list and uses the document template to create the document object, the frame, and the view window.</P>
<P>Since this is a Single Document Interface, a single document template (<font color="#008000">CSingleDocTemplate</font>) is created. Multiple Document Interface applications use one <font color="#008000">CMultiDocTemplate</font> object for each kind of
document they support. For example, a spreadsheet program might have two kinds of documents: tables and graphs. Each would have its own view and its own set of menus. Two instances of <font color="#008000">CMultiDocTemplate</font> would be created in <font
color="#008000">InitInstance()</font>, each pulling together the menu, document, and view that belong together. If you've ever seen the menus in a program change as you switched from one view or document to another, now you know how you can achieve the
same effect: simply associate them with different menu resource IDs as you build the document templates.</P>
<H3><A ID="I14" NAME="I14"><A ID="I15" NAME="I15"><B>From Here</B></A><B>...</B></A></H3>
<P>In this chapter, you examined how an AppWizard-generated application uses MFC to coordinate an application's document and view objects. There is, of course, a great deal more to learn about MFC before you can create your own sophisticated Windows 95
applications. If you'd like to learn more about some topics presented in this chapter, refer to the following:</P>
<ul>
<li> <A HREF="index06.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index06.htm" target="text">Chapter 6</A>, "Drawing on the Screen," demonstrates how to display data in an application's window.</P>
<li> <A HREF="index07.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index07.htm" target="text">Chapter 7</A>, "Printing and Print Preview," shows how to include these powerful features in your application.</P>
<li> <A HREF="index08.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index08.htm" target="text">Chapter 8</A>, "Persistence and File I/O," provides the details of how to use MFC's file-handling classes.</P>
<li> <A HREF="indexb.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/indexb.htm" target="text">Appendix B</A>, "The Developer Studio User Interface," explains how to control your programming projects with Developer Studio.</P>
</ul>
<p><hr></p>
<center>
<p><font size=-2>
© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a
Simon and Schuster Company.</font></p>
</center>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -