Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximizing console window - C#

Tags:

I'm working on a console application on C# and I need to open the console maximized. When I just hit the maximize button on the console window, it maximizes only on height and not in width. I tried to use the following code :

   Console.WindowWidth = 150;
   Console.WindowHeight = 61;

Which works almost as I want on my computer but gives error on some other computers. What should I do to maximize the console?

like image 593
user26830 Avatar asked Feb 26 '14 20:02

user26830


1 Answers

Can't with the CLR. Need to import Win32 API calls and poke your container window. The following might help.

using System.Diagnostics;
using System.Runtime.InteropServices;

[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
}
like image 151
John Wu Avatar answered Sep 19 '22 21:09

John Wu