VC中MFC下的绘图初步解析
Posted zieckey
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了VC中MFC下的绘图初步解析相关的知识,希望对你有一定的参考价值。
作者:zieckey(zieckey@yahoo.com.cn)
All Rights Reserved!
首先我们说下通过鼠标的移动来绘制直线。
这里要捕获到鼠标的两个消息: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下的绘图初步解析的主要内容,如果未能解决你的问题,请参考以下文章