友情提示:如果本网页打开太慢或显示不完整,请尝试鼠标右键“刷新”本网页!
第三电子书 返回本书目录 加入书签 我的书架 我的书签 TXT全本下载 『收藏到我的浏览器』

深入浅出MFC第2版(PDF格式)-第130部分

快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部! 如果本书没有阅读完,想下次继续接着阅读,可使用上方 "收藏到我的浏览器" 功能 和 "加入书签" 功能!




                    RUNTIME_CLASS(CGraphView) 呢? 后者当然也可以, 但却因此必须包含 



                    CGraphView 的声明;而如果你因为这个原因而包含GraphView。h 档,又会产生三个 



                    编译错误,挺麻烦! 



                    至此,Document  中虽然没有任何资料,但程序的UI  已经完备,编译联结后 

                  



                  可得以下执行画面: 



                   修改CGraphDoc,增加一个整数数组m_intArray ,这是真正存放资料的地方, 



                   我采用MFC  内建的CArray,为此,必须在STDAFX。H 中加上一行: 



                    #include        // MFC templates 



718 


…………………………………………………………Page 781……………………………………………………………

                                                      第 13 章    多重文件與多重顯示 



为了设定数组内容,我又增加了一个SetValue 成员函数,并且在【Graph Data】菜单 



命令被执行时,为m_intArray 设定不同的初值: 



    // in GRAPHDOC。H 

   class CGraphDoc : public CDocument 

    { 

    。。。 

   public: 

           CArray m_intArray; 



   public: 

           void SetValue(int i0; int i1; int i2; int i3; int i4; 

                           int i5; int i6; int i7; int i8; int i9); 

    。。。 

    }; 



    // in GRAPHDOC。CPP 

   CGraphDoc::CGraphDoc() 

    { 

           SetValue(5; 10; 15; 20; 25; 78; 64; 38; 29; 9); 

    } 

   void CGraphDoc::SetValue(int i0; int i1; int i2; int i3; int i4; 

                                 int i5; int i6; int i7; int i8; int i9); 

    { 

           m_intArray。SetSize(DATANUM; 0); 

           m_intArray'0' = i0; 

           m_intArray'1' = i1; 

           m_intArray'2' = i2; 

           m_intArray'3' = i3; 

           m_intArray'4' = i4; 

           m_intArray'5' = i5; 

           m_intArray'6' = i6; 

           m_intArray'7' = i7; 

           m_intArray'8' = i8; 

           m_intArray'9' = i9; 

    } 

   void CGraphDoc::OnGraphData1() 

    { 

           SetValue(5; 10; 15; 20; 25; 78; 64; 38; 29; 9); 

           UpdateAllViews(NULL); 

    } 

   void CGraphDoc::OnGraphData2() 

    { 

           SetValue(50; 60; 70; 80; 90; 23; 68; 39; 73; 58); 



                                                                                    719 


…………………………………………………………Page 782……………………………………………………………

                     第篇    深入  MFC  程式設計 



                                UpdateAllViews(NULL); 

                         } 

                        void CGraphDoc::OnGraphData3() 

                         { 

                                SetValue(12; 20; 8; 17; 28; 37; 93; 45; 78; 29); 

                                UpdateAllViews(NULL); 

                         } 

                        void CGraphDoc::OnUpdateGraphData1(CCmdUI* pCmdUI) 

                         { 

                                pCmdUI…》SetCheck(m_intArray'0' == 5); 

                         } 

                        void CGraphDoc::OnUpdateGraphData2(CCmdUI* pCmdUI) 

                         { 

                                pCmdUI…》SetCheck(m_intArray'0' == 50); 

                         } 

                        void CGraphDoc::OnUpdateGraphData3(CCmdUI* pCmdUI) 

                         { 

                                pCmdUI…》SetCheck(m_intArray'0' == 12); 

                         } 



                        各位看到,为了方便,我把m_intArray  的数据封装属性设为public  而非private , 



                        检查「m_intArray  内容究竟是哪一份资料」所用的方法也非常粗糙,呀,不要非难我, 



                        重点不在这里呀! 



                       在RESOURCE。H 文件中加上两个常数定义: 



                         #define DATANUM  10 

                         #define DATAMAX  100 



                            

                      修改CGraphView,在OnDraw 成员函数中取得Document ,再透过Document 



                      对象指针取得整数数组,然后将10 笔数据的曲线图绘出: 



                        #0001  void CGraphView::OnDraw(CDC* pDC) 

                        #0002  { 

                        #0003      CGraphDoc* pDoc = GetDocument(); 

                        #0004      ASSERT_VALID(pDoc); 

                        #0005 

                        #0006      int        cxDot;cxDotSpacing;cyDot; cxGraph;cyGraph; x;y; i; 

                        #0007      RECT       rc; 

                        #0008 

                        #0009      CPen   pen (PS_SOLID; 1; RGB(255; 0; 0)); // red pen 

                        #0010      CBrush brush(RGB(255; 0; 0));             // red brush 



