12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- using Vanara.PInvoke;
- using static Vanara.PInvoke.User32;
- namespace SinMaiLauncher.Interops
- {
- public class WindowInterops
- {
- public static void RemoveBorderVisible(HWND hWnd)
- {
- var oldVal = (WindowLongFlags)GetWindowLong(hWnd, WindowLongFlags.GWL_STYLE);
- if (oldVal != WindowLongFlags.GWL_STYLE)
- SetWindowLong(hWnd, WindowLongFlags.GWL_STYLE, (nint)WindowStyles.WS_VISIBLE);
- }
- public static void SetParent(HWND hWnd, IntPtr hWndContainer)
- {
- User32.SetParent(hWnd, hWndContainer);
- }
- public static void FillWindow(HWND hWnd, Control container)
- {
- var rect = container.ClientRectangle;
- // 调整控制台窗口大小,使其填满容器
- MoveWindow(hWnd, rect.Left, rect.Top, rect.Width, rect.Height, true);
- }
- public enum MoveWindowResult
- {
- Success,
- NoChange,
- FailGet,
- FailSet,
- }
- public static MoveWindowResult TryMoveWindow(IntPtr hWnd, int x, int y, int w, int h)
- {
- if (!GetWindowRect(hWnd, out var rc)) return MoveWindowResult.FailGet;
- if (x != rc.Left ||
- y != rc.Top ||
- w != (rc.Right - rc.Left) ||
- h != (rc.Bottom - rc.Top))
- {
- return MoveWindow(hWnd, x, y, w, h, false)
- ? MoveWindowResult.Success
- : MoveWindowResult.FailSet
- ;
- }
- return MoveWindowResult.NoChange;
- }
- public static void Maximize(HWND hWnd) => ShowWindow(hWnd, ShowWindowCommand.SW_MAXIMIZE);
- public static void Normal(HWND hWnd) => ShowWindow(hWnd, ShowWindowCommand.SW_NORMAL);
- }
- }
|