using System.Runtime.InteropServices; namespace SinMaiLauncher.Interops { public class WindowInterops { private static class NativeWindow { [DllImport("user32.dll", SetLastError = true)] public static extern nint SetParent(nint hWndChild, nint hWndNewParent); [DllImport("user32.dll", SetLastError = true)] public static extern bool SetWindowLong(nint hWnd, int nIndex, uint dwNewLong); // 获取窗口样式 [DllImport("user32.dll")] public static extern uint GetWindowLong(IntPtr hWnd, int nIndex); [DllImport("user32.dll", SetLastError = true)] public static extern bool GetWindowRect(nint hWnd, out RECT lpRect); [DllImport("user32.dll")] [return: MarshalAs(UnmanagedType.Bool)] private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags); [DllImport("user32.dll", SetLastError = true)] public static extern bool MoveWindow(nint hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint); public const int GWL_STYLE = -16; public struct RECT { public int Left, Top, Right, Bottom; } public static class WindowStyles { public const uint WS_OVERLAPPED = 0x00000000; public const uint WS_POPUP = 0x80000000; public const uint WS_CHILD = 0x40000000; public const uint WS_MINIMIZE = 0x20000000; public const uint WS_VISIBLE = 0x10000000; public const uint WS_DISABLED = 0x08000000; public const uint WS_CLIPSIBLINGS = 0x04000000; public const uint WS_CLIPCHILDREN = 0x02000000; public const uint WS_MAXIMIZE = 0x01000000; public const uint WS_CAPTION = 0x00C00000; public const uint WS_BORDER = 0x00800000; public const uint WS_DLGFRAME = 0x00400000; public const uint WS_VSCROLL = 0x00200000; public const uint WS_HSCROLL = 0x00100000; public const uint WS_SYSMENU = 0x00080000; public const uint WS_THICKFRAME = 0x00040000; public const uint WS_GROUP = 0x00020000; public const uint WS_TABSTOP = 0x00010000; public const uint WS_MINIMIZEBOX = 0x00020000; public const uint WS_MAXIMIZEBOX = 0x00010000; } } public static void RemoveBorderVisible(IntPtr hWnd) { var oldVal = NativeWindow.GetWindowLong(hWnd, NativeWindow.GWL_STYLE); if (oldVal != NativeWindow.WindowStyles.WS_VISIBLE) NativeWindow.SetWindowLong(hWnd, NativeWindow.GWL_STYLE, NativeWindow.WindowStyles.WS_VISIBLE); } public static void SetParent(IntPtr hWnd, IntPtr hWndContainer) { NativeWindow.SetParent(hWnd, hWndContainer); } public static void FillWindow(IntPtr hWnd, Control container) { var rect = container.ClientRectangle; // 调整控制台窗口大小,使其填满容器 NativeWindow.MoveWindow(hWnd, rect.Left, rect.Top, rect.Width, rect.Height, true); } public static bool RemoveBorderPopup(IntPtr hWnd) { var oldVal = NativeWindow.GetWindowLong(hWnd, NativeWindow.GWL_STYLE); if (oldVal != NativeWindow.WindowStyles.WS_POPUP) return NativeWindow.SetWindowLong(hWnd, NativeWindow.GWL_STYLE, NativeWindow.WindowStyles.WS_POPUP); return true; } public enum MoveWindowResult { Success, NoChange, FailGet, FailSet, } public static MoveWindowResult MoveWindow(IntPtr hWnd, int x, int y, int w, int h) { if (NativeWindow.GetWindowRect(hWnd, out var rc)) { if (x != rc.Left || y != rc.Top || w != (rc.Right - rc.Left) || h != (rc.Bottom - rc.Top)) { return NativeWindow.MoveWindow(hWnd, x, y, w, h, false) ? MoveWindowResult.Success : MoveWindowResult.FailSet ; } return MoveWindowResult.NoChange; } return MoveWindowResult.FailGet; } } }