MFC绘制连续线条

MFC绘制连续线条

思路:首先要得到线条起点,然后需要捕获鼠标移动过程中的的每一个点(WM_MOUSEMOVE),各点之间绘制非常短的线段,从而绘制一条连续的线条
步骤1.添加成员变量

private:	CPoint m_ptOrigin; //线条起点
            BOOL m_bDraw;//初始化FALSE.判断鼠标左键的状态,左键按下,真,起点,左键弹起,假,终点

步骤2. 添加消息响应函数

void CDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	m_ptOrigin = point;//鼠标按下为线条起点
	m_bDraw = TRUE;
	
	CView::OnLButtonDown(nFlags, point);
}

void CDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	m_bDraw = FALSE;
	
	CView::OnLButtonUp(nFlags, point);
}

void CDrawView::OnMouseMove(UINT nFlags, CPoint point)
{
    CClientDC dc(this);
    CPen pen(PS_SOLID, 1, RGB(255,0,0));//创建一个画笔,宽度1,颜色红色
    CPen* pOldPen = dc.SelectObject(&pen);//将新画笔选入设备描述表  
    if(m_bDraw == TRUE)
    {
       dc.MoveTo(m_ptOrigin);
       dc.LineTo(point);
       m_ptOrigin = point;//修改线段的起点
    }
    dc.SelectObject(pOldPen);//恢复设备描述表中的画笔对象

   CView::OnMouseMove(nFlags, point);
}

你可能感兴趣的:(C++)