?? chap8_3.htm
字號:
<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->GetClipBox(&rectClip);</p>
<p>pDC->LPtoDP(&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<CObList,CStroke*>& strokeList = pDoc->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->GetBoundingRect();</p>
<p>pDC->LPtoDP(&rectStroke);</p>
<p>rectStroke.InflateRect(1, 1); // avoid rounding to nothing</p>
<p>if (!rectStroke.IntersectRect(&rectStroke, &rectClip))</p>
<p>continue;</p>
<p>pStroke->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 < 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->SelectObject(&penStroke);</p>
<p>pDC->MoveTo(m_pointArray[0]);</p>
<p>for (int i=1; i < m_pointArray.GetSize(); i++)</p>
<p>{</p>
<p>pDC->LineTo(m_pointArray[i]);</p>
<p>}</p>
<p>pDC->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(&dc);</p>
<p>dc.DPtoLP(&point);</p>
<p>m_pStrokeCur = GetDocument()->NewStroke();</p>
<p>// Add first point to the new stroke</p>
<p>m_pStrokeCur->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(&dc); // set up mapping mode and viewport origin</p>
<p>dc.DPtoLP(&point);</p>
<p>CPen* pOldPen = dc.SelectObject(pDoc->GetCurrentPen());</p>
<p>dc.MoveTo(m_ptPrev);</p>
<p>dc.LineTo(point);</p>
<p>dc.SelectObject(pOldPen);</p>
<p>m_pStrokeCur->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->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->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->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(&dc);</p>
<p>dc.DPtoLP(&point);</p>
<p>m_pStrokeCur->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()->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> </p>
</center>
</div>
</body>
</html>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -