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

📄 ch04.htm

📁 Using Visual C++ 6.0 一本关于Visual C++ 6.0基本编程应用的书籍。
💻 HTM
📖 第 1 页 / 共 3 页
字号:
	<DT></DT>
	<DD><B>3. </B>Click the Edit Code button to jump to the OnLButtonDown() function
	in your code. Then, add the code shown in Listing 4.5 to the function.
	<P>
</DL>

<H4>Listing 4.5&#160;&#160;RECSVIEW.CPP--CRecsView::OnLButtonDown()</H4>
<PRE>void CRecsView::OnLButtonDown(UINT nFlags, CPoint point)
{
    CRecsDoc *pDoc = GetDocument();
    // don't go past the end of the 100 points allocated
    if (pDoc-&gt;m_pointIndex == 100)
       return;
    //store the click location
    pDoc-&gt;m_points[pDoc-&gt;m_pointIndex] = point;
    pDoc-&gt;m_pointIndex++;
    pDoc-&gt;SetModifiedFlag();
    Invalidate();
    CView::OnLButtonDown(nFlags, point);
</PRE>
<PRE>}
</PRE>
<P>The new OnLButtonDown() adds a point to the document's point array each time the
user clicks the left mouse button over the view window. It increments m_pointIndex
so that the next click goes into the point on the array after this one.</P>
<P>The call to SetModifiedFlag() marks this document as modified, or &quot;dirty.&quot;
MFC automatically prompts the user to save any dirty files on exit. (The details
are found in Chapter 7.) Any code you write that changes any document variables should
call SetModifiedFlag().</P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>NOTE:</strong> Earlier in this chapter you were reminded that public access functions
	in the document have some advantages. One such advantage: Any document member function
	that changed a variable also could call SetModifiedFlag(), thus guaranteeing no programmer
	could forget it.&#160;n
<HR>


</BLOCKQUOTE>

<P>Finally, the call to Invalidate() causes MFC to call the OnDraw() function, where
the window's display is redrawn with the new data. Invalidate() takes a single parameter
(with the default value TRUE) that determines if the background is erased before
calling OnDraw(). On rare occasions you may choose to call Invalidate(FALSE) so that
OnDraw() draws over whatever was already onscreen.</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 Build, Build from the menu bar, to compile and link the application. After
you have the Rectangles application compiled and linked, run it by choosing Build,
Execute. 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 4.2).</P>
<P><A HREF="javascript:popUp('04uvc02.gif')"><B>FIG. 4.2</B></A><B> </B><I>The Rectangles
application draws rectangles wherever you click.</I></P>

<P><I></I>
<H2><A NAME="Heading4"></A>Other View Classes</H2>
<P>The view classes generated by AppWizard in this chapter's sample applications
have been derived from MFC's CView class. There are cases, however, when it is to
your advantage to derive your view class from one of the other MFC view classes derived
from CView. These additional classes provide your view window with special capabilities
such as scrolling and text editing. Table 4.1 lists the various view classes along
with their descriptions.</P>
<P>
<H4>Table 4.1&#160;&#160;View Classes</H4>
<P>
<TABLE BORDER="1">
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT"><B>Class</B></TD>
		<TD ALIGN="LEFT"><B>Description</B></TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CView		</TD>
		<TD ALIGN="LEFT">The base view class from which the specialized view classes are derived		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CCtrlView		</TD>
		<TD ALIGN="LEFT">A base class from which view classes that implement 32-bit Windows common controls
			(such as the ListView, TreeView, and RichEdit controls) are derived		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CDaoRecordView		</TD>
		<TD ALIGN="LEFT">Same as CRecordView, except used with the OLE DB database classes		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CEditView		</TD>
		<TD ALIGN="LEFT">A view class that provides basic text-editing features		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CFormView		</TD>
		<TD ALIGN="LEFT">A view class that implements a form-like window using a dialog box resource		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CHtmlView		</TD>
		<TD ALIGN="LEFT">A view class that can display HTML, with all the capabilities of Microsoft Internet
			Explorer		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CListView		</TD>
		<TD ALIGN="LEFT">A view class that displays a ListView control in its window		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">COleDBRecordView		</TD>
		<TD ALIGN="LEFT">Same as CRecordView, except used with the DAO database classes		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CRecordView		</TD>
		<TD ALIGN="LEFT">A view class that can display database records along with controls for navigating
			the database		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CRichEditView		</TD>
		<TD ALIGN="LEFT">A view class that provides more sophisticated text-editing capabilities by using
			the RichEdit control		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CScrollView		</TD>
		<TD ALIGN="LEFT">A view class that provides scrolling capabilities		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">CTreeView		</TD>
		<TD ALIGN="LEFT">A view class that displays a TreeView control in its window		</TD>
	</TR>
