From ea05fcce2910f893aab45d10d7c60db011abb3c9 Mon Sep 17 00:00:00 2001 From: Brad Rhodes Date: Wed, 27 Apr 2016 10:48:39 -0600 Subject: [PATCH 1/4] Added a window position calculator that can calculate the window position given a specific screen. --- Switcheroo/ICalculateWindowPosition.cs | 9 +++++++++ Switcheroo/MainWindow.xaml.cs | 10 ++++++++-- Switcheroo/Switcheroo.csproj | 3 +++ Switcheroo/WindowPosition.cs | 8 ++++++++ Switcheroo/WindowPositionCalculator.cs | 21 +++++++++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 Switcheroo/ICalculateWindowPosition.cs create mode 100644 Switcheroo/WindowPosition.cs create mode 100644 Switcheroo/WindowPositionCalculator.cs diff --git a/Switcheroo/ICalculateWindowPosition.cs b/Switcheroo/ICalculateWindowPosition.cs new file mode 100644 index 0000000..dd80712 --- /dev/null +++ b/Switcheroo/ICalculateWindowPosition.cs @@ -0,0 +1,9 @@ +using System.Windows.Forms; + +namespace Switcheroo +{ + public interface ICalculateWindowPosition + { + WindowPosition CalculateWindowPosition(Screen screen, double windowWidth, double windowHeight); + } +} \ No newline at end of file diff --git a/Switcheroo/MainWindow.xaml.cs b/Switcheroo/MainWindow.xaml.cs index 53582d9..b0116b8 100644 --- a/Switcheroo/MainWindow.xaml.cs +++ b/Switcheroo/MainWindow.xaml.cs @@ -22,6 +22,7 @@ using System.Collections.Generic; using System.Collections.ObjectModel; using System.Diagnostics; +using System.Drawing; using System.Linq; using System.Net; using System.Reflection; @@ -56,6 +57,8 @@ public partial class MainWindow : Window public static readonly RoutedUICommand SwitchToWindowCommand = new RoutedUICommand(); public static readonly RoutedUICommand ScrollListDownCommand = new RoutedUICommand(); public static readonly RoutedUICommand ScrollListUpCommand = new RoutedUICommand(); + + private static readonly ICalculateWindowPosition WindowPositionCalculator = new WindowPositionCalculator(); private OptionsWindow _optionsWindow; private AboutWindow _aboutWindow; private AltTabHook _altTabHook; @@ -317,9 +320,12 @@ private void CenterWindow() SizeToContent = SizeToContent.Manual; SizeToContent = SizeToContent.WidthAndHeight; + var windowPosition = + WindowPositionCalculator.CalculateWindowPosition(Screen.PrimaryScreen, ActualWidth, ActualHeight); + // Position the window in the center of the screen - Left = (SystemParameters.PrimaryScreenWidth/2) - (ActualWidth/2); - Top = (SystemParameters.PrimaryScreenHeight/2) - (ActualHeight/2); + Left = windowPosition.Left; + Top = windowPosition.Top; } /// diff --git a/Switcheroo/Switcheroo.csproj b/Switcheroo/Switcheroo.csproj index 3d76abe..943b089 100644 --- a/Switcheroo/Switcheroo.csproj +++ b/Switcheroo/Switcheroo.csproj @@ -94,11 +94,14 @@ Designer + + + Designer MSBuild:Compile diff --git a/Switcheroo/WindowPosition.cs b/Switcheroo/WindowPosition.cs new file mode 100644 index 0000000..35716e1 --- /dev/null +++ b/Switcheroo/WindowPosition.cs @@ -0,0 +1,8 @@ +namespace Switcheroo +{ + public class WindowPosition + { + public double Top { get; set; } + public double Left { get; set; } + } +} \ No newline at end of file diff --git a/Switcheroo/WindowPositionCalculator.cs b/Switcheroo/WindowPositionCalculator.cs new file mode 100644 index 0000000..de2958e --- /dev/null +++ b/Switcheroo/WindowPositionCalculator.cs @@ -0,0 +1,21 @@ +using System; +using System.Drawing; +using System.Windows.Forms; + +namespace Switcheroo +{ + class WindowPositionCalculator : ICalculateWindowPosition + { + public WindowPosition CalculateWindowPosition(Screen screen, double windowWidth, double windowHeight) + { + var top = screen.Bounds.X + (((double) screen.Bounds.Width/2) - (windowWidth/2)); + var left = screen.Bounds.Y + (((double) screen.Bounds.Height/2) - (windowHeight/2)); + + return new WindowPosition + { + Left = Math.Round(screen.Bounds.X + (((double) screen.Bounds.Width/2) - (windowWidth/2))), + Top = Math.Round(screen.Bounds.Y + (((double) screen.Bounds.Height/2) - (windowHeight/2))) + }; + } + } +} \ No newline at end of file From 45489cdf220b75e466e449cd75b2014e1c4a59e1 Mon Sep 17 00:00:00 2001 From: Brad Rhodes Date: Wed, 27 Apr 2016 10:55:16 -0600 Subject: [PATCH 2/4] Changed so Switcheroo now appears on the screen with the currently active window. --- Switcheroo/MainWindow.xaml.cs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Switcheroo/MainWindow.xaml.cs b/Switcheroo/MainWindow.xaml.cs index b0116b8..14c93e1 100644 --- a/Switcheroo/MainWindow.xaml.cs +++ b/Switcheroo/MainWindow.xaml.cs @@ -320,8 +320,10 @@ private void CenterWindow() SizeToContent = SizeToContent.Manual; SizeToContent = SizeToContent.WidthAndHeight; + var screen = Screen.FromHandle(_foregroundWindow.HWnd); + var windowPosition = - WindowPositionCalculator.CalculateWindowPosition(Screen.PrimaryScreen, ActualWidth, ActualHeight); + WindowPositionCalculator.CalculateWindowPosition(screen, ActualWidth, ActualHeight); // Position the window in the center of the screen Left = windowPosition.Left; From 2bc59495d8814c28bedeb189c30d64be3df2618f Mon Sep 17 00:00:00 2001 From: Brad Rhodes Date: Wed, 27 Apr 2016 11:28:51 -0600 Subject: [PATCH 3/4] Added option for Multi-Monitor Support. It allows you to choose to use the Default monitor or to use the Active monitor. --- Switcheroo/MainWindow.xaml.cs | 26 +++++++++++++++++++--- Switcheroo/OptionsWindow.xaml | 5 +++++ Switcheroo/OptionsWindow.xaml.cs | 2 ++ Switcheroo/Properties/Settings.Designer.cs | 16 +++++++++++-- Switcheroo/Properties/Settings.settings | 7 +++--- Switcheroo/WindowPositionCalculator.cs | 7 +++--- Switcheroo/app.config | 3 +++ 7 files changed, 55 insertions(+), 11 deletions(-) diff --git a/Switcheroo/MainWindow.xaml.cs b/Switcheroo/MainWindow.xaml.cs index 14c93e1..643b87c 100644 --- a/Switcheroo/MainWindow.xaml.cs +++ b/Switcheroo/MainWindow.xaml.cs @@ -320,16 +320,36 @@ private void CenterWindow() SizeToContent = SizeToContent.Manual; SizeToContent = SizeToContent.WidthAndHeight; - var screen = Screen.FromHandle(_foregroundWindow.HWnd); - var windowPosition = - WindowPositionCalculator.CalculateWindowPosition(screen, ActualWidth, ActualHeight); + WindowPositionCalculator.CalculateWindowPosition(PickScreen(), ActualWidth, ActualHeight); // Position the window in the center of the screen Left = windowPosition.Left; Top = windowPosition.Top; } + /// + /// Decides which screen to pop Switcheroo up + /// + /// + private Screen PickScreen() + { + Screen screen; + switch (Settings.Default.MultiMonitor) + { + case 0: + screen = Screen.PrimaryScreen; + break; + case 1: + screen = Screen.FromHandle(_foregroundWindow.HWnd); + break; + default: + screen = Screen.PrimaryScreen; + break; + } + return screen; + } + /// /// Switches the window associated with the selected item. /// diff --git a/Switcheroo/OptionsWindow.xaml b/Switcheroo/OptionsWindow.xaml index 506d3d9..4035943 100644 --- a/Switcheroo/OptionsWindow.xaml +++ b/Switcheroo/OptionsWindow.xaml @@ -17,6 +17,11 @@ GotFocus="HotkeyPreview_OnGotFocus" LostFocus="HotkeyPreview_OnLostFocus" /> Activate Switcheroo with Alt+Tab + + + Default + Active Monitor + diff --git a/Switcheroo/OptionsWindow.xaml.cs b/Switcheroo/OptionsWindow.xaml.cs index debc3a4..8167354 100644 --- a/Switcheroo/OptionsWindow.xaml.cs +++ b/Switcheroo/OptionsWindow.xaml.cs @@ -63,6 +63,7 @@ public OptionsWindow() HotkeyPreview.Text = _hotkeyViewModel.ToString(); AltTabCheckBox.IsChecked = Settings.Default.AltTabHook; + MultiMonSelector.SelectedIndex = Settings.Default.MultiMonitor; } private void Cancel_Click(object sender, RoutedEventArgs e) @@ -95,6 +96,7 @@ private void Ok_Click(object sender, RoutedEventArgs e) } Settings.Default.AltTabHook = AltTabCheckBox.IsChecked.GetValueOrDefault(); + Settings.Default.MultiMonitor = MultiMonSelector.SelectedIndex; Settings.Default.Save(); if (closeOptionsWindow) diff --git a/Switcheroo/Properties/Settings.Designer.cs b/Switcheroo/Properties/Settings.Designer.cs index 52e85fa..3423490 100644 --- a/Switcheroo/Properties/Settings.Designer.cs +++ b/Switcheroo/Properties/Settings.Designer.cs @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34014 +// Runtime Version:4.0.30319.42000 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -12,7 +12,7 @@ namespace Switcheroo.Properties { [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] - [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "12.0.0.0")] + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "14.0.0.0")] internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase { private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); @@ -106,5 +106,17 @@ public bool AltTabHook { this["AltTabHook"] = value; } } + + [global::System.Configuration.UserScopedSettingAttribute()] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Configuration.DefaultSettingValueAttribute("0")] + public int MultiMonitor { + get { + return ((int)(this["MultiMonitor"])); + } + set { + this["MultiMonitor"] = value; + } + } } } diff --git a/Switcheroo/Properties/Settings.settings b/Switcheroo/Properties/Settings.settings index bcb2fd2..9d2da81 100644 --- a/Switcheroo/Properties/Settings.settings +++ b/Switcheroo/Properties/Settings.settings @@ -1,7 +1,5 @@  - - + @@ -25,5 +23,8 @@ False + + 0 + \ No newline at end of file diff --git a/Switcheroo/WindowPositionCalculator.cs b/Switcheroo/WindowPositionCalculator.cs index de2958e..8fcf949 100644 --- a/Switcheroo/WindowPositionCalculator.cs +++ b/Switcheroo/WindowPositionCalculator.cs @@ -6,11 +6,12 @@ namespace Switcheroo { class WindowPositionCalculator : ICalculateWindowPosition { + public WindowPositionCalculator() + { + + } public WindowPosition CalculateWindowPosition(Screen screen, double windowWidth, double windowHeight) { - var top = screen.Bounds.X + (((double) screen.Bounds.Width/2) - (windowWidth/2)); - var left = screen.Bounds.Y + (((double) screen.Bounds.Height/2) - (windowHeight/2)); - return new WindowPosition { Left = Math.Round(screen.Bounds.X + (((double) screen.Bounds.Width/2) - (windowWidth/2))), diff --git a/Switcheroo/app.config b/Switcheroo/app.config index fe3265c..23eeb9f 100644 --- a/Switcheroo/app.config +++ b/Switcheroo/app.config @@ -32,6 +32,9 @@ False + + 0 + From 23d187be6b73cf44dd6dfcabc525c6dac30e06bb Mon Sep 17 00:00:00 2001 From: Brad Rhodes Date: Wed, 27 Apr 2016 13:03:07 -0600 Subject: [PATCH 4/4] Added support for Multi-Monitor determined by mouse position as suggested in Issue #60 by egabrum. --- Switcheroo/MainWindow.xaml.cs | 4 ++++ Switcheroo/OptionsWindow.xaml | 3 ++- Switcheroo/WindowPositionCalculator.cs | 4 ---- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/Switcheroo/MainWindow.xaml.cs b/Switcheroo/MainWindow.xaml.cs index 643b87c..0c6f641 100644 --- a/Switcheroo/MainWindow.xaml.cs +++ b/Switcheroo/MainWindow.xaml.cs @@ -42,6 +42,7 @@ using Application = System.Windows.Application; using MenuItem = System.Windows.Forms.MenuItem; using MessageBox = System.Windows.MessageBox; +using MouseCursor = System.Windows.Forms.Cursor; namespace Switcheroo { @@ -343,6 +344,9 @@ private Screen PickScreen() case 1: screen = Screen.FromHandle(_foregroundWindow.HWnd); break; + case 2: + screen = Screen.AllScreens.First(s => s.Bounds.Contains(MouseCursor.Position.X, MouseCursor.Position.Y)); + break; default: screen = Screen.PrimaryScreen; break; diff --git a/Switcheroo/OptionsWindow.xaml b/Switcheroo/OptionsWindow.xaml index 4035943..3c60660 100644 --- a/Switcheroo/OptionsWindow.xaml +++ b/Switcheroo/OptionsWindow.xaml @@ -19,8 +19,9 @@ Activate Switcheroo with Alt+Tab - Default + Default Monitor Active Monitor + Mouse Location diff --git a/Switcheroo/WindowPositionCalculator.cs b/Switcheroo/WindowPositionCalculator.cs index 8fcf949..b79059f 100644 --- a/Switcheroo/WindowPositionCalculator.cs +++ b/Switcheroo/WindowPositionCalculator.cs @@ -6,10 +6,6 @@ namespace Switcheroo { class WindowPositionCalculator : ICalculateWindowPosition { - public WindowPositionCalculator() - { - - } public WindowPosition CalculateWindowPosition(Screen screen, double windowWidth, double windowHeight) { return new WindowPosition