diff --git a/Core/AppWindow.cs b/Core/AppWindow.cs index e0d70a1..86b987d 100644 --- a/Core/AppWindow.cs +++ b/Core/AppWindow.cs @@ -26,6 +26,7 @@ using System.Runtime.Caching; using System.Runtime.InteropServices; using System.Text; +using System.Windows.Forms; using ManagedWinapi.Windows; namespace Switcheroo.Core @@ -100,6 +101,33 @@ public void SwitchToLastVisibleActivePopup() WinApi.SwitchToThisWindow(lastActiveVisiblePopup, true); } + public void MoveToActiveScreen(Screen from_screen, Screen to_screen) + { + var lastActiveVisiblePopup = GetLastActiveVisiblePopup(); + int X = to_screen.Bounds.X + Location.X - from_screen.Bounds.X; + int Y = to_screen.Bounds.Y + Location.Y - from_screen.Bounds.Y; + + // make sure the window is not moved out of bound + if (X > (to_screen.Bounds.X + to_screen.Bounds.Width) ) + { + X = to_screen.Bounds.X; + if (Size.Width < to_screen.Bounds.Width) + { + X = to_screen.Bounds.X + to_screen.Bounds.Width / 2 - Size.Width / 2; + } + } + if (Y > (to_screen.Bounds.Y + to_screen.Bounds.Height)) + { + Y = to_screen.Bounds.Y; + if (Size.Height < to_screen.Bounds.Height) + { + Y = to_screen.Bounds.Y + to_screen.Bounds.Height / 2 - Size.Height / 2; + } + } + + WinApi.SetWindowPos(lastActiveVisiblePopup, WinApi.HWND_Z_ORDER.HWND_TOP, X, Y, Size.Width, Size.Height, WinApi.SetWindowFlags.SWP_SHOWWINDOW | WinApi.SetWindowFlags.SWP_NOSIZE); + } + public AppWindow Owner { get diff --git a/Core/WinApi.cs b/Core/WinApi.cs index 7c515c4..0f044e2 100644 --- a/Core/WinApi.cs +++ b/Core/WinApi.cs @@ -47,6 +47,71 @@ internal static class WinApi [DllImport("user32.dll", SetLastError = true)] public static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab); + [DllImport("user32.dll")] + [return: MarshalAs(UnmanagedType.Bool)] + public static extern bool SetWindowPos(IntPtr hWnd, HWND_Z_ORDER hWndInsertAfter, int X, int Y, int cX, int cY, SetWindowFlags uFlags); + + public enum HWND_Z_ORDER + { + HWND_BOTTOM = 1, + // Places the window at the bottom of the Z order.If the hWnd parameter identifies a topmost window, the window loses its topmost status and is placed at the bottom of all other windows. + HWND_NOTOPMOST = -2, + // Places the window above all non-topmost windows (that is, behind all topmost windows). This flag has no effect if the window is already a non-topmost window. + HWND_TOP = 0, + // Places the window at the top of the Z order. + HWND_TOPMOST = -1 + // Places the window above all non-topmost windows.The window maintains its topmost position even when it is deactivated. + } + + [Flags] + public enum SetWindowFlags + { + SWP_ASYNCWINDOWPOS = 0x4000, + // If the calling thread and the thread that owns the window are attached to different input queues, the system posts the request to the thread that owns the window. This prevents the calling thread from blocking its execution while other threads process the request. + + SWP_DEFERERASE = 0x2000, + // Prevents generation of the WM_SYNCPAINT message. + + SWP_DRAWFRAME = 0x0020, + // Draws a frame (defined in the window's class description) around the window. + + SWP_FRAMECHANGED = 0x0020, + //Applies new frame styles set using the SetWindowLong function.Sends a WM_NCCALCSIZE message to the window, even if the window's size is not being changed. If this flag is not specified, WM_NCCALCSIZE is sent only when the window's size is being changed. + + SWP_HIDEWINDOW = 0x0080, + // Hides the window. + + SWP_NOACTIVATE = 0x0010, + // Does not activate the window.If this flag is not set, the window is activated and moved to the top of either the topmost or non-topmost group (depending on the setting of the hWndInsertAfter parameter). + + SWP_NOCOPYBITS = 0x0100, + //Discards the entire contents of the client area.If this flag is not specified, the valid contents of the client area are saved and copied back into the client area after the window is sized or repositioned. + + SWP_NOMOVE = 0x0002, + //Retains the current position (ignores X and Y parameters). + + SWP_NOOWNERZORDER = 0x0200, + // Does not change the owner window's position in the Z order. + + SWP_NOREDRAW = 0x0008, + // Does not redraw changes.If this flag is set, no repainting of any kind occurs. This applies to the client area, the nonclient area (including the title bar and scroll bars), and any part of the parent window uncovered as a result of the window being moved.When this flag is set, the application must explicitly invalidate or redraw any parts of the window and parent window that need redrawing. + + SWP_NOREPOSITION = 0x0200, + // Same as the SWP_NOOWNERZORDER flag. + + SWP_NOSENDCHANGING = 0x0400, + // Prevents the window from receiving the WM_WINDOWPOSCHANGING message. + + SWP_NOSIZE = 0x0001, + // Retains the current size (ignores the cx and cy parameters). + + SWP_NOZORDER = 0x0004, + // Retains the current Z order (ignores the hWndInsertAfter parameter). + + SWP_SHOWWINDOW = 0x0040 + // Displays the window + } + public enum GetAncestorFlags { /// diff --git a/Switcheroo/MainWindow.xaml.cs b/Switcheroo/MainWindow.xaml.cs index a2865b4..adb08e0 100644 --- a/Switcheroo/MainWindow.xaml.cs +++ b/Switcheroo/MainWindow.xaml.cs @@ -378,7 +378,15 @@ private void Switch() foreach (var item in lb.SelectedItems) { var win = (AppWindowViewModel)item; + Screen active_screen = PickScreen(); + Screen window_screen = Screen.AllScreens.First(s => s.Bounds.Contains(win.AppWindow.Location.X, win.AppWindow.Location.Y)); + win.AppWindow.SwitchToLastVisibleActivePopup(); + + if (Settings.Default.MoveWindows && active_screen != window_screen) + { + win.AppWindow.MoveToActiveScreen(window_screen, active_screen); + } } HideWindow(); diff --git a/Switcheroo/OptionsWindow.xaml b/Switcheroo/OptionsWindow.xaml index 4cb443b..ae386ea 100644 --- a/Switcheroo/OptionsWindow.xaml +++ b/Switcheroo/OptionsWindow.xaml @@ -2,7 +2,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Switcheroo Options" ResizeMode="NoResize" SizeToContent="WidthAndHeight" - ShowInTaskbar="False" WindowStyle="ToolWindow" Background="{x:Static SystemColors.WindowBrush}" Height="380" + ShowInTaskbar="False" WindowStyle="ToolWindow" Background="{x:Static SystemColors.WindowBrush}" Height="430" MaxWidth="350"> @@ -44,6 +44,8 @@ Active Monitor Mouse Location + + diff --git a/Switcheroo/OptionsWindow.xaml.cs b/Switcheroo/OptionsWindow.xaml.cs index a1b61d0..c742a2d 100644 --- a/Switcheroo/OptionsWindow.xaml.cs +++ b/Switcheroo/OptionsWindow.xaml.cs @@ -68,6 +68,7 @@ public OptionsWindow() AutoSwitch.IsEnabled = Settings.Default.AltTabHook; RunAsAdministrator.IsChecked = Settings.Default.RunAsAdmin; MultiMonSelector.SelectedIndex = Settings.Default.MultiMonitor; + MoveWindows.IsChecked = Settings.Default.MoveWindows; } private void Cancel_Click(object sender, RoutedEventArgs e) @@ -109,6 +110,7 @@ private void Ok_Click(object sender, RoutedEventArgs e) Settings.Default.AutoSwitch = AutoSwitch.IsChecked.GetValueOrDefault(); Settings.Default.RunAsAdmin = RunAsAdministrator.IsChecked.GetValueOrDefault(); Settings.Default.MultiMonitor = MultiMonSelector.SelectedIndex; + Settings.Default.MoveWindows = MoveWindows.IsChecked.GetValueOrDefault(); Settings.Default.Save(); if (closeOptionsWindow) diff --git a/Switcheroo/Properties/Settings.Designer.cs b/Switcheroo/Properties/Settings.Designer.cs index cb07ada..231f6af 100644 --- a/Switcheroo/Properties/Settings.Designer.cs +++ b/Switcheroo/Properties/Settings.Designer.cs @@ -145,6 +145,21 @@ public bool EnableHotKey { [global::System.Configuration.UserScopedSettingAttribute()] [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("False")] + public bool MoveWindows + { + get + { + return ((bool)(this["MoveWindows"])); + } + set + { + this["MoveWindows"] = value; + } + } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] [global::System.Configuration.DefaultSettingValueAttribute("0")] public int MultiMonitor { get {