I tried Maximizing the Console .NET application using C# commands, but it doesn't work.
EDIT: I feel the need to specify that I use "Console App (.NET Framework)" and not "(Console App (NET Core))" .NET Version is: 4.7.2 Windows version is: Windows 11 (With the new Terminal as my CMD)
This is what I tried: Example1:
using System;
using System.Runtime.InteropServices;
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int MAXIMIZE = 3;
private const int MINIMIZE = 6;
static void Main(string[] args)
{
ShowWindow(GetConsoleWindow(), MAXIMIZE);
Console.ReadKey();
}
With this first example, if you type "MINIMIZE" instead of "MAXIMIZE", It does minimize, but it doesn't MAXIMIZE...
Example2:
[DllImport("user32.dll")]
public static extern bool ShowWindow(System.IntPtr hWnd, int cmdShow);
private static void Maximize()
{
Process p = Process.GetCurrentProcess();
ShowWindow(p.MainWindowHandle, 3); //SW_MAXIMIZE = 3
}
static void Main(string[] args)
{
Maximize();
Console.ReadKey();
}
Your code runs fine in Windows 10, but if you select Windows Terminal in Win11, your code will not work.
If you are using Win11:
Please open Settings=>Privacy&security=>For developers=>Terminal=>Change to Windows Console Host
If you don't find For developers in Privacy&security, you can also search For developers directly in the settings.
At this point, you can maximize the console program by using the code you provided again.
using System;
using System.Runtime.InteropServices;
[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private const int MAXIMIZE = 3;
private const int MINIMIZE = 6;
static void Main(string[] args)
{
ShowWindow(GetConsoleWindow(), MAXIMIZE);
Console.ReadKey();
}
Update:
If you don't want to manually adjust the Windows Terminal to Windows Console Host, you can use the following code directly:
internal class Program
{
[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);
private const int SW_RESTORE = 9; // Use SW_RESTORE to restore the window if it's minimized
private static void Main(string[] args)
{
// Import the necessary functions from user32.dll
// Constants for the ShowWindow function
const int SW_MAXIMIZE = 3;
// Get the handle of the console window
IntPtr consoleWindowHandle = GetForegroundWindow();
// Restore the window if it's minimized
ShowWindow(consoleWindowHandle, SW_RESTORE);
// 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
var width = screenRect.Right - screenRect.Left;
var height = screenRect.Bottom - screenRect.Top;
MoveWindow(consoleWindowHandle, (int)screenRect.Left, (int)screenRect.Top, (int)width, (int)height, true);
Console.ReadKey();
}
}
// Define the Rect structure
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}