我知道 MoveWindow() 和 SetWindowPos() 函数。我知道如何正确使用它们。但是,我想要完成的是缓慢而平稳地移动窗口,就好像用户正在拖动它一样。
我还没有让它正常工作。我尝试的是使用 GetWindowRect() 获取当前坐标,然后使用 setwindow 和 movewindow 函数,每次调用将 Right 递增 10 个像素。
有什么想法吗?
这是我所有定义之外的内容。
while(1)
{
GetWindowRect(notepad,&window);
Sleep(1000);
SetWindowPos(
notepad,
HWND_TOPMOST,
window.top - 10,
window.right,
400,
400,
TRUE
);
}
最佳答案
如果您想要流畅的动画,您需要使其基于时间,并允许 Windows 在移动之间处理消息。 Set a timer , 并回复 WM_TIMER notifications根据 elapsed time 将窗口移动一段距离自从你的动画开始。对于看起来自然的运动,不要使用线性函数来确定距离 - 相反,尝试类似 Robert Harvey's 的方法建议功能。
伪代码:
//
// animate as a function of time - could use something else, but time is nice.
lengthInMS = 10*1000; // ten second animation length
StartAnimation(desiredPos)
{
originalPos = GetWindowPos();
startTime = GetTickCount();
// omitted: hwnd, ID - you'll call SetTimer differently
// based on whether or not you have a window of your own
timerID = SetTimer(30, callback);
}
callback()
{
elapsed = GetTickCount()-startTime;
if ( elapsed >= lengthInMS )
{
// done - move to destination and stop animation timer.
MoveWindow(desiredPos);
KillTimer(timerID);
}
// convert elapsed time into a value between 0 and 1
pos = elapsed / lengthInMS;
// use Harvey's function to provide smooth movement between original
// and desired position
newPos.x = originalPos.x*(1-SmoothMoveELX(pos))
+ desiredPos.x*SmoothMoveELX(pos);
newPos.y = originalPos.y*(1-SmoothMoveELX(pos))
+ desiredPos.y*SmoothMoveELX(pos);
MoveWindow(newPos);
}
关于c++ - 如何以编程方式缓慢移动窗口,就好像用户正在做一样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/932706/