windows 以编程方式最小化/恢复窗口跳过动画效果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6078799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:53:40  来源:igfitidea点击:

Minimize/restore windows programmatically skipping the animation effect

windowswinapiautomationsendmessagewindows-shell

提问by GetFree

I need to perform several operations on a list of windows (minimize some of them, restore others) in order to switch between two or more set of windows at once.

我需要对一个窗口列表执行多个操作(最小化其中一些,恢复其他),以便一次在两组或更多组窗口之间切换。

The problem with this are those animations you can see when minimizing and restoring a window. The whole process look terrible with all those animations going in and out, up and down.
I cannot, however, disable those animations because this is for other computers and i dont want to change other people's settings, plus those animations are actually useful when you minimize/restore one window only (i.e. when YOU do it manually) because you can see what is happening, but for doing it programmatically on several windows at a time, it's not nice.

问题在于您在最小化和恢复窗口时可以看到的那些动画。整个过程看起来很糟糕,所有这些动画进进出出,上下。
但是,我无法禁用这些动画,因为这是针对其他计算机的,我不想更改其他人的设置,而且当您仅最小化/恢复一个窗口时(即,当您手动执行时),这些动画实际上很有用,因为您可以看到发生了什么,但是一次在多个窗口上以编程方式进行操作,这并不好。

I'm currenlty using the SendMessagefunction to send the WM_SYSCOMMANDmessage with params SC_MINIMIZE/SC_RESTORE. I dont know whether there is another way.

我目前正在使用该SendMessage函数发送WM_SYSCOMMAND带有 params SC_MINIMIZE/的消息SC_RESTORE。不知道有没有别的办法。

So, the question:
How can I minimize/restore a window programatically without the animation effect??

所以,问题是:
如何在没有动画效果的情况下以编程方式最小化/恢复窗口?

PS: The programming language is not important. I can use any language that's nessesary for accomplishing this.

PS:编程语言并不重要。我可以使用任何必要的语言来完成这项工作。

采纳答案by Sertac Akyuz

SetWindowPlacementwith SW_SHOWMINIMIZEDor SW_RESTOREas appropriate for showCmdin WINDOWPLACEMENTseems to bypass window animation. I'd keep an eye on the functionality for future versions of the OS though since documentation does not mention anything about animation.

SetWindowPlacementwithSW_SHOWMINIMIZEDSW_RESTOREas 适合showCmdinWINDOWPLACEMENT似乎绕过了窗口动画。我会密切关注操作系统未来版本的功能,因为文档没有提到任何关于动画的内容。

回答by Adrian McCarthy

You could temporarily disable the animations and then restore the user's original setting.

您可以暂时禁用动画,然后恢复用户的原始设置。

class WindowsAnimationSuppressor {
  public:
    WindowsAnimationSuppressor() : m_suppressed(false) {
      m_original_settings.cbSize = sizeof(m_original_settings);
      if (::SystemParametersInfo(SPI_GETANIMATION,
                                 sizeof(m_original_settings),
                                 &m_original_settings, 0)) {
        ANIMATIONINFO no_animation = { sizeof(no_animation), 0 };
        ::SystemParametersInfo(SPI_SETANIMATION,
                               sizeof(no_animation), &no_animation,
                               SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
        m_suppressed = true;
      }
    }

    ~WindowsAnimationSuppressor() {
      if (m_suppressed) {
        ::SystemParametersInfo(SPI_SETANIMATION,
                               sizeof(m_original_settings),
                               &m_original_settings,
                               SPIF_UPDATEINIFILE | SPIF_SENDCHANGE);
      }
    }

  private:
    bool m_suppressed;
    ANIMATIONINFO m_original_settings;
};

void RearrangeWindows() {
  WindowsAnimationSuppressor suppressor;

  // Rearrange the windows here ...
}

When the suppressor is constructed, it remembers the user's original setting and turns off the animation. The destructor restores the original settings. By using a c'tor/d'tor, you ensure that the user's settings are restored if your rearranging code throws an exception.

构造抑制器时,它会记住用户的原始设置并关闭动画。析构函数恢复原始设置。通过使用 c'tor/d'tor,您可以确保在您的重新排列代码引发异常时恢复用户的设置。

There is a small window of vulnerability here. In theory, the user could change the setting during the operation, and then you'll slam the original setting back. That's extremely rare and not really that bad.

这里有一个小的漏洞窗口。理论上,用户可以在操作过程中更改设置,然后您将猛烈地恢复原始设置。这是非常罕见的,也没有那么糟糕。

回答by NGLN

How about Hide > Minimize > Show ?

隐藏 > 最小化 > 显示怎么样?