From a0ee6a14c5627e0c4a5679c8fb9236925f12e717 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Thu, 28 Nov 2024 12:37:40 +0100 Subject: [PATCH] + BytecodeApi.Win32.Desktop new features --- BytecodeApi.Win32/Desktop.cs | 65 ++++++++++++++++++++++++++++-------- 1 file changed, 52 insertions(+), 13 deletions(-) diff --git a/BytecodeApi.Win32/Desktop.cs b/BytecodeApi.Win32/Desktop.cs index e9f8607..6d89018 100644 --- a/BytecodeApi.Win32/Desktop.cs +++ b/BytecodeApi.Win32/Desktop.cs @@ -1,7 +1,4 @@ -using System.Drawing; -using System.Drawing.Imaging; -using System.Media; -using System.Numerics; +using System.Media; using System.Runtime.InteropServices; using System.Windows; @@ -15,7 +12,7 @@ public static class Desktop /// /// Gets the screen DPI. A value of 96 corresponds to 100% font scaling. /// - public static Vector2 Dpi + public static Point Dpi { get { @@ -25,6 +22,46 @@ public static Vector2 Dpi } } /// + /// Gets the current mouse position in screen coordinates. + /// + public static Point MousePosition + { + get + { + Point dpi = Dpi; + + return new( + System.Windows.Forms.Control.MousePosition.X * 96.0 / dpi.X, + System.Windows.Forms.Control.MousePosition.Y * 96.0 / dpi.Y); + } + } + /// + /// Gets a value indicating whether the left mouse button is pressed. + /// + public static bool IsLeftMouseButtonPressed => Native.GetAsyncKeyState(1) != 0; + /// + /// Gets a value indicating whether the right mouse button is pressed. + /// + public static bool IsRightMouseButtonPressed => Native.GetAsyncKeyState(2) != 0; + /// + /// Gets a [] that represent the bounds of all screens. + /// + public static Rect[] Screens + { + get + { + Point dpi = Dpi; + + return System.Windows.Forms.Screen.AllScreens + .Select(screen => new Rect( + screen.Bounds.X * 96.0 / dpi.X, + screen.Bounds.Y * 96.0 / dpi.Y, + screen.Bounds.Width * 96.0 / dpi.X, + screen.Bounds.Height * 96.0 / dpi.Y)) + .ToArray(); + } + } + /// /// Gets a value indicating whether the workstation is locked. /// public static bool IsWorkstationLocked @@ -83,16 +120,16 @@ public static void Beep(bool success) /// /// A with the image of the captured screen. /// - public static Bitmap CaptureScreen(bool allScreens) + public static System.Drawing.Bitmap CaptureScreen(bool allScreens) { - Vector2 dpi = Dpi / 96f; - int left = allScreens ? (int)(SystemParameters.VirtualScreenLeft * dpi.X) : 0; - int top = allScreens ? (int)(SystemParameters.VirtualScreenTop * dpi.Y) : 0; - int width = (int)((allScreens ? SystemParameters.VirtualScreenWidth : SystemParameters.PrimaryScreenWidth) * dpi.X); - int height = (int)((allScreens ? SystemParameters.VirtualScreenHeight : SystemParameters.PrimaryScreenHeight) * dpi.Y); + Point dpi = Dpi; + int left = allScreens ? (int)(SystemParameters.VirtualScreenLeft * dpi.X / 96.0) : 0; + int top = allScreens ? (int)(SystemParameters.VirtualScreenTop * dpi.Y / 96.0) : 0; + int width = (int)((allScreens ? SystemParameters.VirtualScreenWidth : SystemParameters.PrimaryScreenWidth) * dpi.X / 96.0); + int height = (int)((allScreens ? SystemParameters.VirtualScreenHeight : SystemParameters.PrimaryScreenHeight) * dpi.Y / 96.0); - Bitmap bitmap = new(width, height, PixelFormat.Format32bppArgb); - using Graphics graphics = Graphics.FromImage(bitmap); + System.Drawing.Bitmap bitmap = new(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); + using System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(bitmap); graphics.CopyFromScreen(left, top, 0, 0, bitmap.Size); return bitmap; @@ -149,4 +186,6 @@ file static class Native public static extern nint SendMessage(nint handle, uint msg, int wParam, int lParam); [DllImport("gdi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] public static extern int GetDeviceCaps(nint dc, int index); + [DllImport("user32.dll")] + public static extern short GetAsyncKeyState(int vKey); } \ No newline at end of file