按键盘上方向键 ← 或 → 可快速上下翻页,按键盘上的 Enter 键可回到本书目录页,按键盘上方向键 ↑ 可回到本页顶部!
————未阅读完?加入书签已便下次继续阅读!
                             CDocument                 CArray 
                               CScribbleDoc         CStroke             : defined in Scribble 
                                CScribbleDoc        CStroke 
                                                     。 
                                  图8…3b Scribble Step1 文件所使用的类别 
474 
…………………………………………………………Page 537……………………………………………………………
CScribbleDoc  内嵌一个CObList 对象,CObList 串行中的每个元素都是一个CStroke 对 
象指针,而CStroke 又内嵌一个CArray 对象。下面是Step1 程序的Document 设计。 
SCRIBBLEDOC。H (阴影表示与Step0的差异) 
#0001  ///////////////////////////////////////////////////////////////// 
#0002  // class CStroke 
#0003  // 
#0004  // A stroke is a series of connected points in the scribble drawing。 
#0005  // A scribble document may have multiple strokes。 
#0006 
#0007  class CStroke : public CObject 
#0008  { 
#0009  public: 
#0010          CStroke(UINT nPenWidth); 
#0011 
#0012  protected: 
#0013          CStroke(); 
#0014          DECLARE_SERIAL(CStroke) 
#0015 
#0016  // Attributes 
#0017  protected: 
#0018          UINT   m_nPenWidth;    // one pen width applies to entire stroke 
#0019  public: 
#0020          CArray  m_pointArray;   // series of connected 
points 
#0021 
#0022  // Operations 
#0023  public: 
#0024          BOOL DrawStroke(CDC* pDC); 
#0025 
#0026  public: 
#0027          virtual void Serialize(CArchive& ar); 
#0028  }; 
#0029 
#0030  ///////////////////////////////////////////////////////////////// 
#0031 
#0032  class CScribbleDoc : public CDocument 
#0033  { 
#0034  protected: // create from serialization only 
#0035          CScribbleDoc(); 
#0036          DECLARE_DYNCREATE(CScribbleDoc) 
#0037 
#0038  // Attributes 
#0039  protected: 
                                                                                     475 
…………………………………………………………Page 538……………………………………………………………
                   第篇    深入  MFC  程式設計 
                   #0040          // The document keeps track of the current pen width on 
                   #0041          // behalf of all views。 We'd like the user interface of 
                   #0042          // Scribble to be such that if the user chooses the Draw 
                   #0043          // Thick Line mand; it will apply to all views; not just 
                   #0044          // the view that currently has the focus。 
                   #0045 
                   #0046          UINT   m_nPenWidth;    // current user…selected pen width 
                   #0047          CPen   m_penCur;       // pen created according to 
                   #0048                                   // user…selected pen style (width) 
                   #0049  public: 
                   #0050          CTypedPtrList     m_strokeList; 
                   #0051          CPen*           GetCurrentPen() { return &m_penCur; } 
                   #0052 
                   #0053  // Operations 
                   #0054  public: 
                   #0055          CStroke* NewStroke(); 
                   #0056 
                   #0057  // Overrides 
                   #0058          // ClassWizard generated virtual function overrides 
                   #0059          //{{AFX_VIRTUAL(CScribbleDoc) 
                   #0060          public: 
                   #0061          virtual BOOL OnNewDocument(); 
                   #0062          virtual void Serialize(CArchive& ar); 
                   #0063          virtual BOOL OnOpenDocument(LPCTSTR lpszPathName); 
                   #0064          virtual void DeleteContents(); 
                   #0065          //}}AFX_VIRTUAL 
                   #0066 
                   #0067  // Implementation 
                   #0068  public: 
                   #0069          virtual ~CScribbleDoc(); 
                   #0070  #ifdef _DEBUG 
                   #0071          virtual void AssertValid() const; 
                   #0072          virtual void Dump(CDumpContext& dc) const; 
                   #0073  #endif 
                   #0074 
                   #0075  protected: 
                   #0076          void  InitDocument(); 
                   #0077 
                   #0078  // Generated message map functions 
                   #0079  protected: 
                   #0080    //{{AFX_MSG(CScribbleDoc) 
                   #0081      // NOTE the ClassWizard will add and remove member functions here。 
                   #0082      //    DO NOT EDIT what you see in these blocks of generated code ! 
                   #0083    //}}AFX_MSG 
                   #0084    DECLARE_MESSAGE_MAP() 
                   #0085  }; 
                   如果你把本书第一版(使用VC++ 4。0 )的Scribble step1 原封不动地在VC++ 4。2 或 
476 
…………………………………………………………Page 539……………………………………………………………
VC++ 5。0  中编译,你会获得好几个编译错误。问题出在SCRIBBLEDOC。H 文件: 
// forward declaration of data structure class 
class CStroke; 
class CScribbleDoc : public CDocument 
{ 
    。。。 
}; 
class CStroke : public CObject 
{ 
    。。。 
}; 
 并不是程序设计上有什么错误,你只要把CStroke 的声明由CScribbleDoc 之后搬移到 
 CScribbleDoc 之前即可。由此观之,VC++ 4。2 和VC++ 5。0  的编译器似乎不支持forward 
 declaration 。真是没道理! 
SCRIBBLEDOC。CPP            (阴影表示与Step0 的差异) 
#0001  #include 〃stdafx。h〃 
#0002  #include 〃Scribble。h〃 
#0003 
#0004  #include 〃ScribbleDoc。h〃 
#0005 
#0006  #ifdef _DEBUG 
#0007  #define new DEBUG_NEW 
#0008  #undef THIS_FILE 
#0009  static char THIS_FILE'' = __FILE__; 
#0010  #endif 
#0011 
#0012  ///////////////////////////////////////////////////////////////// 
#0013  // CScribbleDoc 
#0014 
#0015  IMPLEMENT_DYNCREATE(CScribbleDoc; CDocument) 
#0