private static void CenterWindow() { //get console size IntPtr consoleId = Kernel32.Window.GetForegroundWindow(); Kernel32.Generic.RECT consoleRect = new Kernel32.Generic.RECT(); Kernel32.Window.GetWindowRect(consoleId, ref consoleRect); int consoleWidth = consoleRect.right - consoleRect.left; int consoleHeight = consoleRect.bottom - consoleRect.top; //get screen size IntPtr desktopId = Kernel32.Window.GetDesktopWindow(); Kernel32.Generic.RECT screenRect = new Kernel32.Generic.RECT(); Kernel32.Window.GetWindowRect(desktopId, ref screenRect); int screenWidth = screenRect.right - screenRect.left; int screenHeight = screenRect.bottom - screenRect.top; //move console to center of screen Kernel32.Window.MoveWindow(consoleId, screenWidth / 2 - consoleWidth / 2, screenHeight / 2 - consoleHeight / 2, consoleWidth, consoleHeight, true); }
internal static extern bool GetWindowRect( IntPtr hWnd, ref Generic.RECT rect );