Skip to content

Instantly share code, notes, and snippets.

@NathanielArnoldR2
Created September 11, 2021 23:18
Add-Type -TypeDefinition @"
using System;
using System.Runtime.InteropServices;
namespace ConsoleHostInteraction {
public static class WindowTricks {
private struct RECT {
public int left;
public int top;
public int right;
public int bottom;
}
[DllImport("user32.dll")]
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
[DllImport("user32.dll")]
private static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
private static extern int GetSystemMetrics(int smIndex);
public static void MoveWindowToCenter (IntPtr hWnd) {
RECT Rect = new RECT();
GetWindowRect(hWnd, ref Rect);
int screenWidth = GetSystemMetrics(0);
int screenHeight = GetSystemMetrics(1);
int consoleWidth = Rect.right - Rect.left;
int consoleHeight = Rect.bottom - Rect.top;
MoveWindow(hWnd, (screenWidth / 2) - (consoleWidth / 2), (screenHeight / 2) - (consoleHeight / 2), consoleWidth, consoleHeight, true);
}
}
}
"@
[ConsoleHostInteraction.WindowTricks]::MoveWindowToCenter((Get-Process -Id $PID).MainWindowHandle)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment