几种画直线的方法-孙鑫C++笔记

// HDC画直线
CPoint m_ptOrigin ;
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = point;
  CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
  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);

  CView::OnLButtonUp(nFlags, point);
}

// CDC画直线
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = point;
  CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
  CDC *pDC = GetDC();
  pDC->MoveTo(m_ptOrigin);
  pDC->LineTo(point);
  ReleaseDC(pDC);

  CView::OnLButtonUp(nFlags, point);
}

// CClientDC、CWindowDC画直线
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = point;
  CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
  CClientDC dc(this);          //画到客户区
  CClientDC dc(GetParent());      //画到客户区+工具栏
  CWindowDC dc(this);          //画到客户区
  CWindowDC dc(GetParent());  //画到客户区+工具栏+菜单栏+标题栏
  CWindowDC dc(GetDesktopWindow());  //画到整个屏幕窗口
  dc.MoveTo(m_ptOrigin);
  dc.LineTo(point);

  CView::OnLButtonUp(nFlags, point);
}

//带颜色的直线
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = point;
  CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
  CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
  CClientDC dc(this);
  CPen *pOldPen = dc.SelectObject(&pen);
  dc.MoveTo(m_ptOrigin);
  dc.LineTo(point);
  dc.SelectObject(pOldPen);

  CView::OnLButtonUp(nFlags, point);
}

//画刷
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = point;
  CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
  CBrush brush(RGB(255, 0, 0));
  CClientDC dc(this);
  dc.FillRect(CRect(m_ptOrigin, point), &brush);

  CView::OnLButtonUp(nFlags, point);
}
//位图画刷
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = point;
  CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
  CBitmap bitmap;
  bitmap.LoadBitmap(IDB_BITMAP1);
  CBrush brush(&bitmap);
  CClientDC dc(this);
  dc.FillRect(CRect(m_ptOrigin, point), &brush);

  CView::OnLButtonUp(nFlags, point);
}
//透明画刷
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = point;
  CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
  CClientDC dc(this);
  // dc.Rectangle(CRect(m_ptOrigin, point)); //不透明
  CBrush *pBrush = CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
  CBrush *pOldBrush = dc.SelectObject(pBrush);
  dc.Rectangle(CRect(m_ptOrigin, point));
  dc.SelectObject(pOldBrush);    //透明

  CView::OnLButtonUp(nFlags, point);
}
//用画笔写字
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = point;
  m_bDraw = TRUE;
  CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
  m_bDraw = FALSE;
  CView::OnLButtonUp(nFlags, point);
}
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
  CClientDC dc(this);
  CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
  CPen *pOldPen = dc.SelectObject(&pen);
  if(m_bDraw == TRUE)  {
    dc.MoveTo(m_ptOrigin);
    dc.LineTo(point);
    m_ptOrigin = point;
  }
  dc.SelectObject(pOldPen);
  
  CView::OnMouseMove(nFlags, point);
}

//画扇形
CPoint m_ptOld;
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
  m_ptOrigin = m_ptOld = point;
  m_bDraw = TRUE;
  CView::OnLButtonDown(nFlags, point);
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
  m_bDraw = FALSE;
  CView::OnLButtonUp(nFlags, point);
}
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
  CClientDC dc(this);
  CPen pen(PS_SOLID, 1, RGB(255, 0, 0));
  CPen *pOldPen = dc.SelectObject(&pen);
  if(m_bDraw == TRUE)  {
    dc.MoveTo(m_ptOrigin);
    dc.LineTo(m_ptOld);
    // dc.MoveTo(m_ptOrigin); // 不带边线
    dc.MoveTo(m_ptOld);    // 带边线
    dc.LineTo(point);
    m_ptOld = point;
  }
  dc.SelectObject(pOldPen);

  CView::OnMouseMove(nFlags, point);
}

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。