Преглед изворни кода

:bug: 高分屏下的鼠标位置

BookerLiu пре 1 година
родитељ
комит
39251b66f6
3 измењених фајлова са 169 додато и 99 уклоњено
  1. 1 1
      Constant/Constants.cs
  2. 26 31
      Control/Windows/PixelColorPickerWindow.xaml.cs
  3. 142 67
      Util/ShowWindowFollowMouse.cs

+ 1 - 1
Constant/Constants.cs

@@ -9,7 +9,7 @@ namespace GeekDesk.Constant
         public static string APP_DIR = AppDomain.CurrentDomain.BaseDirectory.Trim();
 
         // 是否为开发模式
-        public static bool DEV = true;
+        public static bool DEV = false;
 
         public static string MY_NAME = DEV ? "GeekDesk-D" : "GeekDesk";
 

+ 26 - 31
Control/Windows/PixelColorPickerWindow.xaml.cs

@@ -11,6 +11,7 @@ using System.Windows.Input;
 using System.Windows.Interop;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
+using static System.Windows.Forms.VisualStyles.VisualStyleElement;
 using Cursors = System.Windows.Input.Cursors;
 using KeyEventArgs = System.Windows.Input.KeyEventArgs;
 using MouseEventArgs = System.Windows.Input.MouseEventArgs;
