Borderless1942/Win32Extensions.cs

99 lines
2.9 KiB
C#
Raw Permalink Normal View History

2022-01-18 18:25:47 +10:30
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
2022-01-19 15:02:25 +10:30
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;
using Windows.Win32.UI.WindowsAndMessaging;
2022-01-18 18:25:47 +10:30
namespace Borderless1942;
public static class Win32Extensions
{
public static Monitor GetPrimaryMonitor()
{
nint handle = PInvoke.MonitorFromPoint(new POINT
{
x = 0,
y = 0
}, 0);
return new(handle);
}
public static async ValueTask<Window> WaitForMainWindowAsync(this Process process)
2022-01-18 18:25:47 +10:30
{
while (process.MainWindowHandle == IntPtr.Zero)
{
await Task.Delay(100);
}
return process.GetMainWindow();
}
public unsafe static Window GetMainWindow(this Process process)
{
2022-01-19 15:02:25 +10:30
return new((nint)process.MainWindowHandle);
2022-01-18 18:25:47 +10:30
}
}
2022-01-19 15:02:25 +10:30
public readonly record struct Window(nint Handle)
2022-01-18 18:25:47 +10:30
{
2022-01-19 15:02:25 +10:30
private HWND Win32Handle => new(Handle);
2022-01-18 18:25:47 +10:30
public Monitor GetCurrentMonitor()
{
2022-01-19 15:02:25 +10:30
nint handle = PInvoke.MonitorFromWindow(Win32Handle, 0);
2022-01-18 18:25:47 +10:30
return new(handle);
}
public void SetPosition(int x, int y)
{
2022-01-19 15:02:25 +10:30
var flags = SET_WINDOW_POS_FLAGS.SWP_NOSIZE | SET_WINDOW_POS_FLAGS.SWP_NOZORDER | SET_WINDOW_POS_FLAGS.SWP_NOACTIVATE |
SET_WINDOW_POS_FLAGS.SWP_NOOWNERZORDER | SET_WINDOW_POS_FLAGS.SWP_NOSENDCHANGING | SET_WINDOW_POS_FLAGS.SWP_FRAMECHANGED;
PInvoke.SetWindowPos(Win32Handle, PInvoke.HWND_TOPMOST, x, y, 0, 0, flags);
PInvoke.SendMessage(Win32Handle, PInvoke.WM_EXITSIZEMOVE, default, default);
2022-01-18 18:25:47 +10:30
}
public Rectangle GetBounds()
{
var windowInfo = new WINDOWINFO();
2022-01-19 15:02:25 +10:30
PInvoke.GetWindowInfo(Win32Handle, ref windowInfo);
2022-01-18 18:25:47 +10:30
return Rectangle.From(windowInfo.rcWindow);
}
public void RemoveBorders()
{
2022-01-19 15:02:25 +10:30
var style = PInvoke.GetWindowLong(Win32Handle, WINDOW_LONG_PTR_INDEX.GWL_STYLE);
style &= ~(int)(WINDOW_STYLE.WS_THICKFRAME | WINDOW_STYLE.WS_DLGFRAME | WINDOW_STYLE.WS_BORDER);
_ = PInvoke.SetWindowLong(Win32Handle, WINDOW_LONG_PTR_INDEX.GWL_STYLE, style);
2022-01-18 18:25:47 +10:30
2022-01-19 15:02:25 +10:30
style = PInvoke.GetWindowLong(Win32Handle, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE);
style &= ~(int)(WINDOW_EX_STYLE.WS_EX_DLGMODALFRAME | WINDOW_EX_STYLE.WS_EX_WINDOWEDGE | WINDOW_EX_STYLE.WS_EX_CLIENTEDGE | WINDOW_EX_STYLE.WS_EX_STATICEDGE);
_ = PInvoke.SetWindowLong(Win32Handle, WINDOW_LONG_PTR_INDEX.GWL_EXSTYLE, style);
PInvoke.SendMessage(Win32Handle, PInvoke.WM_EXITSIZEMOVE, default, default);
2022-01-18 18:25:47 +10:30
}
}
2022-01-19 15:02:25 +10:30
public readonly record struct Monitor(nint Handle)
2022-01-18 18:25:47 +10:30
{
2022-01-19 15:02:25 +10:30
private HMONITOR Win32Handle => new(Handle);
2022-01-18 18:25:47 +10:30
public unsafe Rectangle GetBounds()
{
var monitorInfo = new MONITORINFO
{
cbSize = (uint)sizeof(MONITORINFO)
};
2022-01-19 15:02:25 +10:30
PInvoke.GetMonitorInfo(Win32Handle, ref monitorInfo);
2022-01-18 18:25:47 +10:30
return Rectangle.From(monitorInfo.rcMonitor);
}
}
[StructLayout(LayoutKind.Sequential)]
public readonly record struct Rectangle(int Left, int Top, int Right, int Bottom)
{
public int Width => Right - Left;
public int Height => Bottom - Top;
2022-01-19 15:02:25 +10:30
internal static Rectangle From(RECT rectangle) => Unsafe.As<RECT, Rectangle>(ref rectangle);
2022-01-18 18:25:47 +10:30
}