Skip to content

Commit

Permalink
+ BytecodeApi.Win32.Desktop new features
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Fischer committed Nov 28, 2024
1 parent 627ab22 commit a0ee6a1
Showing 1 changed file with 52 additions and 13 deletions.
65 changes: 52 additions & 13 deletions BytecodeApi.Win32/Desktop.cs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -15,7 +12,7 @@ public static class Desktop
/// <summary>
/// Gets the screen DPI. A value of 96 corresponds to 100% font scaling.
/// </summary>
public static Vector2 Dpi
public static Point Dpi
{
get
{
Expand All @@ -25,6 +22,46 @@ public static Vector2 Dpi
}
}
/// <summary>
/// Gets the current mouse position in screen coordinates.
/// </summary>
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);
}
}
/// <summary>
/// Gets a <see cref="bool" /> value indicating whether the left mouse button is pressed.
/// </summary>
public static bool IsLeftMouseButtonPressed => Native.GetAsyncKeyState(1) != 0;
/// <summary>
/// Gets a <see cref="bool" /> value indicating whether the right mouse button is pressed.
/// </summary>
public static bool IsRightMouseButtonPressed => Native.GetAsyncKeyState(2) != 0;
/// <summary>
/// Gets a <see cref="Rect" />[] that represent the bounds of all screens.
/// </summary>
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();
}
}
/// <summary>
/// Gets a <see cref="bool" /> value indicating whether the workstation is locked.
/// </summary>
public static bool IsWorkstationLocked
Expand Down Expand Up @@ -83,16 +120,16 @@ public static void Beep(bool success)
/// <returns>
/// A <see cref="Bitmap" /> with the image of the captured screen.
/// </returns>
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;
Expand Down Expand Up @@ -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);
}

0 comments on commit a0ee6a1

Please sign in to comment.