720 


…………………………………………………………Page 783……………………………………………………………

                                                     第 13 章    多重文件與多重顯示 



   #0011      CBrush* pOldBrush = pDC…》SelectObject(&brush); 

   #0012      CPen*   pOldPen = pDC…》SelectObject(&pen); 

   #0013 

   #0014      cxGraph = 100; 

   #0015      cyGraph = DATAMAX;  // defined in resource。h 

   #0016 

   #0017      this…》GetClientRect(&rc); 

   #0018      pDC…》SetMapMode(MM_ANISOTROPIC); 

   #0019      pDC…》SetWindowOrg(0; 0); 

   #0020      pDC…》SetViewportOrg(10; rc。bottom…10); 

   #0021      pDC…》SetWindowExt(cxGraph; cyGraph); 

   #0022      pDC…》SetViewportExt(rc。right…20; …(rc。bottom…20)); 

   #0023 

   #0024      // 我们希望图形占满窗口的整个可用空间(以水平方向为准) 

   #0025      // 并希望曲线点的宽度是点间距宽度的1。2, 

   #0026      // 所以 (dot_spacing + dot_width)*num_datapoints=graph_width 

   #0027      // 亦即dot_spacing * 3/2 * num_datapoints = graph_width 

   #0028      // 亦即dot_spacing = graph_width / num_datapoints * 2/3 

   #0029 

   #0030      cxDotSpacing = (2 * cxGraph) / (3 * DATANUM); 

   #0031      cxDot = cxDotSpacing/2; 

   #0032      if  (cxDotMoveTo(0; 0); 

   #0037      pDC…》LineTo(0; cyGraph); 

   #0038      pDC…》MoveTo(0; 0); 

   #0039      pDC…》LineTo(cxGraph; 0); 

   #0040 

   #0041      //画出资料点 

   #0042      pDC…》SelectObject(::GetStockObject  (NULL_PEN)); 

   #0043      for  (x=0+cxDotSpacing;y=0;i=0; iRectangle(x; y+pDoc…》m_intArray'i'; 

   #0045                         x+cxDot; y+pDoc…》m_intArray'i'…cyDot); 

   #0046 

   #0047     pDC…》SelectObject(pOldBrush); 

   #0048     pDC…》SelectObject(pOldPen); 

   #0049  } 



  修改CTextView 程序代码, 在OnDraw 成员函数中取得Document , 再透过 



  Document 对象指针取得整数数组,然后将10 笔资料以文字方式显示出来: 



   #0001  #include 〃stdafx。h〃 

   #0002  #include 〃Graph。h〃 



                                                                                   721 


