增加多文件备份, 尝试修复UI假死状态, 修复高分屏缩放导致的鼠标追随bug

This commit is contained in:
BookerLiu
2024-08-07 11:04:48 +08:00
parent 8fea77c304
commit a61c69aa0b
14 changed files with 504 additions and 85 deletions

View File

@@ -1,6 +1,9 @@
using GeekDesk.Constant;
using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
namespace GeekDesk.Util
{
@@ -18,6 +21,86 @@ namespace GeekDesk.Util
RIGHT_CENTER = 7
}
public static void FollowMouse(Window window)
{
// Get the mouse position
var mousePosition = System.Windows.Forms.Control.MousePosition;
// Get the window size
var windowWidth = window.Width;
var windowHeight = window.Height;
Console.WriteLine("windowWidth + windowHeight:" + windowWidth + "+" + windowHeight);
// Get the screen where the mouse is located
var screen = System.Windows.Forms.Screen.FromPoint(new System.Drawing.Point(mousePosition.X, mousePosition.Y));
var workingArea = screen.WorkingArea;
// Get the DPI scaling factor for the screen
//float dpiX, dpiY;
//using (var graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero))
//{
// dpiX = graphics.DpiX / 96f; // 96 is the default DPI
// dpiY = graphics.DpiY / 96f; // 96 is the default DPI
//}
float dpiX = GetDpi( true);
float dpiY = GetDpi(false);
// Convert mouse position to logical pixels based on DPI
double mouseX = mousePosition.X / dpiX;
double mouseY = mousePosition.Y / dpiY;
// Calculate target position to center the window on the mouse
double targetLeft = mouseX - windowWidth / 2;
double targetTop = mouseY - windowHeight / 2;
// Ensure the window does not exceed the screen boundaries
if (targetLeft < workingArea.Left / dpiX)
targetLeft = workingArea.Left / dpiX;
if (targetLeft + windowWidth / dpiX > workingArea.Right / dpiX)
targetLeft = workingArea.Right / dpiX - windowWidth / dpiX;
if (targetTop < workingArea.Top / dpiY)
targetTop = workingArea.Top / dpiY;
if (targetTop + windowHeight / dpiY > workingArea.Bottom / dpiY)
targetTop = workingArea.Bottom / dpiY - windowHeight / dpiY;
// Update window position
window.Left = targetLeft * dpiX;
window.Top = targetTop * dpiY;
}
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 static IntPtr GetScreenHandleFromMouse()
{
// Get the mouse position
var mousePosition = System.Windows.Forms.Control.MousePosition;
// Convert mouse position to a POINT structure
System.Drawing.Point point = new System.Drawing.Point(mousePosition.X, mousePosition.Y);
// Get the window handle from the point
IntPtr windowHandle = WindowUtil.WindowFromPoint(point);
return windowHandle;
}
// Constants for DPI
private const int HORZRES = 8;
private const int VERTRES = 10;
private const int LOGPIXELSX = 88;
private const int LOGPIXELSY = 90;
/// <summary>
/// 随鼠标位置显示面板
/// </summary>
@@ -25,6 +108,12 @@ namespace GeekDesk.Util
{
//获取鼠标位置
System.Windows.Point p = MouseUtil.GetMousePosition();
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;