From 984c89e09994d66c68c7334621dd9455de902da3 Mon Sep 17 00:00:00 2001 From: Kang Yu Date: Sat, 2 Mar 2024 20:24:43 -0800 Subject: [PATCH] new feature: shift click PW icon to center foreground window --- .../Common/PersistentWindowProcessor.cs | 12 +++++++++--- .../SystrayShell/Program.cs | 5 +++++ .../SystrayShell/SystrayForm.cs | 2 ++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs b/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs index 689c14a..f7bb9ce 100644 --- a/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs +++ b/Ninjacrab.PersistentWindows.Solution/Common/PersistentWindowProcessor.cs @@ -947,6 +947,14 @@ namespace PersistentWindows.Common return offscreen; } + public void CenterWindow(IntPtr hwnd) + { + IntPtr desktopWindow = User32.GetDesktopWindow(); + RECT target_rect = new RECT(); + User32.GetWindowRect(desktopWindow, ref target_rect); + User32.MoveWindow(hwnd, target_rect.Left + target_rect.Width / 4, target_rect.Top + target_rect.Height / 4, target_rect.Width / 2, target_rect.Height / 2, true); + } + public bool RecallLastKilledPosition(IntPtr hwnd) { if (deadApps.ContainsKey(curDisplayKey)) @@ -1202,9 +1210,7 @@ namespace PersistentWindows.Common if (IsOffScreen(hwnd)) { - IntPtr desktopWindow = User32.GetDesktopWindow(); - User32.GetWindowRect(desktopWindow, ref target_rect); - User32.MoveWindow(hwnd, target_rect.Left + 50, target_rect.Top + 50, target_rect.Width * 3 / 4, target_rect.Height * 3 / 4, true); + CenterWindow(hwnd); Log.Error("fix invisible window \"{0}\"", GetWindowTitle(hwnd)); } } diff --git a/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs b/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs index 7b9162c..e8b4a78 100644 --- a/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs +++ b/Ninjacrab.PersistentWindows.Solution/SystrayShell/Program.cs @@ -622,6 +622,11 @@ namespace PersistentWindows.SystrayShell pwp.RecallLastKilledPosition(pwp.GetForegroundWindow()); } + static public void CenterWindow() + { + pwp.CenterWindow(pwp.GetForegroundWindow()); + } + static public void PauseAutoRestore() { pwp.pauseAutoRestore = true; diff --git a/Ninjacrab.PersistentWindows.Solution/SystrayShell/SystrayForm.cs b/Ninjacrab.PersistentWindows.Solution/SystrayShell/SystrayForm.cs index cf23133..8636299 100644 --- a/Ninjacrab.PersistentWindows.Solution/SystrayShell/SystrayForm.cs +++ b/Ninjacrab.PersistentWindows.Solution/SystrayShell/SystrayForm.cs @@ -116,6 +116,8 @@ namespace PersistentWindows.SystrayShell Program.FgWindowToBottom(); else if (ctrlKeyPressed > 0 && altKeyPressed == 0 && shiftKeyPressed == 0) Program.RecallLastKilledPosition(); + else if (ctrlKeyPressed == 0 && altKeyPressed == 0 && shiftKeyPressed > 0) + Program.CenterWindow(); else if (ctrlKeyPressed == 0 && altKeyPressed == 0 && shiftKeyPressed == 0) //restore unnamed(default) snapshot Program.RestoreSnapshot(0);