Hi @Steph ,
This method can maximize the Console window in windows11.
It maximizes the console window and resizes it to fill the entire screen.
You can modify the method you need according to this code.
using System.Runtime.InteropServices;
internal class Program
{
// Structure used by GetWindowRect
struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
private static void Main(string[] args)
{
// Import the necessary functions from user32.dll
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll")]
static extern bool GetWindowRect(IntPtr hWnd, out Rect lpRect);
[DllImport("user32.dll")]
static extern bool MoveWindow(IntPtr hWnd, int x, int y, int nWidth, int nHeight, bool bRepaint);
// Constants for the ShowWindow function
const int SW_MAXIMIZE = 3;
// Get the handle of the console window
IntPtr consoleWindowHandle = GetForegroundWindow();
// Maximize the console window
ShowWindow(consoleWindowHandle, SW_MAXIMIZE);
// Get the screen size
Rect screenRect;
GetWindowRect(consoleWindowHandle, out screenRect);
// Resize and reposition the console window to fill the screen
int width = screenRect.Right - screenRect.Left;
int height = screenRect.Bottom - screenRect.Top;
MoveWindow(consoleWindowHandle, screenRect.Left, screenRect.Top, width, height, true);
Console.ReadKey();
}
}
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.