Files
Spectrum/Spectrum/Utilities/Win32.cs
T
2026-01-16 17:13:37 -05:00

84 lines
2.9 KiB
C#

using System.Runtime.InteropServices;
namespace Spectrum.Utilities
{
class Win32
{
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(int nIndex);
[DllImport("user32.dll")]
public static extern short GetAsyncKeyState(int vKey);
private const int SM_CXSCREEN = 0;
private const int SM_CYSCREEN = 1;
private const int SM_XVIRTUALSCREEN = 76;
private const int SM_YVIRTUALSCREEN = 77;
private const int SM_CXVIRTUALSCREEN = 78;
private const int SM_CYVIRTUALSCREEN = 79;
public static bool IsWindowHidden = false;
public static (int Width, int Height) GetPrimaryScreenSize()
{
return (GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
}
public static (int X, int Y, int Width, int Height) GetVirtualScreenBounds()
{
return (
GetSystemMetrics(SM_XVIRTUALSCREEN),
GetSystemMetrics(SM_YVIRTUALSCREEN),
GetSystemMetrics(SM_CXVIRTUALSCREEN),
GetSystemMetrics(SM_CYVIRTUALSCREEN)
);
}
[DllImport("user32.dll")]
private static extern bool SetWindowDisplayAffinity(IntPtr hwnd, uint dwAffinity);
[DllImport("user32.dll")]
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
[DllImport("kernel32.dll")]
private static extern bool AttachConsole(int dwProcessId);
[DllImport("kernel32.dll")]
private static extern bool AllocConsole();
private const uint WDA_NONE = 0x00000000;
private const uint WDA_MONITOR = 0x00000001;
private const uint WDA_EXCLUDEFROMCAPTURE = 0x00000011;
private const uint SWP_NOSIZE = 0x0001;
private const uint SWP_NOZORDER = 0x0004;
private const uint SWP_NOACTIVATE = 0x0010;
private const int ATTACH_PARENT_PROCESS = -1;
public static bool EnableAntiCapture(IntPtr windowHandle)
{
IsWindowHidden = true;
return SetWindowDisplayAffinity(windowHandle, WDA_EXCLUDEFROMCAPTURE);
}
public static bool DisableAntiCapture(IntPtr windowHandle)
{
IsWindowHidden = false;
return SetWindowDisplayAffinity(windowHandle, WDA_NONE);
}
public static void SetWindowPosition(IntPtr windowHandle, int x, int y)
{
SetWindowPos(windowHandle, IntPtr.Zero, x, y, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
}
public static void SetWindowBounds(IntPtr windowHandle, int x, int y, int width, int height)
{
SetWindowPos(windowHandle, IntPtr.Zero, x, y, width, height, SWP_NOZORDER | SWP_NOACTIVATE);
}
public static void EnsureConsoleAttached()
{
if (!AttachConsole(ATTACH_PARENT_PROCESS))
AllocConsole();
}
}
}