MFC學習筆記之繪圖控制

MFC中進行與繪圖相關的控制,首先先爲VIEW類添加一個成員變量 m_nDrawType,用來指定要畫的圖形 ,增長相應的菜單項用來繪製 點 直線 矩形 和 橢圓 在菜單項的響應函數中寫下面的代碼:函數

view plainthis

  1. void CGraphicView::OnDot()   url

  2. {  spa

  3.     // TODO: Add your command handler code here  .net

  4.     m_nDrawType = 1;  code

  5. }  blog

  6.   

  7. void CGraphicView::OnLine()   ip

  8. {  get

  9.     // TODO: Add your command handler code here  it

  10.     m_nDrawType = 2;  

  11. }  

  12.   

  13. void CGraphicView::OnRectangle()   

  14. {  

  15.     // TODO: Add your command handler code here  

  16.     m_nDrawType = 3;  

  17. }  

  18.   

  19. void CGraphicView::OnEllipse()   

  20. {  

  21.     // TODO: Add your command handler code here  

  22.     m_nDrawType = 4;  

  23. }  

知道每種圖形相應的序號,而後就要繪圖,先是相應LBUTTONDOWM消息,記錄下當前點,而後再響應 LBUTTONUP消息 進行繪圖操做,代碼以下:

view plain

  1. void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)   

  2. {  

  3.     // TODO: Add your message handler code here and/or call default  

  4.     m_ptOrigin = point;  

  5.     CView::OnLButtonDown(nFlags, point);  

  6. }  

  7.   

  8. void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)   

  9. {  

  10.     // TODO: Add your message handler code here and/or call default  

  11.     CClientDC dc(this);  

  12.       

  13.     CPen pen(m_nLineStyle,m_nLineWidth,m_clr);  

  14.     dc.SelectObject(&pen);  

  15.       

  16.     CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));  

  17.     dc.SelectObject(pBrush);  

  18.       

  19.     switch(m_nDrawType)  

  20.     {  

  21.     case

更多詳細介紹:http://url7.me/lvwE1

相關文章
相關標籤/搜索