?? ch23.htm
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN"><HTML><HEAD> <META NAME="GENERATOR" Content="Symantec Visual Page Mac 1.1"> <TITLE>Teach Yourself Visual C++® 5 in 24 Hours -- Hour 23 -- Advanced Views</TITLE></HEAD><BODY TEXT="#000000" BGCOLOR="#FFFFFF"><H1 ALIGN="CENTER"><IMG SRC="../button/sams.gif" WIDTH="171" HEIGHT="66" ALIGN="BOTTOM"BORDER="0"><BR><FONT COLOR="#000077">Teach Yourself Visual C++® 5 in 24 Hours</FONT></H1><CENTER><P><A HREF="../ch22/ch22.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch24/ch24.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <HR></CENTER><H1 ALIGN="CENTER"><FONT COLOR="#000077">- Hour 23 -<BR>Advanced Views</FONT></H1><P>In this hour you learn more about MFC and Document/View, expanding on the materialcovered in Hour 9, "The Document/View Architecture." In this hour, youwill learn about<UL> <LI>Form views, a convenient way to use controls in your views, much like dialog boxes <LI>The MFC <TT>CFormView</TT> class that supports form views <LI>How to associate multiple views with one document</UL><P>In this hour, you will also modify the DVTest project from Hour 9 so that it includesform views as well as multiple views for a document.<H2><FONT COLOR="#000077"><B>What Is a Form View?</B></FONT></H2><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>form view</I> is a viewthat can contain controls, much like a dialog box.</P><P>You usually add controls to a form view using a dialog resource, the same wayyou build dialog boxes. However, unlike dialog boxes, a form view is never modal;in fact, several form views can be open simultaneously, just like other views.</P><P>The most common reason to use a form view is because it's an easy-to-use viewthat looks like a dialog box. With practice, you can create a form view in a fewminutes. You can add controls used in a form view to the form view's dialog resource,just as you add controls to a resource used by a normal dialog box. After you addthe controls, you can associate them with MFC objects and class member variables,just as you associate controls with dialog boxes.</P><P>Using form views enables you to easily adapt the DDX and DDV routines used bydialog boxes to a view. Unlike a modal dialog box, several different form views canbe open at once, making your program much more flexible. Like other views, a formview has access to all the Document/View interfaces, giving it direct access to thedocument class.</P><P>It's common for a form view to be one of several possible views for a document.In an MDI application, it's common to have more than one view for each document type.For example, a form view can be used as a data entry view, and another type of viewcan be used for display purposes. Figure 23.1 presents a class diagram showing someof the classes derived from <TT>CView</TT>.</P><P><A NAME="01"></A><A HREF="01.htm"><B>Figure 23.1.</B></A> <I><BR>The MFC view classes.</I><H3><FONT COLOR="#000077"><B>What Are the Other Types of Views?</B></FONT></H3><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>A <I>scroll view</I> is aview that can be larger than its visible area.</P><P>In a scroll view, the invisible part of the view can be made visible using scrollbarsassociated with the view. An easy way to visualize how scrolling works is to imaginea large virtual view, hidden except for a small window used as a viewport, as shownin Figure 23.2.</P><P>Only a portion of the entire view is visible in Figure 23.2. The view window scrollsto different parts of the document; the underlying large view retains its originalposition. Although you can implement scrolling yourself for any class derived from<TT>CView</TT>, much of the work is done for you if you use <TT>CScrollView</TT>.</P><P><A NAME="02"></A><A HREF="02.htm"><B>Figure 23.2.</B></A> <BR><I>Scrolling a view using <TT>CScrollView</TT>.</I></P><P><FONT COLOR="#000077"><B>New Term:</B></FONT><B> </B>The <I>edit view</I> is aview that consists of an edit control. The edit view automatically supports printing,using the clipboard cut, paste, and copy functions, and Find and Replace. The editview is supported by the <TT>CEditView</TT> class, so it can be associated with adocument just like any other view.<BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>The edit view does not support true what-you-see-is-what-you-get (WYSIWYG) editing; only one font is supported for the entire view, and the display is not always 100 percent accurate with regard to a printed page. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Using a Form View</B></FONT></H3><BLOCKQUOTE> <P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Using a form view requires only a few more steps than using a dialog box. All the hard work is handled by the framework and the MFC <TT>CFormView</TT> class. Using ClassWizard, you can add a form view to a project using 30 or 40 lines of your own code. <HR></BLOCKQUOTE><P>To illustrate how form views are used, add a form view to a project that was builtusing AppWizard. To reduce the amount of code that must be entered, you can reusethe DVTest project built in Hour 9. To recap, the DVTest project stored a collectionof names in its document class. In this hour, you will associate a form view withthe document and use it to display and input names into the program. For now, theform view is the only view associated with the document; you will learn about multipleviews later this hour.<H3><FONT COLOR="#000077"><B>Creating a Dialog Resource for a Form View</B></FONT></H3><P>Although a form view uses a dialog resource to lay out its controls, a form viewisn't really a dialog box; instead, a form view uses the dialog resource as a templatewhen the view is created. For this reason, the <TT>CFormView</TT> class has specialrequirements for the dialog resources it uses. Use the properties shown in Table23.1 for dialog resources used by form views.<H4><FONT COLOR="#000077">Table 23.1. Properties for dialog resources used by formviews.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Property</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Value</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Style</TD> <TD ALIGN="LEFT" VALIGN="TOP">Child</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Border</TD> <TD ALIGN="LEFT" VALIGN="TOP">None</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Visible</TD> <TD ALIGN="LEFT" VALIGN="TOP">Unchecked</TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Titlebar</TD> <TD ALIGN="LEFT" VALIGN="TOP">Unchecked</TD> </TR></TABLE></P><P>Other than the values listed in Table 23.1, there are no other limitations fordialog box properties or controls. Any controls you can add to a dialog box can beused in a form view.</P><P>Developer Studio enables you to add a dialog box resource to your project thathas all of the properties needed for a form view. Follow these steps:<DL> <DD>1. Right-click the tree inside the ResourceView window.<BR> <BR> 2. Select Insert from the context menu. A dialog box will be displayed containing a tree of resources that can be added to your project.<BR> <BR> 3. Expand the Dialog tree; a list of dialog box templates is displayed.<BR> <BR> 4. Select the <TT>IDD_FORMVIEW</TT> template, and click the New button.</DL><P>To get started on the form view example, add a dialog resource to the DVTest project.This dialog resource will be used in a form view, as shown in Figure 23.3.</P><P><A NAME="03"></A><A HREF="03.htm"><B>Figure 23.3.</B> </A><I><BR>The dialog resource used as a form view in the CDVTest sample project.</I></P><P>Set the dialog resource and control resource IDs as listed in Table 23.2. Thelist box should not be automatically sorted by Windows for this example, so clearthe Sort attribute for the list box control. Use default properties for all othercontrols.<H4><FONT COLOR="#000077">Table 23.2. Properties for the CDVTest form view dialogresource.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Property</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>ID</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Dialog</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDD_FORM_VIEW</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Edit Control</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_EDIT</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">List Control</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_LIST</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Close Button</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_CLOSE</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Apply Button</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_APPLY</TT></TD> </TR></TABLE><H3><FONT COLOR="#000077"><B>Adding a Form View Class to a Project</B></FONT></H3><P>Use ClassWizard to add a form view class to a project, much as you would add adialog box class to a project. After creating the dialog resource, add the form viewclass using the following steps:<DL> <DD>1. Open ClassWizard. Because ClassWizard knows that a new resource has been added, a dialog box prompts you to choose between two options for the new dialog resource. Select the option labeled Create a New Class.<BR> <BR> 2. Fill in the Add Class dialog box using the values from Table 23.3, then click OK.</DL><H4><FONT COLOR="#000077">Table 23.3. Sample values for the Add Class dialog box.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Control</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Value</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Name</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CFormTest</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Base Class</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CFormView</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">File</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>FormTest.cpp</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Dialog ID</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDD_FORM_VIEW</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP">Automation</TD> <TD ALIGN="LEFT" VALIGN="TOP">None</TD> </TR></TABLE></P><P>Use ClassWizard to add two member variables to the <TT>CFormTest</TT> class, asshown in Table 23.4.<H4><FONT COLOR="#000077">Table 23.4. Control variables added to the CFormTest class.</FONT></H4><P><TABLE BORDER="1"> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><B>Control ID</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Control Type</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Variable Type</B></TD> <TD ALIGN="LEFT" VALIGN="TOP"><B>Variable Name</B></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_LIST</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Control</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CListCtrl</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>m_lbNames</TT></TD> </TR> <TR ALIGN="LEFT" rowspan="1"> <TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_EDIT</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP">Control</TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>CEdit</TT></TD> <TD ALIGN="LEFT" VALIGN="TOP"><TT>m_edNames</TT></TD> </TR></TABLE><H3><FONT COLOR="#000077"><B>Using <TT>CFormView</TT> Instead of <TT>CView</TT></B></FONT></H3><P>Because <TT>CFormView</TT> is a subclass derived from <TT>CView</TT>, you cansubstitute it for <TT>CView</TT> in most cases. As was discussed in Hour 9, a documentclass is associated with a view class using a <TT>CMultiDocTemplate</TT> object inMDI programs. You can change the view associated with a particular document by editingthe parameters used when the <TT>CMultiDocTemplate</TT> object is constructed.</P><P>Listing 23.1 associates the <TT>CFormTest</TT> view class with the <TT>CDVTestDoc</TT>document class. Update the code that creates the document template in the <TT>CDVTestApp::InitInstance</TT>function, found in the <TT>CDVTestApp.cpp</TT> source file. You must change onlythe fourth parameter to the constructor, as shown by the comment.
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -