reduce window flickering by avoid unnecessary SetWindowPlacement() call. Revert complicated SetWindowPos() call to simple MoveWindow() call

This commit is contained in:
Kang Yu 2020-02-22 12:43:06 -08:00
parent 11cd6f568d
commit 060e1f67e1
2 changed files with 22 additions and 14 deletions

View file

@ -11,6 +11,7 @@ namespace Ninjacrab.PersistentWindows.Common.Models
public string ApplicationName { get; set; }
public RECT ScreenPosition { get; set; }
public WindowPlacement WindowPlacement { get; set; }
public bool NeedUpdateWindowPlacement { get; set; }
public static string GetKey(IntPtr hWnd, string applicationName)
{

View file

@ -460,6 +460,7 @@ namespace Ninjacrab.PersistentWindows.Common
ProcessId = processId,
WindowPlacement = windowPlacement,
NeedUpdateWindowPlacement = false,
ScreenPosition = screenPosition
};
@ -477,10 +478,6 @@ namespace Ninjacrab.PersistentWindows.Common
monitorApplications[displayKey].Remove(curDisplayMetrics.Key);
needUpdate = true;
}
else if (!prevDisplayMetrics.ScreenPosition.Equals(curDisplayMetrics.ScreenPosition))
{
needUpdate = true;
}
else if (!prevDisplayMetrics.EqualPlacement(curDisplayMetrics))
{
Log.Trace("Unexpected WindowPlacement.NormalPosition change if ScreenPosition keep same {0} {1} {2}",
@ -524,6 +521,11 @@ namespace Ninjacrab.PersistentWindows.Common
}
*/
//monitorApplications[displayKey][curDisplayMetrics.Key].WindowPlacement = curDisplayMetrics.WindowPlacement;
curDisplayMetrics.NeedUpdateWindowPlacement = true;
needUpdate = true;
}
else if (!prevDisplayMetrics.ScreenPosition.Equals(curDisplayMetrics.ScreenPosition))
{
needUpdate = true;
}
else
@ -617,7 +619,7 @@ namespace Ninjacrab.PersistentWindows.Common
// block capture for current hwnd
this.curRestoreHwnd = hwnd;
bool success;
bool success = true;
// recover NormalPosition (the workspace position prior to snap)
if (windowPlacement.ShowCmd == ShowWindowCommands.Maximize)
{
@ -628,18 +630,22 @@ namespace Ninjacrab.PersistentWindows.Common
User32.SetWindowPlacement(hwnd, ref windowPlacement);
windowPlacement.ShowCmd = ShowWindowCommands.Maximize;
}
success = User32.SetWindowPlacement(hwnd, ref windowPlacement);
Log.Info("SetWindowPlacement({0} [{1}x{2}]-[{3}x{4}]) - {5}",
window.Process.ProcessName,
windowPlacement.NormalPosition.Left,
windowPlacement.NormalPosition.Top,
windowPlacement.NormalPosition.Width,
windowPlacement.NormalPosition.Height,
success);
if (curDisplayMetrics.NeedUpdateWindowPlacement)
{
success &= User32.SetWindowPlacement(hwnd, ref windowPlacement);
Log.Info("SetWindowPlacement({0} [{1}x{2}]-[{3}x{4}]) - {5}",
window.Process.ProcessName,
windowPlacement.NormalPosition.Left,
windowPlacement.NormalPosition.Top,
windowPlacement.NormalPosition.Width,
windowPlacement.NormalPosition.Height,
success);
}
// recover previous screen position
RECT rect = prevDisplayMetrics.ScreenPosition;
//success &= User32.MoveWindow(hwnd, rect.Left, rect.Top, rect.Width, rect.Height, true);
success &= User32.MoveWindow(hwnd, rect.Left, rect.Top, rect.Width, rect.Height, true);
/*
success &= User32.SetWindowPos(
window.HWnd,
IntPtr.Zero,
@ -660,6 +666,7 @@ namespace Ninjacrab.PersistentWindows.Common
rect.Height,
SetWindowPosFlags.DoNotActivate |
SetWindowPosFlags.DoNotChangeOwnerZOrder);
*/
Log.Info("MoveWindow({0} [{1}x{2}]-[{3}x{4}]) - {5}",
window.Process.ProcessName,