c++ - 如何以编程方式缓慢移动窗口,就好像用户正在做一样?

标签 c++ c winapi

我知道 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/

相关文章:

c++ - 虚拟继承与非默认构造函数

c - 从 Makefile 将变量导入 C 程序

c++ - 在析构函数中调用 CloseHandle 会导致运行时内存错误,如何正确关闭结构/类中的文件句柄?

Winapi:以编程方式获取特殊文件夹的路径(给定其 CLSID 作为字符串)

C#:如何唤醒已关闭的系统?

c++ - 删除 vector 问题 vector 中的对象

c++ - 2D vector - 通过搜索删除行

c++ - 对于模板类,非泛型成员函数是否会被复制?

c - C中不兼容类型和不同命名空间的分配

C 相同结构不同尺寸