Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4470905
  • 博文数量: 1148
  • 博客积分: 25453
  • 博客等级: 上将
  • 技术积分: 11949
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-06 21:14
文章分类

全部博文(1148)

文章存档

2012年(15)

2011年(1078)

2010年(58)

分类: C/C++

2011-07-07 21:13:58


1.绘制线条的起点: 鼠标左键按下
  1. void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default
  4.     
  5.     m_ptOrigin = point;//线条的起点
  6.     CView::OnLButtonDown(nFlags, point);
  7. }

2.利用SDK全局函数实现画线功能
  1. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default

  4.     //首先获得窗口的设备描述表
  5.     HDC hdc;
  6.     hdc = ::GetDC(m_hWnd);
  7.     //移动到线条的起点
  8.     MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,NULL);
  9.     //画线
  10.     LineTo(hdc,point.x,point.y);
  11.     //释放设备描述符
  12.     ::ReleaseDC(m_hWnd,hdc);
  13.     CView::OnLButtonUp(nFlags, point);
  14. }
3. 利用MFCD的CDC类实现画线功能
  1. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default

  4.     CDC * pDC = GetDC();
  5.     pDC->MoveTo(m_ptOrigin);
  6.     pDC->LineTo(point);
  7.     ReleaseDC(pDC);

  8.     CView::OnLButtonUp(nFlags, point);
  9. }
4.利用MFC的CClientDC类实现画线功能
  1. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default
  4.   
  5.     CClientDC dc(this);
  6.     dc.MoveTo(m_ptOrigin);
  7.     dc.LineTo(point);    

  8.     CView::OnLButtonUp(nFlags, point);
  9. }
5.画彩色线条
  1. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default

  4.     CPen pen(PS_SOLID,10,RGB(255,0,0));
  5.     CClientDC dc(this);
  6.     CPen *pOldPen = dc.SelectObject(&pen);
  7.     
  8.     dc.MoveTo(m_ptOrigin);
  9.     dc.LineTo(point);    
  10.     dc.SelectObject(pOldPen);

  11.     CView::OnLButtonUp(nFlags, point);
  12. }

6.使用画刷绘图
  1. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default
  4.     //创建一个红色画刷
  5.     CBrush brush(RGB(255,0,0));
  6.     //创建并获得设备描述表
  7.     CClientDC dc(this);
  8.     //利用红色画刷填充鼠标拖拽过程中形成的矩形区域
  9.     dc.FillRect(CRect(m_ptOrigin,point),&brush);
  10.     CView::OnLButtonUp(nFlags, point);
  11. }
7.绘制连续线条
  1. void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default
  4.     m_bDraw = FALSE;
  5.     CView::OnLButtonUp(nFlags, point);
  6. }

  1. void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default
  4.     
  5.     m_ptOrigin = point;//线条的起点
  6.     m_bDraw = TRUE; //判断 左键按下标志,在mouse move 消息函数中使用 标志
  7.     CView::OnLButtonDown(nFlags, point);
  8. }

  1. void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
  2. {
  3.     // TODO: Add your message handler code here and/or call default
  4. /*    
  5.     CClientDC dc(this);
  6.     if(m_bDraw == TRUE)
  7.     {
  8.         dc.MoveTo(m_ptOrigin);
  9.         dc.LineTo(point);
  10.         //修改线段的起点
  11.         m_ptOrigin = point;
  12.     }
  13.     CView::OnMouseMove(nFlags, point);

  14. */
  15.     CClientDC dc(this);
  16.     //创建一个红色的、宽度为1的实现画笔
  17.     CPen pen(PS_SOLID,1,RGB(255,0,0));
  18.     //把创建的画笔选入设备描述表
  19.     CPen *pOldPen = dc.SelectObject(&pen);
  20.     if(m_bDraw == TRUE)
  21.     {
  22.         dc.MoveTo(m_ptOrigin);
  23.         dc.LineTo(point);
  24.         //修改线段的起点
  25.         m_ptOrigin = point;
  26.     }
  27.     //恢复设备描述表
  28.     dc.SelectObject(pOldPen);

  29.     CView::OnMouseMove(nFlags, point);
  30. }


阅读(811) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~