Skip to content

Instantly share code, notes, and snippets.

@NathanielArnoldR2
Created September 11, 2021 23:18
Show Gist options
  • Save NathanielArnoldR2/775613aab37620a5745b9fc1da1988e6 to your computer and use it in GitHub Desktop.
Save NathanielArnoldR2/775613aab37620a5745b9fc1da1988e6 to your computer and use it in GitHub Desktop.
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