Код: Выделить всё
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("kernel32.dll", ExactSpelling = true)]
static extern IntPtr GetConsoleWindow();
const int SW_RESTORE = 9; // Restore window if minimized or not in normal state
const int SW_MAXIMIZE = 3; // Command to maximize the window
class Program
{
static void Main(string[] args)
{
ConsoleManipulator consoleManipulator = new ConsoleManipulator();
// Bring the console window to the front
consoleManipulator.BringConsoleToFront();
// Maximize the console window
consoleManipulator.MaximizeConsole();
Console.ReadLine(); // Keep the console open to observe changes
}
}
// Method to bring the console window to the front
public void BringConsoleToFront()
{
IntPtr consoleWindow = GetConsoleWindow();
if (consoleWindow != IntPtr.Zero)
{
// First, restore the window if it’s minimized or not normal
ShowWindow(consoleWindow, SW_RESTORE);
Console.WriteLine("Console restored.");
// Bring to front
SetForegroundWindow(consoleWindow);
Console.WriteLine("Console brought to the front.");
}
}
// Method to maximize the console window
public void MaximizeConsole()
{
IntPtr consoleWindow = GetConsoleWindow();
if (consoleWindow != IntPtr.Zero)
{
ShowWindow(consoleWindow, SW_MAXIMIZE); // Maximize the window
Console.WriteLine("Console maximized.");
}
else
{
Console.WriteLine("Failed to get console window handle.");
}
}
Когда я запускаю программу с показанным кодом, consoleManipulator. BringConsoleToFront() переносит консоль в начало моей Visual Studio, но MaximizeConsole() не может развернуть консоль. Консольное приложение остается без изменений с фактическими размерами высоты и ширины...
Подробнее здесь: https://stackoverflow.com/questions/790 ... e-maximize