我目前要在对话框上实现鼠标移动一个标签控件的功能,可是移动中总是会有很多问题,
1 移动的时候屏幕闪的好厉害
2 移动控件的响应速度好慢
3 当先点击移动几次后,然后我在控件上不先点击他,直接在控件上移动鼠标,那控件也会跟着移动
请大家帮我分析下代码,这是我项目中的一个功能,可就这个问题都搞了2天了,请大家帮下我这个菜鸟,不胜感激!
具体代码如下://鼠标左键点击
void CMyStaticButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bIsPush = TRUE;
m_OldPoint = point;
CStatic::OnLButtonDown(nFlags, point);
}
//鼠标左键弹起
void CMyStaticButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bIsPush = FALSE;
CStatic::OnLButtonUp(nFlags, point);
}//鼠标移动
void CMyStaticButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bIsPush==TRUE)
{
int nX = point.x - m_OldPoint.x;
int nY = point.y - m_OldPoint.y;
m_OldPoint = point;
CRect rt;
GetWindowRect(&rt);/**/
GetParent()->ScreenToClient(&rt);
rt.left += nX;
rt.right += nX;
rt.top += nY;
rt.bottom += nY;
//
MoveWindow(rt, false);
GetParent()->Invalidate();
}
Static::OnMouseMove(nFlags, point);
}
1 移动的时候屏幕闪的好厉害
2 移动控件的响应速度好慢
3 当先点击移动几次后,然后我在控件上不先点击他,直接在控件上移动鼠标,那控件也会跟着移动
请大家帮我分析下代码,这是我项目中的一个功能,可就这个问题都搞了2天了,请大家帮下我这个菜鸟,不胜感激!
具体代码如下://鼠标左键点击
void CMyStaticButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bIsPush = TRUE;
m_OldPoint = point;
CStatic::OnLButtonDown(nFlags, point);
}
//鼠标左键弹起
void CMyStaticButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bIsPush = FALSE;
CStatic::OnLButtonUp(nFlags, point);
}//鼠标移动
void CMyStaticButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if(m_bIsPush==TRUE)
{
int nX = point.x - m_OldPoint.x;
int nY = point.y - m_OldPoint.y;
m_OldPoint = point;
CRect rt;
GetWindowRect(&rt);/**/
GetParent()->ScreenToClient(&rt);
rt.left += nX;
rt.right += nX;
rt.top += nY;
rt.bottom += nY;
//
MoveWindow(rt, false);
GetParent()->Invalidate();
}
Static::OnMouseMove(nFlags, point);
}
你干脆,将GetParent()这个窗口(估计是对话框吧),在里面重载画底色的,直接返回,不要让重新画底色了!可能闪烁会好些!
另外,建议将Invalidate() 用InvalidateRect();因为Invalidate()刷新整个窗口,而InvalidateRect刷新窗口的部分,也就是传入参数的rect大小,你只要求出要刷新的rect即可!
2、由于移动的过程不停的刷新、设置窗口位置,所以,感觉很慢
3、在OnMouseMove(UINT nFlags, CPoint point)中,判断nFlags的状态,是否已经左键按键!
if(m_bIsPush && (MK_LBUTTON&nFlags))
{
}最后,建议你移动的过程,不要真的去控件,用一个方框代替当前的控件的位置;而鼠标松下的时候,再将当前控件移过来!
不要在OnMouseMove里面
改成
//鼠标左键点击
void CMyStaticButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default m_OldPoint = point;
CStatic::OnLButtonDown(nFlags, point);
}
//鼠标左键弹起
void CMyStaticButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
int nX = point.x - m_OldPoint.x;
int nY = point.y - m_OldPoint.y;
m_OldPoint = point;
CRect rt;
GetWindowRect(&rt);/**/
GetParent()->ScreenToClient(&rt);
rt.left += nX;
rt.right += nX;
rt.top += nY;
rt.bottom += nY;
MoveWindow(rt, false);
GetParent()->Invalidate();
CStatic::OnLButtonUp(nFlags, point);
} //鼠标移动
void CMyStaticButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default Static::OnMouseMove(nFlags, point);
}
这样有改就是运动的轨迹看不到了
UpdteWindow();Invalidate(0的话整个窗口都要重绘的,哪样子的话会很抖的···
只是我消息重载是在我定义的控件类里面实现的
只是在移动控件的时候出现了问题
最好在OnLButtonUp()中处理
现在的截面不怎么闪了,但是移动的效果不是很好,有时候还是移动不了,不知道为什么void CMyStaticButton::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
m_bIsPush = TRUE; //鼠标已经按下
m_OldPoint = point;
CStatic::OnLButtonDown(nFlags, point);
}void CMyStaticButton::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
// BOOL testBool=FALSE;
if(m_bIsMove) //鼠标移动后
{
int nX = point.x - m_OldPoint.x;
int nY = point.y - m_OldPoint.y;
m_OldPoint = point;
CRect rt;
GetWindowRect(&rt);
GetParent()->ScreenToClient(&rt);
rt.left += nX;
rt.right += nX;
rt.top += nY;
rt.bottom += nY;
MoveWindow(rt, FALSE);
GetParent()->Invalidate();
//UpdateWindow();
}
//GetParent()->Invalidate();
m_bIsPush = FALSE;
m_bIsMove = FALSE;
CStatic::OnLButtonUp(nFlags, point);
}void CMyStaticButton::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_bIsPush)
{
m_bIsMove=TRUE; //鼠标已经移动
}
CStatic::OnMouseMove(nFlags, point);
}