using System.ComponentModel;
|
using System.Runtime.InteropServices;
|
using System.Text;
|
|
namespace Moli.Desktop.Interop;
|
|
internal static class NativeMethods
|
{
|
internal const int GwlExStyle = -20;
|
internal const int WsExTransparent = 0x00000020;
|
internal const int WsExLayered = 0x00080000;
|
internal const int WsExToolWindow = 0x00000080;
|
internal const int WsExNoActivate = 0x08000000;
|
internal const int LwaAlpha = 0x00000002;
|
internal const uint SwpNoSize = 0x0001;
|
internal const uint SwpNoMove = 0x0002;
|
internal const uint SwpNoZOrder = 0x0004;
|
internal const uint SwpNoActivate = 0x0010;
|
internal const uint SwpShowWindow = 0x0040;
|
internal const uint SmtoNormal = 0x0000;
|
internal static readonly IntPtr HwndBottom = new(1);
|
|
internal delegate bool EnumWindowsProc(IntPtr hwnd, IntPtr lParam);
|
internal delegate bool MonitorEnumProc(IntPtr hMonitor, IntPtr hdcMonitor, ref Rect lprcMonitor, IntPtr dwData);
|
|
[DllImport("user32.dll", EntryPoint = "GetWindowLongPtrW", SetLastError = true)]
|
internal static extern IntPtr GetWindowLongPtr64(IntPtr hWnd, int nIndex);
|
|
[DllImport("user32.dll", EntryPoint = "GetWindowLongW", SetLastError = true)]
|
internal static extern int GetWindowLong32(IntPtr hWnd, int nIndex);
|
|
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtrW", SetLastError = true)]
|
internal static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
|
|
[DllImport("user32.dll", EntryPoint = "SetWindowLongW", SetLastError = true)]
|
internal static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);
|
|
[DllImport("user32.dll", SetLastError = true)]
|
[return: MarshalAs(UnmanagedType.Bool)]
|
internal static extern bool SetLayeredWindowAttributes(IntPtr hwnd, uint crKey, byte bAlpha, uint dwFlags);
|
|
[DllImport("user32.dll", SetLastError = true)]
|
[return: MarshalAs(UnmanagedType.Bool)]
|
internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, uint uFlags);
|
|
[DllImport("user32.dll", SetLastError = true)]
|
internal static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
|
|
[DllImport("user32.dll", EntryPoint = "FindWindowW", CharSet = CharSet.Unicode, SetLastError = true)]
|
internal static extern IntPtr FindWindow(string lpClassName, string? lpWindowName);
|
|
[DllImport("user32.dll", EntryPoint = "FindWindowExW", CharSet = CharSet.Unicode, SetLastError = true)]
|
internal static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string? lpszWindow);
|
|
[DllImport("user32.dll", SetLastError = true)]
|
[return: MarshalAs(UnmanagedType.Bool)]
|
internal static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
|
|
[DllImport("user32.dll", SetLastError = true)]
|
internal static extern IntPtr SendMessageTimeout(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam, uint flags, uint timeout, out IntPtr result);
|
|
[DllImport("user32.dll", SetLastError = true)]
|
[return: MarshalAs(UnmanagedType.Bool)]
|
internal static extern bool EnumDisplayMonitors(IntPtr hdc, IntPtr lprcClip, MonitorEnumProc lpfnEnum, IntPtr dwData);
|
|
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
|
[return: MarshalAs(UnmanagedType.Bool)]
|
internal static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfoEx lpmi);
|
|
internal static IntPtr GetWindowLongPtr(IntPtr hwnd, int index)
|
{
|
return IntPtr.Size == 8
|
? GetWindowLongPtr64(hwnd, index)
|
: new IntPtr(GetWindowLong32(hwnd, index));
|
}
|
|
internal static IntPtr SetWindowLongPtr(IntPtr hwnd, int index, IntPtr value)
|
{
|
return IntPtr.Size == 8
|
? SetWindowLongPtr64(hwnd, index, value)
|
: new IntPtr(SetWindowLong32(hwnd, index, value.ToInt32()));
|
}
|
|
internal static int GetLastError() => Marshal.GetLastWin32Error();
|
|
internal static void ThrowLastWin32Error(string operation)
|
{
|
var error = GetLastError();
|
if (error != 0)
|
{
|
throw new Win32Exception(error, $"{operation} failed.");
|
}
|
}
|
|
[StructLayout(LayoutKind.Sequential)]
|
internal struct Rect
|
{
|
public int Left;
|
public int Top;
|
public int Right;
|
public int Bottom;
|
}
|
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
|
internal struct MonitorInfoEx
|
{
|
public int Size;
|
public Rect Monitor;
|
public Rect WorkArea;
|
[MarshalAs(UnmanagedType.Bool)]
|
public bool Flags;
|
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
|
public string DeviceName;
|
}
|
}
|