这里要捕获到鼠标的两个消息:WM_LBUTTONDOWN, WM_LBUTTONUP。响应WM_LBUTTONDOWN消息记录直线的起始点,响应WM_LBUTTONUP消息记录直线的终点并绘制直线。
- void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- //MessageBox("Left Button Clicks DrawView");
- m_ptOrigin = point ; //这里先定义了一个内部变量保存直线的起始点
- CView::OnLButtonDown(nFlags, point);
- }
下面看看响应WM_LBUTTONUP消息记录直线的终点并绘制直线。
- //方法一
- void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- HDC hdc;
- hdc = ::GetDC(m_hWnd); //调用全局函数
- MoveToEx(hdc,m_ptOrigin.x,m_ptOrigin.y,0);
- LineTo(hdc,point.x,point.y);
- ::ReleaseDC(m_hWnd,hdc);
- CView::OnLButtonUp(nFlags, point);
- }
- //方法二
- void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CDC *pDC = GetDC();
- pDC->MoveTo(m_ptOrigin);
- pDC->LineTo(point);
- ReleaseDC(pDC);
- }
- //方法三
- void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CClientDC dc(this) ; //CClientDC对象构造的时候就调用了GetDC,析构的时候调用ReleaseDC ,只能访问客户区CClientDC dc(GetParent());
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(point);
- }
- //方法四
- void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CWindowDC dc(this) ; //CWindowDC可以访问客户区和非客户区
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(point);
- }
- void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- CWindowDC dc(GetDesktopWindow()) ; //现在可以访问桌面
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(point);
- }
连续的直线
- //画连续的线条
- void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
- {
- if ( m_bDraw )
- {
- CClientDC dc(this) ;
- dc.MoveTo(m_ptOrigin); //移动到原来的点
- dc.LineTo(point); //绘制直线
- m_ptOrigin = point ; //将现在的点赋值给原来的坐标,以便下次调用
- }
- CView::OnMouseMove(nFlags, point);
- }
- //改变画笔的颜色
- void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
- {
- if ( m_bDraw )
- {
- CC
- lientDC dc(this) ;
- CPen pen( PS_SOLID , 1 , RGB(255,128,128) );
- CPen *pOldPen = dc.SelectObject(&pen);
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(point);
- m_ptOrigin = point ;
- dc.SelectObject(pOldPen);
- }
- CView::OnMouseMove(nFlags, point);
- }
- //画扇型
- void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
- {
- if ( m_bDraw )
- {
- CClientDC dc(this) ;
- CPen pen( PS_SOLID , 1 , RGB(255,128,128) );
- CPen *pOldPen = dc.SelectObject(&pen);
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(m_ptOld);
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(point);
- m_ptOld = point ;
- dc.SelectObject(pOldPen);
- }
- CView::OnMouseMove(nFlags, point);
- }
- //画带边线的扇型
- void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
- {
- if ( m_bDraw )
- {
- CClientDC dc(this) ;
- CPen pen( PS_SOLID , 1 , RGB(255,128,128) );
- CPen *pOldPen = dc.SelectObject(&pen);
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(m_ptOld);
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(point);
- dc.MoveTo(m_ptOld);
- dc.LineTo(point);
- m_ptOld = point ;
- dc.SelectObject(pOldPen);
- }
- CView::OnMouseMove(nFlags, point);
- }
- //看看绘图的模式设置方法
- void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
- {
- if ( m_bDraw )
- {
- CClientDC dc(this) ;
- dc.SetROP2(R2_BLACK); //绘图的模式设置,始终绘制黑色图形
- CPen pen( PS_SOLID , 1 , RGB(255,128,128) );
- CPen *pOldPen = dc.SelectObject(&pen);
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(m_ptOld);
- dc.MoveTo(m_ptOrigin);
- dc.LineTo(point);
- dc.MoveTo(m_ptOld);
- dc.LineTo(point);
- m_ptOld = point ;
- dc.SelectObject(pOldPen);
- }
- CView::OnMouseMove(nFlags, point);
- }
画移动的直线
- void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- end = point;
- pre = point;
- start = point;
- CView::OnLButtonDown(nFlags, point);
- }
- void CTestView::OnMouseMove(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CTestDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- CDC *pDC =GetDC();
- int OldROP = SetROP2(*pDC, R2_NOTXORPEN);
- CBrush brushWHITE(RGB(0, 0, 255));
- CBrush* pOldBrush = pDC->SelectObject(&brushWHITE);
- CPen penBlack;
- penBlack.CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
- CPen* pOldPen = pDC->SelectObject(&penBlack);
- if(nFlags)
- {
- pre =end;
- pDC->MoveTo(start.x,start.y);
- pDC->LineTo(pre.x,pre.y);
- end = point;
- pDC->MoveTo(start.x,start.y);
- pDC->LineTo(end.x,end.y);
- }
- pDC->SelectObject(pOldBrush);
- pDC->SelectObject(pOldPen);
- SetROP2(*pDC,OldROP);
- CView::OnMouseMove(nFlags, point);
- }
- void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
- {
- // TODO: Add your message handler code here and/or call default
- CTestDoc* pDoc = GetDocument();
- ASSERT_VALID(pDoc);
- CDC *pDC =GetDC();
- end = point;
- pDC->MoveTo(start.x,start.y);
- CBrush brushWHITE(RGB(0, 0, 255));
- CBrush* pOldBrush = pDC->SelectObject(&brushWHITE);
- CPen penBlack;
- penBlack.CreatePen(PS_SOLID, 1, RGB(0, 0, 255));
- CPen* pOldPen = pDC->SelectObject(&penBlack);
- pDC->LineTo(end.x,end.y);
- pDC->SelectObject(pOldBrush);
- pDC->SelectObject(pOldPen);
- CView::OnLButtonUp(nFlags, point);
- }
PS:有些代码需要根据实际情况,修改。不能直接应用。