Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to move windows to active monitor when multi-monitor #131

Open
wants to merge 1 commit into
base: fix-multimonitor
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Core/AppWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions Core/WinApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
Expand Down
8 changes: 8 additions & 0 deletions Switcheroo/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion Switcheroo/OptionsWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -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">
<Window.Resources>
</Window.Resources>
Expand Down Expand Up @@ -44,6 +44,8 @@
<ComboBoxItem>Active Monitor</ComboBoxItem>
<ComboBoxItem>Mouse Location</ComboBoxItem>
</ComboBox>
<CheckBox x:Name="MoveWindows" Margin="5,5,5,1" Content="Move switched windows to Active Monitor"/>
<TextBlock Margin="25,0,5,10" FontSize="10" Foreground="DimGray" TextWrapping="Wrap"><Run Text="Move selected windows to Monitor which Switcheroo appears."/></TextBlock>
<Grid Margin="5">
<Grid.ColumnDefinitions>
<ColumnDefinition />
Expand Down
2 changes: 2 additions & 0 deletions Switcheroo/OptionsWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
15 changes: 15 additions & 0 deletions Switcheroo/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.