MFC中实现用DC画线

void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default
 //MessageBox("view clicked");
 m_ptOrigin = point;
 m_bDraw = TRUE;
 CView::OnLButtonDown(nFlags, point);
}

void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
 // TODO: Add your message handler code here and/or call default

第一种:使用API函数
/* 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类实现

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

第三种:使用CClientDC

 /*CClientDC dc(this);
 //CClientDC dc(GetParent());
 dc.MoveTo(m_ptOrigin);
 dc.LineTo(point);*/

第四种:使用CWindowDC

 /*CWindowDC dc(this);
 dc.MoveTo(m_ptOrigin);
 dc.LineTo(point);*/

 /*CPen pen(PS_SOLID,2,RGB(255,0,0));//设置画笔的样式颜色
 CClientDC dc(this);
 CPen *pOldPen = dc.SelectObject(&pen);
 dc.MoveTo(m_ptOrigin);
 dc.LineTo(point);
 dc.SelectObject(*pOldPen);*/

 //CBrush brush(RGB(255,2,2));//使用画刷,设置画刷的样式颜色
 /*CBitmap bitmap;
 bitmap.LoadBitmap(IDB_BITMAP1);
 CBrush brush(&bitmap);
 CClientDC dc(this);
 dc.FillRect(CRect(m_ptOrigin,point),&brush);*/

 /*CClientDC dc(this);
 CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH))//设置透明画刷
 CBrush *pOldBrush = dc.SelectObject(pBrush);
 dc.Rectangle(CRect(m_ptOrigin,point));
 dc.SelectObject(pOldBrush);*/

 m_bDraw = 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,1,RGB(255,6,66));
 CPen *pOlePen = dc.SelectObject(&pen);
 
 if(m_bDraw == TRUE)
 {
  dc.MoveTo(m_ptOrigin);
  dc.LineTo(point);
  m_ptOrigin = point;
 }

 dc.SelectObject(pOlePen);
 CView::OnMouseMove(nFlags, point);
}
 

你可能感兴趣的:(MFC中实现用DC画线)