MFC中鼠标拖动矩形和改变矩形大小问题

easybf easybf 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我想实现拖动已画的矩形,下面是自己写的代码
void CRectTView::OnRect() //菜单事件,创建一个矩形
{
// TODO: 在此添加命令处理程序代码
CClientDC dc(this);
rect = new CRect(oldpoint,botpoint);
dc.Rectangle(&rect);
m_ptrArray.Add(&rect);
}
void CRectTView::OnDraw(CDC* pDC)
{
CRectTDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;

// TODO: 在此处为本机数据添加绘制代码
for(int i=0;i<m_ptrArray.GetSize();i++)
{
pDC->Rectangle((CRect *)m_ptrArray.GetAt(i));
}
}

void CRectTView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
if( (point.x<rect.left && point.x>rect.right) &&(point.y<rect.top && point.y>rect.bottom))
{
bMove = TRUE;
pointMove = point; //pointMove 为了计算偏移量声明的一个点,在这里保存鼠标按下去时的坐标
}

CView::OnLButtonDown(nFlags, point);
}

void CRectTView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CClientDC dc(this);
dc.SetROP2(R2_NOTCOPYPEN);
if(bMove)
{

moveX = point.x - pointMove.x;
moveY = point.y - pointMove.y;
oldpoint.x +=moveX; //oldpoint 是成员变量 rect的左顶点。
oldpoint.y +=moveY;
botpoint.x +=moveX; //botPoint rect的右下脚坐标点
botpoint.y +=moveY;

dc.Rectangle(CRect(oldpoint,botpoint));
dc.SelectStockObject(NULL_BRUSH);
Invalidate(FALSE);
}

CView::OnMouseMove(nFlags, point);
}

void CRectTView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
bMove = FALSE;
CView::OnLButtonUp(nFlags, point);
}

问题是拖动的时候不是平缓连续的,而且会有很多矩形出现,而不是只有原来的那个矩形在移动。
请大家帮我找找原因。
...全文

相关推荐

评论 5
收藏 已收藏
分享
返回顶部
easybf
图形处理/算法 19,463 成员 50,678 内容