From cbbd09b8a500e7a7c14575c51f45acaf290958a0 Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Wed, 23 Jul 2025 13:41:01 +0800 Subject: [PATCH] Add cursor info class --- Flow.Launcher.Infrastructure/CursorInfo.cs | 26 +++++++++++++++++++++ Flow.Launcher.Infrastructure/MonitorInfo.cs | 6 +---- 2 files changed, 27 insertions(+), 5 deletions(-) create mode 100644 Flow.Launcher.Infrastructure/CursorInfo.cs diff --git a/Flow.Launcher.Infrastructure/CursorInfo.cs b/Flow.Launcher.Infrastructure/CursorInfo.cs new file mode 100644 index 000000000..046dc4ba9 --- /dev/null +++ b/Flow.Launcher.Infrastructure/CursorInfo.cs @@ -0,0 +1,26 @@ +using System.Drawing; +using System.Runtime.InteropServices; +using Windows.Win32; + +namespace Flow.Launcher.Infrastructure; + +/// +/// Contains full information about a cursor. +/// +/// +/// Use this class to replace the System.Windows.Forms.Cursor class which can cause possible System.PlatformNotSupportedException. +/// +public class CursorInfo +{ + public static Point Position + { + get + { + if (!PInvoke.GetCursorPos(out var pt)) + { + Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error()); + } + return pt; + } + } +} diff --git a/Flow.Launcher.Infrastructure/MonitorInfo.cs b/Flow.Launcher.Infrastructure/MonitorInfo.cs index 9a62fc0f4..a87aab7d8 100644 --- a/Flow.Launcher.Infrastructure/MonitorInfo.cs +++ b/Flow.Launcher.Infrastructure/MonitorInfo.cs @@ -102,11 +102,7 @@ public class MonitorInfo /// The display monitor that contains the cursor, or null if no monitor is found. public static unsafe MonitorInfo GetCursorDisplayMonitor() { - if (!PInvoke.GetCursorPos(out var pt)) - { - Marshal.ThrowExceptionForHR(Marshal.GetLastWin32Error()); - } - var cursorMonitor = PInvoke.MonitorFromPoint(pt, MONITOR_FROM_FLAGS.MONITOR_DEFAULTTONEAREST); + var cursorMonitor = PInvoke.MonitorFromPoint(CursorInfo.Position, MONITOR_FROM_FLAGS.MONITOR_DEFAULTTONEAREST); MonitorInfo cursorMonitorInfo = null; var callback = new MONITORENUMPROC((monitor, deviceContext, rect, data) => {