diff --git a/Control/Windows/IconfontWindow.xaml.cs b/Control/Windows/IconfontWindow.xaml.cs index 0f5e4bb..4ad380e 100644 --- a/Control/Windows/IconfontWindow.xaml.cs +++ b/Control/Windows/IconfontWindow.xaml.cs @@ -10,7 +10,7 @@ using System.Windows; using System.Windows.Controls; using System.Windows.Input; - +using static GeekDesk.Util.ShowWindowFollowMouse; namespace GeekDesk.Control.Windows { @@ -132,6 +132,7 @@ namespace GeekDesk.Control.Windows window = new IconfontWindow(listInfo, menuInfo); } window.Show(); + ShowWindowFollowMouse.Show(window, MousePosition.LEFT_CENTER, 0, 0); } private void CustomButton_Click(object sender, RoutedEventArgs e) diff --git a/GeekDesk.csproj b/GeekDesk.csproj index 1e6e5fb..a3ebc62 100644 --- a/GeekDesk.csproj +++ b/GeekDesk.csproj @@ -173,6 +173,7 @@ + diff --git a/MainWindow.xaml.cs b/MainWindow.xaml.cs index b140da9..56af67e 100644 --- a/MainWindow.xaml.cs +++ b/MainWindow.xaml.cs @@ -18,6 +18,7 @@ using System.Windows.Controls; using System.Windows.Input; using System.Windows.Interop; using System.Windows.Media; +using static GeekDesk.Util.ShowWindowFollowMouse; namespace GeekDesk { @@ -260,7 +261,7 @@ namespace GeekDesk { if (appData.AppConfig.FollowMouse) { - ShowAppAndFollowMouse(); + ShowWindowFollowMouse.Show(mainWindow, MousePosition.CENTER, 0, 0); } else { mainWindow.Visibility = Visibility.Visible; @@ -268,54 +269,7 @@ namespace GeekDesk Keyboard.Focus(mainWindow); } - /// - /// 随鼠标位置显示面板 (鼠标始终在中间) - /// - private static void ShowAppAndFollowMouse() - { - //获取鼠标位置 - System.Windows.Point p = MouseUtil.GetMousePosition(); - double left = SystemParameters.VirtualScreenLeft; - double top = SystemParameters.VirtualScreenTop; - double width = SystemParameters.VirtualScreenWidth; - double height = SystemParameters.VirtualScreenHeight; - double right = width - Math.Abs(left); - double bottom = height - Math.Abs(top); - - - if (p.X - mainWindow.Width / 2 < left) - { - //判断是否在最左边缘 - mainWindow.Left = left; - } - else if (p.X + mainWindow.Width / 2 > right) - { - //判断是否在最右边缘 - mainWindow.Left = right - mainWindow.Width; - } - else - { - mainWindow.Left = p.X - mainWindow.Width / 2; - } - - - if (p.Y - mainWindow.Height / 2 < top) - { - //判断是否在最上边缘 - mainWindow.Top = top; - } - else if (p.Y + mainWindow.Height / 2 > bottom) - { - //判断是否在最下边缘 - mainWindow.Top = bottom - mainWindow.Height; - } - else - { - mainWindow.Top = p.Y - mainWindow.Height / 2; - } - - mainWindow.Visibility = Visibility.Visible; - } + /// diff --git a/Util/CommonCode.cs b/Util/CommonCode.cs index c2e8d5e..fe7c88a 100644 --- a/Util/CommonCode.cs +++ b/Util/CommonCode.cs @@ -1,7 +1,9 @@ using GeekDesk.Constant; using GeekDesk.ViewModel; +using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; +using System.Windows; /// /// 提取一些代码 @@ -50,8 +52,7 @@ namespace GeekDesk.Util } } - - + } diff --git a/Util/ShowWindowFollowMouse.cs b/Util/ShowWindowFollowMouse.cs new file mode 100644 index 0000000..5432dfe --- /dev/null +++ b/Util/ShowWindowFollowMouse.cs @@ -0,0 +1,107 @@ +using System; +using System.Windows; + +namespace GeekDesk.Util +{ + public class ShowWindowFollowMouse + { + + public enum MousePosition + { + CENTER = 1, + LEFT_TOP = 2, + LEFT_BOTTOM = 3, + RIGHT_TOP = 4, + RIGHT_BOTTOM = 5, + LEFT_CENTER = 6, + RIGHT_CENTER = 7 + } + + /// + /// 随鼠标位置显示面板 (鼠标始终在中间) + /// + public static void Show(Window window, MousePosition position, double widthOffset, double heightOffset) + { + //获取鼠标位置 + System.Windows.Point p = MouseUtil.GetMousePosition(); + double left = SystemParameters.VirtualScreenLeft; + double top = SystemParameters.VirtualScreenTop; + double width = SystemParameters.VirtualScreenWidth; + double height = SystemParameters.VirtualScreenHeight; + double right = width - Math.Abs(left); + double bottom = height - Math.Abs(top); + + double afterWidth; + double afterHeight; + + switch (position) + { + + case MousePosition.LEFT_BOTTOM: + afterWidth = 0; + afterHeight = window.Height; + break; + case MousePosition.LEFT_TOP: + afterWidth = 0; + afterHeight = 0; + break; + case MousePosition.LEFT_CENTER: + afterWidth = 0; + afterHeight = window.Height / 2; + break; + case MousePosition.RIGHT_BOTTOM: + afterWidth = window.Width; + afterHeight = window.Height; + break; + case MousePosition.RIGHT_TOP: + afterWidth = window.Width; + afterHeight = 0; + break; + case MousePosition.RIGHT_CENTER: + afterWidth = window.Width; + afterHeight = window.Height / 2; + break; + default: + afterWidth = window.Width / 2; + afterHeight = window.Height / 2; + break; + } + + afterWidth += widthOffset; + afterHeight -= heightOffset; + + if (p.X - afterWidth < left) + { + //判断是否在最左边缘 + window.Left = left; + } + else if (p.X + afterWidth > right) + { + //判断是否在最右边缘 + window.Left = right - window.Width; + } + else + { + window.Left = p.X - afterWidth; + } + + + if (p.Y - afterHeight < top) + { + //判断是否在最上边缘 + window.Top = top; + } + else if (p.Y + afterHeight > bottom) + { + //判断是否在最下边缘 + window.Top = bottom - window.Height; + } + else + { + window.Top = p.Y - afterHeight; + } + window.Visibility = Visibility.Visible; + } + + } +}