</TABLE>
</P>
<P>To use one of these classes, substitute the desired class for the CView 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 4.3. When 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. Chapter
5 demonstrates using the CScrollView class to implement a scrolling view.</P>
<P>A CEditView 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 GetPrinterFont() or SetPrinterFont() member function
or get the currently selected text by calling GetSelectedText(). Moreover, the FindText()
member function locates a given text string, and OnReplaceAll() replaces all occurrences
of a given text string with another string.</P>
<P><A HREF="javascript:popUp('04uvc03.gif')"><B>FIG. 4.3</B></A><B> </B><I>You can
use AppWizard to select your application's base view class.</I></P>

<P>The CRichEditView 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 might have guessed, the CRichEditView class features a rich
set of methods you can use to control your application's view object.</P>
<P>Figure 4.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>
<P><A HREF="javascript:popUp('04uvco04.gif')"><B>FIG. 4.4</B></A><B> </B><I>The view
classes all trace their ancestry back to CView.</I></P>

<P><I></I>
<H2><A NAME="Heading5"></A>Document Templates, Views, and Frame Windows</H2>
<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 InitInstance() method of the Rectangles application's
CRecsApp class, you see (among other things) the lines shown in Listing 4.6.</P>
<P>
<H4>Listing 4.6&#160;&#160;RECS.CPP--Initializing an Application's Document</H4>
<PRE>CSingleDocTemplate* pDocTemplate;
pDocTemplate = new CSingleDocTemplate(
     IDR_MAINFRAME,
     RUNTIME_CLASS(CRecsDoc),
     RUNTIME_CLASS(CMainFrame),
     RUNTIME_CLASS(CRecsView));
</PRE>
<PRE>AddDocTemplate(pDocTemplate);
</PRE>
<P>In Listing 4.6, you discover one secret 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, discussed in Chapter 26, &quot;Exceptions
and Templates.&quot; A document template is an older concept, named before C++ templates
were implemented by Microsoft, that pulls together the following objects:</P>
<P>

<UL>
	<LI>A resource ID identifying a menu resource--IDR_MAINFRAME in this case
	<P>
	<LI>A document class--CRecsDoc in this case
	<P>
	<LI>A frame window class--always CMainFrame
	<P>
	<LI>A view class--CRecsView in this case
</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 RUNTIME_CLASS. 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 DECLARE_DYNCREATE macro in its declaration
(in the header file) and the IMPLEMENT_DYNCREATE 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 CMainFrame
class, you see the following line near the top of the class's declaration:</P>
<P>
<PRE>DECLARE_DYNCREATE(CMainFrame)
</PRE>
<P>As you can see, the DECLARE_DYNCREATE macro requires the class's name as its single
argument.</P>
<P>Now, if you look near the top of CMainFrame's implementation file (MAINFRM.CPP),
you see this line:</P>
<P>
<PRE>IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
</PRE>
<P>The IMPLEMENT_DYNCREATE 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 DECLARE_DYNCREATE and IMPLEMENT_DYNCREATE 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 Chapter 3.</P>
<P>The last line of Listing 4.6 calls AddDocTemplate() to pass the object on to the
application object, CRecsApp, which keeps a list of documents. AddDocTemplate() adds
this document to this list and uses the document template to create the document
object, the frame, and the view window.</P>

<P>Because this is a Single Document Interface, a single document template (CSingleDocTemplate)
is created. Multiple Document Interface applications use one CMultiDocTemplate 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 CMultiDocTemplate would be created in InitInstance(),
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, you know how you can achieve the same effect: Simply associate them with
different menu resource IDs as you build the document templates.</P>
<H1></H1>
<CENTER>
<P>
<HR>
<A HREF="../ch03/ch03.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch05/ch05.htm"><IMG
SRC="../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> <BR>
<BR>
</P>

<P>&#169; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

⌨️ 快捷键说明

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