@@ -22,7 +23,7 @@ namespace GeekDesk.Control.Windows
     /// </summary>
     public partial class PixelColorPickerWindow : IWindowCommon
     {
-        private static int PIXEL_REC_LENGTH = 20;
+        private static int PIXEL_REC_LENGTH = 10;
 
         private static readonly int MIN_LENGTH = 10;
         private static readonly int MAX_LENGTH = 50;
@@ -73,11 +74,7 @@ namespace GeekDesk.Control.Windows
 
             //获取缩放比例
             double scale = ScreenUtil.GetScreenScalingFactor();
-            //如果主显示器是最左边和最上边,则显示主显示器的缩放比例,反之则缩放比例不添加缩放比例
-            if (Screen.PrimaryScreen.Bounds.X != x || Screen.PrimaryScreen.Bounds.Y != y)
-            {
-                scale = 1;
-            }
+        
 
             this.Width = allWidth;
             this.Height = allHeight;
@@ -173,23 +170,25 @@ namespace GeekDesk.Control.Windows
         [System.Runtime.InteropServices.DllImport("gdi32.dll")]
         public static extern bool DeleteObject(IntPtr onj);
 
+
+        // Constants for DPI
+        private const int HORZRES = 8;
+        private const int VERTRES = 10;
+        private const int LOGPIXELSX = 88;
+        private const int LOGPIXELSY = 90;
+        private static float GetDpi(bool isX)
+        {
+            IntPtr hdc = WindowUtil.GetDC(IntPtr.Zero);
+            int dpi = isX ? WindowUtil.GetDeviceCaps(hdc, LOGPIXELSX) : WindowUtil.GetDeviceCaps(hdc, LOGPIXELSY);
+            WindowUtil.ReleaseDC(IntPtr.Zero, hdc);
+            return dpi / 96f;
+        }
+
         private void SetPixelAbout(MouseEventArgs e)
         {
             VisualBrush b = (VisualBrush)PixelBG.Fill;
 
-            Point pos;
-            //if (e == null)
-            //{
-            //    pos = MouseUtil.GetMousePosition();
-            //}
-            //else
-            //{
-            //    pos = e.MouseDevice.GetPosition(DesktopBG);
-            //}
-
-            
-
-            pos = Mouse.GetPosition(this);
+            Point pos = Mouse.GetPosition(this);
 
 
             Rect viewBox = b.Viewbox;
@@ -203,30 +202,26 @@ namespace GeekDesk.Control.Windows
 
             double x = pos.X + 10;
             double y = pos.Y + 10;
-            if (x + ColorCanvas.Width > SystemParameters.VirtualScreenWidth)
+
+
+
+            //获取缩放比例
+            double scale = ScreenUtil.GetScreenScalingFactor();
+            if (x + ColorCanvas.Width > this.Width / scale)
             {
                 x = pos.X - ColorCanvas.Width - 10;
             }
-            if (y + ColorCanvas.Height > SystemParameters.VirtualScreenHeight)
+            if (y + ColorCanvas.Height > this.Height / scale)
             {
                 y = pos.Y - ColorCanvas.Height - 10;
             }
 
+
             Canvas.SetLeft(ColorCanvas, x);
             Canvas.SetTop(ColorCanvas, y);
 
 
-            //Color wColor = GetColorAtPosition(pos);
-
-            //获取缩放比例
-            double scale = ScreenUtil.GetScreenScalingFactor();
-            //如果主显示器是最左边和最上边,则显示主显示器的缩放比例,反之则缩放比例不添加缩放比例
-            if (Screen.PrimaryScreen.Bounds.X != x || Screen.PrimaryScreen.Bounds.Y != y)
-            {
-                scale = 1;
-            }
 
-            //Console.WriteLine(DesktopBG.Width + "=" + DesktopBG.Height + "=" + pos.X + "=" + pos.Y);
 
             Color wColor = GetColorAtPosition(pos);
 

+ 142 - 67
Util/ShowWindowFollowMouse.cs

@@ -4,6 +4,8 @@ using System.Drawing;
 using System.Runtime.InteropServices;
 using System.Windows;
 using System.Windows.Forms;
+using System.Windows.Input;
+using static GeekDesk.Util.ShowWindowFollowMouse;
 
 namespace GeekDesk.Util
 {
@@ -102,94 +104,167 @@ namespace GeekDesk.Util
        
 
         /// <summary>
-        /// 随鼠标位置显示面板 
+        /// 随鼠标位置显示面板  后面三个参数暂时没有使用
         /// </summary>
         public static void Show(Window window, MousePosition position, double widthOffset = 0, double heightOffset = 0)
         {
             //获取鼠标位置
-            System.Windows.Point p = MouseUtil.GetMousePosition();
+            System.Drawing.Point p = System.Windows.Forms.Cursor.Position;
 
+            // 获取鼠标所在的屏幕
+            Screen currentScreen = Screen.FromPoint(p);
             float dpiX = GetDpi(true);
             float dpiY = GetDpi(false);
-            p.X = p.X / dpiX;
-            p.Y = p.Y / dpiY;
 
-            double left = SystemParameters.VirtualScreenLeft;
-            double top = SystemParameters.VirtualScreenTop;
-            double width = SystemParameters.VirtualScreenWidth;
-            double height = SystemParameters.WorkArea.Height;
-            double right = width - Math.Abs(left);
-            double bottom = height - Math.Abs(top);
+            p.X = (int)(p.X / dpiX);
+            p.Y = (int)(p.Y / dpiY);
 
-            double afterWidth;
-            double afterHeight;
+            //工作区宽度
+            double screenWidth = currentScreen.WorkingArea.Width / dpiX;
+            //工作区高度
+            double screenHeight = currentScreen.WorkingArea.Height / dpiY;
+            double screenX = currentScreen.WorkingArea.X / dpiX;
+            double screenY = currentScreen.WorkingArea.Y / dpiY;
 
-            switch (position)
+            //判断是否超出最左边缘
+            if (p.X - window.Width / 2 < screenX)
             {
-
-                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 - Constants.SHADOW_WIDTH;
-            }
-            else if (p.X + afterWidth > right)
+                //超出最左边缘
+                window.Left = screenX - Constants.SHADOW_WIDTH;
+            } else
             {
-                //判断是否在最右边缘
-                window.Left = right - window.Width + Constants.SHADOW_WIDTH;
+                window.Left = p.X - window.Width / 2 + Constants.SHADOW_WIDTH;
             }
-            else
+
+            //判断是否超出最右边缘
+            if (p.X + window.Width / 2 > screenWidth + screenX)
             {
-                window.Left = p.X - afterWidth;
+                //超出最右边缘
+                window.Left = screenWidth + screenX - window.Width + Constants.SHADOW_WIDTH;
             }
+            
 
-
-            if (p.Y - afterHeight < top)
+            //判断是否超出最上边缘
+            if (p.Y - window.Height / 2 < screenY)
             {
-                //判断是否在最上边缘
-                window.Top = top - Constants.SHADOW_WIDTH;
-            }
-            else if (p.Y + afterHeight > bottom)
+                //超出最上边缘
+                window.Top = screenY - Constants.SHADOW_WIDTH;
+            } else
             {
-                //判断是否在最下边缘
-                window.Top = bottom - window.Height + Constants.SHADOW_WIDTH;
+                window.Top = p.Y - window.Height / 2 + Constants.SHADOW_WIDTH;
             }
-            else
+
+            //判断是否超出最下边缘
+            if (p.Y + window.Height / 2 > screenHeight + screenY)
             {
-                window.Top = p.Y - afterHeight;
+                //超出最下边缘
+                window.Top = screenHeight + screenY - window.Height + Constants.SHADOW_WIDTH;
             }
+            
+
+
+
+
+
+            //// 显示屏幕信息
+            //Console.WriteLine("鼠标当前所在屏幕的信息:");
+            //Console.WriteLine($"屏幕设备名称: {currentScreen.DeviceName}");
+            //Console.WriteLine($"屏幕分辨率: {currentScreen.Bounds.Width}x{currentScreen.Bounds.Height}");
+            //Console.WriteLine($"屏幕工作区: {currentScreen.WorkingArea}");
+            //Console.WriteLine($"主屏幕: {currentScreen.Primary}");
+
+            //Console.WriteLine(p.X + "=" + p.Y);
+            //float dpiX = GetDpi(true);
+            //float dpiY = GetDpi(false);
+            //p.X = (int)(p.X / dpiX);
+            //p.Y = (int)(p.Y / dpiY);
+            //Console.WriteLine(p.X + "=" + p.Y);
+            //double left = SystemParameters.VirtualScreenLeft;
+            //double top = SystemParameters.VirtualScreenTop;
+            //double width = SystemParameters.VirtualScreenWidth;
+            //double height = SystemParameters.WorkArea.Bottom;
+
+            //Console.WriteLine("VirtualScreenTop:" + SystemParameters.VirtualScreenTop);
+            //Console.WriteLine("VirtualScreenLeft:" + SystemParameters.VirtualScreenLeft);
+            //Console.WriteLine("VirtualScreenWidth:" + SystemParameters.VirtualScreenWidth);
+            //Console.WriteLine("VirtualScreenHeight:" + SystemParameters.VirtualScreenHeight);
+            //Console.WriteLine("WorkArea.Bottom:" + SystemParameters.WorkArea.Bottom);
+            //Console.WriteLine(" window.Height:" + window.Height);
+            //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 - Constants.SHADOW_WIDTH;
+            //}
+            //else if (p.X + afterWidth > right)
+            //{
+            //    //判断是否在最右边缘
+            //    window.Left = right - window.Width + Constants.SHADOW_WIDTH;
+            //}
+            //else
+            //{
+            //    window.Left = p.X - afterWidth;
+            //}
+
+
+            //if (p.Y - afterHeight < top)
+            //{
+            //    //判断是否在最上边缘
+            //    window.Top = top - Constants.SHADOW_WIDTH;
+            //}
+            //else if (p.Y + afterHeight > bottom)
+            //{
+            //    Console.WriteLine("p.Y:" + p.Y);
+            //    Console.WriteLine("afterHeight:" + afterHeight);
+            //    Console.WriteLine("bottom:" + bottom);
+            //    //判断是否在最下边缘
+            //    window.Top = bottom - window.Height + Constants.SHADOW_WIDTH;
+            //}
+            //else
+            //{
+            //    window.Top = p.Y - afterHeight;
+            //}
         }
 
     }