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

📄 chap8_3.htm

📁 VC++编程实例。非常详细
💻 HTM
📖 第 1 页 / 共 3 页
字号:
          <p>void CDrawView::OnDraw(CDC* pDC)</p>
          <p>{</p>
          <p>CDrawDoc* pDoc = GetDocument();</p>
          <p>ASSERT_VALID(pDoc);</p>
          
          <p><b> </b></p>
          <b> 
          <p>// Get the invalidated rectangle of the view, or in the case</p>
          <p>// of printing, the clipping region of the printer dc.</p>
          <p>CRect rectClip;</p>
          <p>CRect rectStroke;</p>
          <p>pDC-&gt;GetClipBox(&amp;rectClip);</p>
          <p>pDC-&gt;LPtoDP(&amp;rectClip);</p>
          <p>rectClip.InflateRect(1, 1); // avoid rounding to nothing</p>
          <p>// Note: CScrollView::OnPaint() will have already adjusted the</p>
          <p>// viewport origin before calling OnDraw(), to reflect the</p>
          <p>// currently scrolled position.</p>
          <p>// The view delegates the drawing of individual strokes to</p>
          <p>// CStroke::DrawStroke().</p>
          <p>CTypedPtrList&lt;CObList,CStroke*&gt;&amp; strokeList = pDoc-&gt;m_strokeList;</p>
          <p>POSITION pos = strokeList.GetHeadPosition();</p>
          <p>while (pos != NULL)</p>
          <p>{</p>
          <p>CStroke* pStroke = strokeList.GetNext(pos);</p>
          <p>rectStroke = pStroke-&gt;GetBoundingRect();</p>
          <p>pDC-&gt;LPtoDP(&amp;rectStroke);</p>
          <p>rectStroke.InflateRect(1, 1); // avoid rounding to nothing</p>
          <p>if (!rectStroke.IntersectRect(&amp;rectStroke, &amp;rectClip))</p>
          <p>continue;</p>
          <p>pStroke-&gt;DrawStroke(pDC);</p>
          <p>}</p>
          </b> 
          <p>// TODO: add draw code for native data here</p>
          <p>}</p>
          
          <p>OnDraw首先调用GetClipBox取得当前被剪裁区域(无效矩形区域),它把矩形复制导GetClipBox的参数rectClip中。然后将rectClip的坐标由逻辑坐标转换为设备坐标。为了防止该矩形太小而无法包围其他内容,上下各放大一个单位。然后OnDraw遍历笔划链表中的所有笔划,获取它们的最小矩形,用IntersectRect看它是否与无效矩形相交。如果相交,说明笔划的部分或全部落在无效矩形中,此时调用笔划的DrawStroke方法画出该笔划。</p>
          <p> <b>图8-6</b> 
            根据包围笔划 的矩形是否与无效</p>
          <p> 矩形相交 
            ,判断笔划是否落入无效矩形中 </p>
          <p> 为了获得笔划的最小包围矩形,需要在结束笔划时计算出包围笔划的最小矩形。因此为笔划提供两个方法:一个是FinishStroke(),用于在笔划结束时计算最小矩形,见清单8.7。</p>
          
          <p> <b>清单</b><b>8.7 
            CStroke::FinishStroke()成员函数</b></p>
           
          <p>void CStroke::FinishStroke()</p>
          <p>{</p>
          <p>// Calculate the bounding rectangle. It's needed for smart</p>
          <p>// repainting.</p>
          <p>if (m_pointArray.GetSize()==0)</p>
          <p>{</p>
          <p>m_rectBounding.SetRectEmpty();</p>
          <p>return;</p>
          <p>}</p>
          <p>CPoint pt = m_pointArray[0];</p>
          <p>m_rectBounding = CRect(pt.x, pt.y, pt.x, pt.y);</p>
          <p>for (int i=1; i &lt; m_pointArray.GetSize(); i++)</p>
          <p>{</p>
          <p>// If the point lies outside of the accumulated bounding</p>
          <p>// rectangle, then inflate the bounding rect to include it.</p>
          <p>pt = m_pointArray[i];</p>
          <p>m_rectBounding.left = min(m_rectBounding.left, pt.x);</p>
          <p>m_rectBounding.right = max(m_rectBounding.right, pt.x);</p>
          <p>m_rectBounding.top = max(m_rectBounding.top, pt.y);</p>
          <p>m_rectBounding.bottom = min(m_rectBounding.bottom, pt.y);</p>
          <p>}</p>
          <p>// Add the pen width to the bounding rectangle. This is necessary</p>
          <p>// to account for the width of the stroke when invalidating</p>
          <p>// the screen.</p>
          <p>m_rectBounding.InflateRect(CSize(m_nPenWidth, -(int)m_nPenWidth));</p>
          <p>return;</p>
          <p>}</p>
          
          <p>另一个是DrawStroke(),用于绘制笔划:</p>
           
          <p>BOOL CStroke::DrawStroke(CDC* pDC)</p>
          <p>{</p>
          <p>CPen penStroke;</p>
          <p>if (!penStroke.CreatePen(PS_SOLID, m_nPenWidth, RGB(0,0,0)))</p>
          <p>return FALSE;</p>
          <p>CPen* pOldPen = pDC-&gt;SelectObject(&amp;penStroke);</p>
          <p>pDC-&gt;MoveTo(m_pointArray[0]);</p>
          <p>for (int i=1; i &lt; m_pointArray.GetSize(); i++)</p>
          <p>{</p>
          <p>pDC-&gt;LineTo(m_pointArray[i]);</p>
          <p>}</p>
          <p>pDC-&gt;SelectObject(pOldPen);</p>
          <p>return TRUE;</p>
          <p>}</p>
          
          <p>鼠标绘图</p>
          <p align="JUSTIFY"> 鼠标绘图基本过程是:用户按下鼠标左键时开始绘图,在鼠标左键按下且移动过程中不断画线跟踪鼠标位置,当松开鼠标左键结束绘图。因此,需要处理三个消息:WM_LBUTTONDOWN、WM_MOUSEMOVE、WM_LBUTTONUP。用ClassWizard为上述三个消息生成消息处理函数,并在其中手工加入代码,修改后的成员函数如下:</p>
          <p align="JUSTIFY"> <b> </b></p>
          <b>
          <p align="JUSTIFY"> 清单8.8 
            鼠标消息处理函数OnLButtonDown()</p>
          </b> 
          <p>void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) </p>
          <p>{</p>
          <p>// TODO: Add your message handler code here and/or call default</p>
          <p>// Pressing the mouse button in the view window starts a new stroke</p>
          <p>// CScrollView changes the viewport origin and mapping mode.</p>
          <p>// It's necessary to convert the point from device coordinates</p>
          <p>// to logical coordinates, such as are stored in the document.</p>
          <p>CClientDC dc(this);</p>
          <p>OnPrepareDC(&amp;dc);</p>
          <p>dc.DPtoLP(&amp;point);</p>
          <p>m_pStrokeCur = GetDocument()-&gt;NewStroke();</p>
          <p>// Add first point to the new stroke</p>
          <p>m_pStrokeCur-&gt;m_pointArray.Add(point);</p>
          <p>SetCapture(); // Capture the mouse until button up.</p>
          <p>m_ptPrev = point; // Serves as the MoveTo() anchor point for the</p>
          <p>// LineTo() the next point, as the user drags the</p>
          <p>// mouse.</p>
          <p>return;</p>
          <p>}</p>
          
          <p> </p>
          <p> 在鼠标左键按下,首先获得鼠标按下的位置坐标。由于它是设备坐标,因此先用DPToLP将它转换为逻辑坐标。在此之前,要用OnPrepareDC()对视图坐标原点进行调整。然后用CDrawDoc的NewStroke()成员函数创建一个笔划对象,并将笔划对象加入到笔划链表中。然后,将当前点坐标加入道笔划对象内部的点数组中。以后,当鼠标移动时,OnMouseMove就不断修改该笔划对象的内部数据成员(加入新的点到笔划对象的数组中)。另外,为了用LineTo画出线条,需要将当前鼠标位置保存到m_ptPrev中,以便出现一个新的点时,画一条从m_ptPrev到新的点的直线。</p>
          <p> 但是,由于用户的鼠标可以在屏幕上任意移动。当鼠标移出窗口外时,窗口无法收到鼠标消息。此时,如果松开了鼠标左键,应用程序由于无法接受到该条消息而不会终止当前笔划,这样就造成了错误。如何避免这种情况发生呢?解决的办法是要让窗口在鼠标移出窗口外时仍然能接受到鼠标消息。幸好,Windows提供了一个API函数SetCapture()解决了这一问题。</p>
          <p> CWnd::SetCapture()用于捕获鼠标:无论鼠标光标位置在何处,都会将鼠标消息送给调用它的那一个窗口。在用完后,需要用ReleaseCapture()释放窗口对鼠标的控制,否则其他窗口将无法接收到鼠标消息。这一工作当然最好在鼠标左键松开OnLButtonUp()时来做。</p>
          
          <p> <b>清单</b><b>8.9 
            OnLButtonUp消息处理函数</b></p>
           
          <p>void CDrawView::OnLButtonUp(UINT nFlags, CPoint point) </p>
          <p>{</p>
          <p>// TODO: Add your message handler code here and/or call default</p>
          
          <p> </p>
           
          <p>// Mouse button up is interesting in the draw application</p>
          <p>// only if the user is currently drawing a new stroke by dragging</p>
          <p>// the captured mouse.</p>
          <p>if (GetCapture() != this)</p>
          <p>return; // If this window (view) didn't capture the mouse,</p>
          <p>// then the user isn't drawing in this window.</p>
          <p>CDrawDoc* pDoc = GetDocument();</p>
          <p>CClientDC dc(this);</p>
          <p>// CScrollView changes the viewport origin and mapping mode.</p>
          <p>// It's necessary to convert the point from device coordinates</p>
          <p>// to logical coordinates, such as are stored in the document.</p>
          <p>OnPrepareDC(&amp;dc); // set up mapping mode and viewport origin</p>
          <p>dc.DPtoLP(&amp;point);</p>
          <p>CPen* pOldPen = dc.SelectObject(pDoc-&gt;GetCurrentPen());</p>
          <p>dc.MoveTo(m_ptPrev);</p>
          <p>dc.LineTo(point);</p>
          <p>dc.SelectObject(pOldPen);</p>
          <p>m_pStrokeCur-&gt;m_pointArray.Add(point);</p>
          <p>// Tell the stroke item that we're done adding points to it.</p>
          <p>// This is so it can finish computing its bounding rectangle.</p>
          <p>m_pStrokeCur-&gt;FinishStroke();</p>
          <p>// Tell the other views that this stroke has been added</p>
          <p>// so that they can invalidate this stroke's area in their</p>
          <p>// client area.</p>
          <p>pDoc-&gt;UpdateAllViews(this, 0L, m_pStrokeCur);</p>
          <p>ReleaseCapture(); // Release the mouse capture established at</p>
          <p>// the beginning of the mouse drag.</p>
          <p>return;</p>
          <p>}</p>
          
          <p> </p>
          <p> OnLButtonUp首先检查鼠标是否被当前窗口所捕获,如果不是则返回。然后画出笔划最后两点之间的极短的直线段。接着,调用CStroke::FinishStroke(),请求CStroke对象计算它的最小矩形。然后调用pDoc-&gt;UpdateAllViews(this, 
            0L, m_pStrokeCur)通知其他视图更新显示。</p>
          <p> 当一个视图修改了文档内容并更新显示时,一般的其它的对应于同一文档的视图也需要相应更新,这通过调用文档的成员函数UpdateAllViews完成。</p>
           
          <blockquote> 
            <blockquote> 
              <p>void UpdateAllViews( CView* pSender, LPARAM lHint = 0L, CObject* 
                pHint = </p>
              <p>NULL );</p>
            </blockquote>
          </blockquote>
          
          <p> UpdateAllViews带三个参数:pSender指向修改文档的视图。由于该视图已经作了更新,所以不再需要更新。比如,在上面的例子中,OnLButtonUp已经绘制了视图,因此不需要再次更新。如果为NULL,则文档对应的所有视图都被更新。</p>
          <p> lHint和pHint包含了更新视图时所需的附加信息。在本例中,其他视图只需要重画当前绘制中的笔划,因此OnLButtonUp把当前笔划指针传给UpdateAllViews函数。该函数调用文档所对应的除pSender外的所有视图的OnUpdate函数,并将lHint和pHint传给OnUpdate函数通知更新附加信息。</p>
          <p> OnLButtonUp最后释放对鼠标的控制,这样别的应用程序窗口就可以获得鼠标消息了。</p>
          <p> 结合上面讲到的知识,读者不难自行理解下面的OnMouseMove函数。</p>
           
          <p>void CDrawView::OnMouseMove(UINT nFlags, CPoint point) </p>
          <p>{</p>
          <p>// TODO: Add your message handler code here and/or call default</p>
          <p>// Mouse movement is interesting in the Scribble application</p>
          <p>// only if the user is currently drawing a new stroke by dragging</p>
          <p>// the captured mouse.</p>
          <p>if (GetCapture() != this)</p>
          <p>return; // If this window (view) didn't capture the mouse,</p>
          <p>// then the user isn't drawing in this window.</p>
          <p>CClientDC dc(this);</p>
          <p>// CScrollView changes the viewport origin and mapping mode.</p>
          <p>// It's necessary to convert the point from device coordinates</p>
          <p>// to logical coordinates, such as are stored in the document.</p>
          <p>OnPrepareDC(&amp;dc);</p>
          <p>dc.DPtoLP(&amp;point);</p>
          <p>m_pStrokeCur-&gt;m_pointArray.Add(point);</p>
          <p>// Draw a line from the previous detected point in the mouse</p>
          <p>// drag to the current point.</p>
          <p>CPen* pOldPen = dc.SelectObject(GetDocument()-&gt;GetCurrentPen());</p>
          <p>dc.MoveTo(m_ptPrev);</p>
          <p>dc.LineTo(point);</p>
          <p>dc.SelectObject(pOldPen);</p>
          <p>m_ptPrev = point;</p>
          <p>return;</p>
          <p>}</p>
          
          <p>至此,绘图程序的文档、视图全部设计完了,现在编译运行程序。程序启动后,在空白窗口中徒手绘图,如图8-7所示。</p>
          <p align="center"> <img src="T8_7.gif" alt="T8_7.tif (267124 bytes)" width="470" height="335"></p>
          <p align="center">图8-7 
            多文档绘图程序窗口</p>
          <div align="center"> 
            <table border="0" cellpadding="0" cellspacing="0" width="615">
              <tr> 
                <td><a href="chap8_2.htm">上一页</a></td>
                <td> 
                  <p align="right"><a href="chap8_4.htm">下一页</a> 
                </td>
              </tr>
            </table>
            <p><a href="http://www.cpcw.com">电脑报首页</a> <a href="../../index.htm">网络学院首页</a></p>
          </div>
          <hr noshade color="#3973DE" size="1">
          
          <p align="center">本教程由<a href="http://vcdynasty.yeah.net">Visual C++王朝(Where 
            programmers come together)</a>协助制作<br>
            未经许可,请勿以任何形式复制
        </td>
      </tr>
    </table>
    <p>&nbsp;</p>
  </center>
</div>

</body>
</html>

⌨️ 快捷键说明

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