Changing a window's restore position using SetWindowPlacement doesn't work on every window

心不动则不痛 提交于 2019-12-02 00:58:27

Yes, this is by design. The Form class keeps track of the restore bounds itself, necessary so it can properly reposition the window after it was re-created. Windows Forms often recreates the window on-the-fly to implement property setters for properties that can only be specified by CreateWindowEx(). Like ShowInTaskbar.

The private RestoreWindowBoundsIfNecessary() method puts the window back, it will run when the window is restored. From what I can tell, the restore bounds are latched just before the window is minimized or maximized. If you want to modify the restore position while the window is min/maximized then you'll have to use MoveWindow to move it where you want it to go after restoring the window. Ought to produce some flicker.

I was able to avoid any flicker just by setting the window's size and location in a later spin of the dispatcher. Then, as Hans so helpfully pointed out, RestoreWindowBoundsIfNecessary will use that size and location when restoring the window.

Form window = ...
window.StartPosition = FormStartPosition.Manual
window.WindowState = savedState;
if (window.WindowState == FormWindowState.Normal)
{
    window.Location = savedLocation;
    window.Size = savedSize;
}
else
{
    window.BeginInvoke((MethodInvoker)delegate 
    {
        window.Location = savedLocation;
        window.Size = savedSize;
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!