?? ch08.htm
字號:
of messages sent to the <TT>CMouseTstView</TT> class will be displayed in the Message
list box.<BR>
<BR>
2. Select the <TT>WM_LBUTTONDOWN</TT> message from the Message list box, and click
the Add Function button.<BR>
<BR>
3. Repeat step 2 for the <TT>WM_RBUTTONDOWN</TT>, <TT>WM_LBUTTONDBLCLK</TT>, and
<TT>WM_RBUTTONDBLCLK</TT> messages.<BR>
<BR>
4. Click OK to close ClassWizard.
</DL>
<P>Edit the message-handling functions so they look like the function provided in
Listing 8.5. You must remove some source code provided by ClassWizard in each function.
<H4><FONT COLOR="#000077">TYPE: Listing 8.5. The four mouse-handling functions for
CMouseTstView.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMouseTstView::OnLButtonDblClk(UINT nFlags, CPoint point)</TT>
<TT>{</TT>
<TT> m_ptMouse = point;</TT>
<TT> m_szDescription = "Left Button Double Click";</TT>
<TT> InvalidateRect( NULL );</TT>
<TT>}</TT>
<TT>void CMouseTstView::OnLButtonDown(UINT nFlags, CPoint point)</TT>
<TT>{</TT>
<TT> m_ptMouse = point;</TT>
<TT> m_szDescription = "Left Button Down";</TT>
<TT> InvalidateRect( NULL );</TT>
<TT>}</TT>
<TT>void CMouseTstView::OnRButtonDblClk(UINT nFlags, CPoint point)</TT>
<TT>{</TT>
<TT> m_ptMouse = point;</TT>
<TT> m_szDescription = "Right Button Double Click";</TT>
<TT> InvalidateRect( NULL );</TT>
<TT>}</TT>
<TT>void CMouseTstView::OnRButtonDown(UINT nFlags, CPoint point)</TT>
<TT>{</TT>
<TT> m_ptMouse = point;</TT>
<TT> m_szDescription = "Right Button Down";</TT>
<TT> InvalidateRect( NULL );</TT>
</FONT></PRE>
<P><TT>}</TT> Each of the message-handling functions in Listing 8.5 stores the position
of both the mouse event and a text string that describes the event. Each function
then invalidates the view rectangle. The next step is to use the <TT>CMouseTstView::OnDraw</TT>
function to display the event. Edit <TT>CMouseTstView::OnDraw</TT> so it contains
the source code in Listing 8.6. Remove any existing source code provided by AppWizard.
<H4><FONT COLOR="#000077">TYPE: Listing 8.6. The OnDraw member function for CMouseTstView.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMouseTstView::OnDraw(CDC* pDC)</TT>
<TT>{</TT>
<TT> pDC->TextOut( m_ptMouse.x, m_ptMouse.y, m_szDescription );</TT>
</FONT></PRE>
<P><TT>}</TT> The <TT>OnDraw</TT> member function uses <TT>TextOut</TT> to display
the previously saved event message. The <TT>CPoint</TT> object, <TT>m_ptMouse</TT>,
was used to store the mouse event's position. A <TT>CPoint</TT> object has two member
variables, <TT>x</TT> and <TT>y</TT>, which are used to plot a point in a window.
<H3><FONT COLOR="#000077"><B>Running MouseTst</B></FONT></H3>
<P>Build and run MouseTst, then click the main window's client area. A message is
displayed whenever you click the left or right mouse button.</P>
<P>Figure 8.4 shows the MouseTst program after a mouse button has been clicked.</P>
<P><A NAME="04"></A><A HREF="04.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch08/04.htm"><B>Figure 8.4.</B></A> <I><BR>
The MouseTst program displaying a mouse event.</I>
<H2><FONT COLOR="#000077"><B>What Are MFC Base Classes?</B></FONT></H2>
<P>The MFC class library includes a large number of classes well suited for Windows
programming. Most of these classes are derived from <TT>CObject</TT>, a class that
is at the root of the MFC class hierarchy. In addition, any class that represents
a window or control is derived from the <TT>CWnd</TT> class, which handles basic
functions that are common to all windows.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>The <TT>CObject</TT>
and <TT>CWnd</TT> classes use virtual functions, which enable your program to access
general-purpose functions through a base pointer. This enables you to easily use
any object that is derived from <TT>CObject</TT> or <TT>CWnd</TT> when interacting
with the MFC framework.
<HR>
</BLOCKQUOTE>
<H3><FONT COLOR="#000077"><B>The <TT>CObject</TT> Base Class</B></FONT></H3>
<P>Almost every class used in an MFC program is derived from <TT>CObject</TT>. The
<TT>CObject</TT> class provides four types of services:
<UL>
<LI>Diagnostic memory management provides diagnostic messages when memory leaks are
detected. These leaks are often caused by failing to free objects that have been
dynamically created.<BR>
<BR>
<LI>Dynamic creation support uses the <TT>CRuntimeClass</TT> to enable objects to
be created at runtime. This is different from creating objects dynamically using
the <TT>new</TT> operator.<BR>
<BR>
<LI>Serialization support enables an object to be stored and loaded in an object-oriented
fashion. Serialization is discussed in Hour 22, "Serialization."<BR>
<BR>
<LI>Runtime class information is used by the MFC class library to provide diagnostic
information when errors are discovered in your program. Runtime class information
is also used when you're serializing objects to or from storage.
</UL>
<H3><FONT COLOR="#000077"><B>The <TT>CWnd</TT> Base Class</B></FONT></H3>
<P>The <TT>CWnd</TT> class is derived from <TT>CObject</TT> and adds a great deal
of functionality that is shared by all windows in an MFC program. This also includes
dialog boxes and controls, which are just specialized versions of windows. Figure
8.5 shows some of the major MFC classes derived from <TT>CWnd</TT>.</P>
<P><A NAME="05"></A><A HREF="05.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch08/05.htm"><B>Figure 8.5.</B> </A><I><BR>
Some of the major MFC classes derived from <TT>CWnd</TT>.</I></P>
<P>The <TT>CWnd</TT> class defines functions that can be applied to any <TT>CWnd</TT>
object, including objects that are instances of classes derived from <TT>CWnd</TT>.
As first shown in Hour 5, "Button Controls," to set the caption or title
for any window, including controls, you can use the <TT>CWnd::SetWindowText</TT>
function.</P>
<P>Almost every significant object in an MFC program is a <TT>CObject</TT> instance.
This enables you to take advantage of the MFC support for discovering many common
memory leaks and other types of programming errors. The <TT>CObject</TT> class also
declares functions that can be used to provide diagnostic dumps during runtime and
support for serialization. Serialization is discussed in Hour 22.</P>
<P>Every window in an MFC program is a <TT>CWnd</TT> object. <TT>CWnd</TT> is derived
from <TT>CObject</TT> so it has all the <TT>CObject</TT> functionality built in.
Using the <TT>CWnd</TT> class to handle all controls and windows in your program
enables you to take advantage of polymorphism; the <TT>CWnd</TT> class provides all
the general window functions for all types of windows. This means you don't need
to know exactly what type of control or window is accessed through a <TT>CWnd</TT>
pointer in many cases.
<H3><FONT COLOR="#000077"><B>An Example Using the <TT>CObject</TT> and <TT>CWnd</TT>
Base Classes</B></FONT></H3>
<P>The <TT>CObject</TT> and <TT>CWnd</TT> classes are used in different ways. The
<TT>CObject</TT> class is normally used as a base class when you create your own
classes. The <TT>CWnd</TT> class is often passed as a function parameter or return
value and is used as a generic pointer to any type of window in an MFC program.</P>
<P>In this section, you create a sample console mode project that demonstrates how
the <TT>CObject</TT> class is used. To start the sample, create a new console mode
project named Runtime. In addition, configure the project so that it uses the MFC
class library by following these steps:
<DL>
<DD>1. Select Settings from the Project menu. This opens the Project Settings dialog
box.<BR>
<BR>
2. Click the General tab.<BR>
<BR>
3. Select Use MFC in a Shared DLL from the Microsoft Foundation Classes combo box.<BR>
<BR>
4. Close the dialog box by clicking OK.
</DL>
<H4><FONT COLOR="#000077">Using CObject as a Base Class</FONT></H4>
<P>The <TT>CObject</TT> class is always used as a base class; there isn't really
anything that can be done with a plain <TT>CObject</TT>. When used as a base class,
the <TT>CObject</TT> class provides a great deal of basic functionality to a class.
You can control the amount of functionality provided by <TT>CObject</TT> by using
macros in the derived class's declaration and definition files.</P>
<P>Four different levels of support are offered by <TT>CObject</TT> to its derived
classes:
<UL>
<LI>Basic support with memory leak detection requires no macros.<BR>
<BR>
<LI>Support for runtime class identification requires the use of the <TT>DECLARE_DYNAMIC</TT>
macro in the class declaration and the <TT>IMPLEMENT_DYNAMIC</TT> macro in the class
definition.<BR>
<BR>
<LI>Support for dynamic object creation requires the use of the <TT>DECLARE_DYNCREATE</TT>
macro in the class declaration and the <TT>IMPLEMENT_DYNCREATE</TT> macro in the
class definition. The use of dynamic object creation is discussed later in this hour.<BR>
<BR>
<LI>Serialization support requires the use of the <TT>DECLARE_SERIAL</TT> macro in
the class declaration and the <TT>IMPLEMENT_SERIAL</TT> macro in the class definition.
The use of serialization is discussed in Hour 22.
</UL>
<P>Each of the <TT>CObject</TT> macros is used in a similar way. All <TT>DECLARE</TT>
macros have one parameter--the name of the class. The <TT>IMPLEMENT</TT> macros generally
take two parameters--the name of the class and the name of the immediate base class.
<TT>IMPLEMENT_SERIAL</TT> is an exception because it requires three parameters, as
discussed in Hour 22.</P>
<P>Listing 8.7 is the class declaration for <TT>CMyObject</TT>, a simple class that
is derived from <TT>CObject</TT>. The <TT>CMyObject</TT> class supports dynamic creation,
so it includes the <TT>DECLARE_DYNCREATE</TT> macro.
<H4><FONT COLOR="#000077">TYPE: Listing 8.7. The CMyObject class declaration, using
CObject as a base class.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>class CMyObject : public CObject</TT>
<TT>{</TT>
<TT> DECLARE_DYNCREATE( CMyObject );</TT>
<TT>// Constructor</TT>
<TT>public:</TT>
<TT> CMyObject();</TT>
<TT>//Attributes</TT>
<TT>public:</TT>
<TT> void Set( const CString& szName );</TT>
<TT> CString Get() const;</TT>
<TT>//Implementation</TT>
<TT>private:</TT>
<TT> CString m_szName;</TT>
</FONT></PRE>
<P><TT>};</TT> Save the source code from Listing 8.7 in the Runtime project directory
as <TT>MyObj.h</TT>. It's just an <TT>include</TT> file, so don't add it to the project.</P>
<P>The source code for the <TT>CMyObject</TT> member functions is provided in Listing
8.8. Save this source code as <TT>MyObj.cpp</TT> and add it to the Runtime project.
This source file contains the <TT>IMPLEMENT_DYNCREATE</TT> macro that matches the
<TT>DECLARE_DYNCREATE</TT> macro from the class declaration.
<H4><FONT COLOR="#000077">TYPE: Listing 8.8. Member functions for the CMyObject class.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>#include <afx.h></TT>
<TT>#include "MyObj.h"</TT>
<TT>IMPLEMENT_DYNCREATE( CMyObject, CObject );</TT>
<TT>CMyObject::CMyObject()</TT>
<TT>{</TT>
<TT>}</TT>
<TT>void CMyObject::Set( const CString& szName )</TT>
<TT>{</TT>
<TT> m_szName = szName;</TT>
<TT>}</TT>
<TT>CString CMyObject::Get() const</TT>
<TT>{</TT>
<TT> return m_szName;</TT>
</FONT></PRE>
<P><TT>}</TT>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -