下载本文示例代码
推荐:Visual C 图像处理编程讲座 首先我们说下通过鼠标的移动来绘制直线。 这里要捕获到鼠标的两个消息: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);} 连续线绘图: 思路:鼠标移动的信号被捕捉到,然后随时响应该信号,绘制图线。 这里要设置一个BOOL变量 m_bDraw 来判断是否鼠标左键按下了。
//画连续的线条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 ) { CClientDC 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);} 到此,我们知道了一般地绘图方法了。
推荐:Visual C 图像处理编程讲座 首先我们说下通过鼠标的移动来绘制直线。 这里要捕获到鼠标的两个消息: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);} 连续线绘图: 思路:鼠标移动的信号被捕捉到,然后随时响应该信号,绘制图线。 这里要设置一个BOOL变量 m_bDraw 来判断是否鼠标左键按下了。
//画连续的线条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 ) { CClientDC 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);} 到此,我们知道了一般地绘图方法了。
下载本文示例代码
VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步VC中利用MFC设计绘图程序初步