如何在MFC中用鼠标画直线

而且能不能用鼠标控制它移动
2024-12-28 05:39:33
推荐回答(4个)
回答1:

类向导添加响应函数(三个)

编辑代码

void CView::OnLButtonDown(UINT nFlags, CPoint point)

{

// TODO: 在此添加消息处理程序代码和/或调用默认值

::SetCursor(LoadCursor(NULL, IDC_ARROW));

//注意以下几个变量要在头函数中定义

//bool m_Dragging; CPoint m_PointOrigin;CPoint m_PointOld;

m_Dragging = 1;

m_PointOrigin = point;

m_PointOld = point;

SetCapture();

RECT Rect;

GetClientRect(&Rect);

ClientToScreen(&Rect);

::ClipCursor(&Rect);

CScrollView::OnLButtonDown(nFlags, point);

}

void CView::OnMouseMove(UINT nFlags, CPoint point)

{

// TODO: 在此添加消息处理程序代码和/或调用默认值

if (m_Dragging)

{

CClientDC dc(this);

dc.SetROP2(R2_NOT);

dc.MoveTo(m_PointOrigin);

dc.LineTo(m_PointOld);

dc.MoveTo(m_PointOrigin);

dc.LineTo(point);

m_PointOld = point;

}

CScrollView::OnMouseMove(nFlags, point);

}

void CView::OnLButtonUp(UINT nFlags, CPoint point)

{

// TODO: 在此添加消息处理程序代码和/或调用默认值

CClientDC dc(this);

if (m_Dragging)

{

m_Dragging = 0;

::ReleaseCapture();

::ClipCursor(NULL);

dc.SetROP2(R2_NOT);

dc.MoveTo(m_PointOrigin);

dc.LineTo(m_PointOld);

dc.MoveTo(m_PointOrigin);

dc.LineTo(point);

m_PointOld = point;

}

CScrollView::OnLButtonUp(nFlags, point);

}

结果截图

回答2:

OnLButtonDown(UINT nFlags, CPoint point)
{
::SetCursor(m_HCross);
m_Dragging=1;
m_PointOrigin=point;
m_PointOld=point;
SetCapture();
RECT Rect;
GetClientRect(&Rect);
ClientToScreen(&Rect);
::ClipCursor (&Rect);
CView::OnLButtonDown(nFlags, point);
}

OnMouseMove(UINT nFlags, CPoint point)
{
if(m_Dragging)
{
CClientDC dc(this);
dc.SetROP2 (R2_NOT);

dc.MoveTo (m_PointOrigin);
dc.LineTo (m_PointOld);
dc.MoveTo (m_PointOrigin);
dc.LineTo (point);
m_PointOld=point;
}

CView::OnMouseMove(nFlags, point);
}

OnLButtonUp(UINT nFlags, CPoint point)
{ CClientDC dc(this);

if(m_Dragging)
{
m_Dragging=0;
::ReleaseCapture ();
::ClipCursor (NULL);

dc.SetROP2 (R2_NOT);
dc.MoveTo (m_PointOrigin);
dc.LineTo (m_PointOld);
dc.MoveTo (m_PointOrigin);
dc.LineTo (point);
m_PointOld=point;

}

CView::OnLButtonUp(nFlags, point);
}

回答3:

首先通过鼠标左键按下和鼠标左键弹起消息捕捉要画线的两个点,并存在CPoint类的对象中;然后在OnPaint()函数中用MoveTo(左键按下的点)和LineTo(左键弹起的点),这样就可以画线了,当然你可以用CPen类对象修改线条的颜色、样式和粗细

回答4:

用鼠标划线,用Device context ,即设备上下文,用来在画布上划线和作图的。里面有很多工具,自己去查资料吧。用moveto(x,y)和Lineto(x,y);函数就可以了。

用鼠标控制也可以实现,也就是在mousemove中加点代码,让线随着鼠标做运动。动脑子好好想想吧,代码就不贴了。好久没用过VC了,呵呵