CDrawView::CDrawView()
{
// TODO: add construction code here
m_ptOrigin=0;
m_ptOld=0;
m_bDraw=FALSE;
}
void CDrawView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_ptOrigin=point;
m_ptOld=point;
m_bDraw=TRUE;
}
void CDrawView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
/*
//用SDK平台的函数写的
//首先获取窗口的设备描述表
HDC hdc;
hdc=::GetDC(m_hWnd);
//移动到线条的起点
MoveToEx(hdc,point.x,point.y,NULL);
//画线
LineTo(hdc,point.x,point.y);
//释放DC
::ReleaseDC(m_hWnd,hdc);
*/
//用CDC类写的
/*
CDC *pDC=GetDC();
pDC->MoveTo(m_ptOrigin);
pDC->LineTo(point);
ReleaseDC(pDC);
*/
//用CClientDC类写的
/* CClientDC dc(GetParent());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
*/
//用CWindowDC类写
/* CWindowDC dc(GetDesktopWindow());
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
*/
/* CPen pen(PS_COSMETIC,1,RGB(255,56,0));
CClientDC dc(this);
CPen *pOldPen=dc.SelectObject(&pen);
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
dc.SelectObject(pOldPen);
*/
/*
//创建红色画刷
CBrush brush(RGB(255,0,0));
//创建并获得DC
CClientDC dc(this);
//利用红色画刷填满一块矩形区域
dc.FillRect(CRect(m_ptOrigin,point),&brush);
*/
/*
//创建位图对象
CBitmap bitmap;
//加载位图资源
bitmap.LoadBitmap(IDB_BITMAP1);
//创建位图画刷
CBrush brush(&bitmap);
CClientDC dc(this);
dc.FillRect(CRect(m_ptOrigin,point),&brush);
*/
/* CClientDC dc(this);
dc.Rectangle(CRect(m_ptOrigin,point));
*/
/*
CClientDC dc(this);
//获取透明画刷
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
CBrush *pOldBrush=dc.SelectObject(pBrush);
dc.Rectangle(CRect(m_ptOrigin,point));
dc.SelectObject(pOldBrush);
*/
//画连续线条
m_bDraw=FALSE;
CView::OnLButtonUp(nFlags, point);
}
void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
dc.SetROP2(R2_NOTCOPYPEN);
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);
//每次从鼠标当前位置向上一个位置连线
dc.LineTo(m_ptOld);
//修改线段的起点
//注释掉修改起点的这句,就可以画出类似扇形的样子
//m_ptOrigin=point;
m_ptOld=point;
}
dc.SelectObject(pOldPen);
CView::OnMouseMove(nFlags, point);
}
上面是部分代码
上面定义了三个Ciew类的对象,其中两个CPoint对象 m_ptOrign保存鼠标左键按下时的坐标,m_ptOld保存鼠标左键起来时的坐标。另外一个m_bDraw是BOOL变量,用来作为左键是否按下