在CView中画线

1. 建立一个单文档

2.在CXxView上右键选择Add Member Variable 添加CPoint  m_ptOrigin,私有;添加BOOL m_hDraw ;

3.在构造函数中添加m_ptOrigin = 0,m_hDraw = FALSE;

4.同2选择Add Windows Message Handler 添加LBOTTONDOWN,LBOTTONUP,MOUSEMOVE并编辑

5.在OnLButtonDown函数中添加m_ptOrigin = point;

6.在 OnLButtonUp函数中

  A。 在窗口画直线

   HDC hdc;
   hdc = ::GetDC(m_hWnd);
   MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL);
   LineTo(hdc,point.x,point.y);
   ::ReleaseDC(m_hWnd,hdc);

或者:

   CDC *pDC = GetDC( );
   pDC->MoveTo(m_ptOrigin );
   pDC->LineTo(point );
   ReleaseDC(pDC);

B。把工具栏包含进

   CClientDC dc(GetParent() );
   dc.MoveTo(m_ptOrigin);
   dc.LineTo(point);
  ::ReleaseDC(dc);

桌面窗口:

   CWindowDC dc(GetDesktopWindow() );
   dc.MoveTo(m_ptOrigin );
   dc.LineTo(point );

 

C.使用画笔、刷

   CPen pen(PS_SOLID,1,RGB(255,1,1) );
   CClientDC dc(this);

//CClientDC dc(GetParent() );
   CPen * pOldPen=dc.SelectObject(&pen);
   dc.MoveTo(m_ptOrigin);
   dc.LineTo(point );
   dc.SelectObject(pOldPen);

/

  CBrush brush(RGB(0,250,0) );
 CClientDC dc(this);
 dc.FillRect(CRect(m_ptOrigin,point ),&brush);

 

D.使用位图

 CBitmap bitMap;
 bitMap.LoadBitmap(IDB_BITMAP1);
 CBrush brush(&bitMap);
 CClientDC dc(this);
 dc.FillRect(CRect(m_ptOrigin,point ),&brush);

 

E.画曲线

void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = point;
 m_hDraw = TRUE;
 CView::OnLButtonDown(nFlags, point);
}

 

void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{

 m_hDraw = FALSE;
 CView::OnLButtonUp(nFlags, point);
}

 

void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 CClientDC dc(this);
 CPen pen( PS_SOLID,2,RGB(255,0,0) );
 CPen * pOldPen = dc.SelectObject(&pen );

 if (m_hDraw )
 {
  dc.MoveTo(m_ptOrigin);
  dc.LineTo(point );
  m_ptOrigin = point;
 }
 dc.SelectObject(pOldPen);
 CView::OnMouseMove(nFlags, point);
}

 

你可能感兴趣的:(在CView中画线)