…………………………………………………………Page 784……………………………………………………………

                     第篇    深入  MFC  程式設計 



                         #0003  #include 〃GraphDoc。h〃 

                         #0004  #include 〃TextView。h〃 

                         #0005  。。。 

                         #0006  void CTextView::OnDraw(CDC* pDC) 

                         #0007  { 

                         #0008      CGraphDoc* pDoc =  (CGraphDoc*)GetDocument(); 

                         #0009 

                         #0010      TEXTMETRIC   tm; 

                         #0011      int          x;y; cy; i; 

                         #0012      char         sz'20'; 

                         #0013      pDC…》GetTextMetrics(&tm); 

                         #0014      cy = tm。tmHeight; 

                         #0015      pDC…》SetTextColor(RGB(255; 0; 0));   // red text 

                         #0016      for (x=5;y=5;i=0; im_intArray'i'); 

                         #0019          pDC…》TextOut (x;y; sz; lstrlen(sz)); 

                         #0020      } 

                         #0021  } 



                         修改 CBarView       程式碼,在 OnDraw            成員函式取得                         ,再透過 

                                                                                        Document 



                         Document  物件指標取得整數陣列,然後將 10  筆資料以長條圖繪出: 



                         #0001  #include 〃stdafx。h〃 

                         #0002  #include 〃Graph。h〃 

                         #0003  #include 〃GraphDoc。h〃 

                         #0004  #include 〃TextView。h〃 

                         #0005  。。。 

                         #0006  void CBarView::OnDraw(CDC* pDC) 

                         #0007  { 

                         #0008      CGraphDoc* pDoc =  (CGraphDoc*)GetDocument(); 

                         #0009 

                         #0010      int          cxBar;cxBarSpacing; cxGraph;cyGraph; x;y; i; 

                         #0011      RECT         rc; 

                         #0012 

                         #0013      CBrush brush(RGB(255; 0; 0));  // red brush 

                         #0014      CBrush* pOldBrush = pDC…》SelectObject(&brush); 

                         #0015      CPen pen(PS_SOLID; 1; RGB(255; 0; 0)); // red pen 

                         #0016      CPen* pOldPen = pDC…》SelectObject(&pen); 

                         #0017 

                         #0018      cxGraph = 100; 

                         #0019      cyGraph = DATAMAX;  // defined in resource。h 

                         #0020 

                         #0021      this…》GetClientRect(&rc); 

                         #0022      pDC…》SetMapMode(MM_ANISOTROPIC); 



722 


…………………………………………………………Page 785……………………………………………………………

                                                    第 13 章    多重文件與多重顯示 



   #0023      pDC…》SetWindowOrg(0; 0); 

   #0024      pDC…》SetViewportOrg(10; rc。bottom…10); 

   #0025      pDC…》SetWindowExt(cxGraph; cyGraph); 

   #0026      pDC…》SetViewportExt(rc。right…20; …(rc。bottom…20)); 

   #0027 

   #0028      //长条图的条状物之间距离是条状物宽度的1/3。 

   #0029      //我们希望条状物能够填充窗口的整个可用空间。 

   #0030      //所以(bar_spacing + bar_width) * num_bars = graph_width 

   #0031      //亦即bar_width * 4/3 * num_bars = graph_width 

   #0032      //亦即bar_width = graph_width / num_bars * 3/4 

   #0033 

   #0034      cxBar = (3 * cxGraph) / (4 * DATANUM); 

   #0035      cxBarSpacing = cxBar/3; 

   #0036      if (cxBarMoveTo(0; 0); 

   #0040      pDC…》LineTo(0; cyGraph); 

   #0041      pDC…》MoveTo(0; 0); 

   #0042      pDC…》LineTo(cxGraph; 0); 

   #0043 

   #0044      //长条图 

   #0045      for (x=0+cxBarSpacing;y=0;i=0; i《 DATANUM; i++;x+=cxBar+cxBarSpacing) 

   #0046          pDC…》Rectangle(x; y; x+cxBar; y+pDoc…》m_intArray'i'); 

   #0047 

   #0048     pDC…》Se
返回目录 上一页 下一页 回到顶部 0 0
快捷操作: 按键盘上方向键 ← 或 → 可快速上下翻页 按键盘上的 Enter 键可回到本书目录页 按键盘上方向键 ↑ 可回到本页顶部!
温馨提示: 温看小说的同时发表评论,说出自己的看法和其它小伙伴们分享也不错哦!发表书评还可以获得积分和经验奖励,认真写原创书评 被采纳为精评可以获得大量金币、积分和经验奖励哦!