22

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?

DOK
  • 32,337
  • 7
  • 60
  • 92
user26830
  • 1,059
  • 4
  • 16
  • 25
  • _What error do you get_? Did you read it? – SLaks Feb 26 '14 at 21:01
  • 1
    http://msdn.microsoft.com/en-us/library/windows/desktop/ms683193(v=vs.85).aspx – SLaks Feb 26 '14 at 21:03
  • read this and look to SLaks comments: http://stackoverflow.com/questions/7670633/maximum-columns-for-a-console-in-c-sharp – Bassam Alugili Feb 26 '14 at 21:06
  • 1
    @SLaks I don't have the exact error right here but it was something about the dimensions of the screens being smaller or larger then the current format or something like that. – user26830 Feb 26 '14 at 21:06
  • @BassamAlugili thanks for the link, but this is only for dimensions and not maximizing it (like clicking on the maximize button on the right top). – user26830 Feb 26 '14 at 21:08

4 Answers4

37

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
}
John Wu
  • 50,556
  • 8
  • 44
  • 80
  • 1
    Wow, I'm having [Dan Appleman flashbacks](http://www.amazon.com/Applemans-Visual-Basic-Programmers-Guide/dp/0672315904). Well done; thanks. – ruffin Nov 19 '14 at 22:54
  • I had to re-enable the VB.NET prettiness, but then I got it to work, so thanks! :) – mbm29414 Apr 20 '16 at 14:33
19
    [DllImport("kernel32.dll", ExactSpelling = true)]

    private static extern IntPtr GetConsoleWindow();
    private static IntPtr ThisConsole = GetConsoleWindow();

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

    private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
    private const int HIDE = 0;
    private const int MAXIMIZE = 3;
    private const int MINIMIZE = 6;
    private const int RESTORE = 9;


    static void Main(string[] args)
    {
       ShowWindow(ThisConsole, MINIMIZE);
    }
Châu Nguyễn
  • 269
  • 3
  • 5
  • 3
    Please provide some explanation why does code solve the issue. Code only answers are discouraged. – zero323 Jan 02 '16 at 12:53
  • 1
    This answer works even when you start your console application without debugger, the accepted answer doesnt. Another options is `FindWindowByCaption` in user32.dll – nawfal Jun 29 '16 at 17:21
  • 3
    I suppose it should be `ShowWindow(ThisConsole, MAXIMIZE);` – Mojtaba Nov 01 '19 at 12:14
1

This works for a .NET 6 Console application:

using System.Runtime.InteropServices;

[DllImport("kernel32.dll", ExactSpelling = true)]

static extern IntPtr GetConsoleWindow();
IntPtr ThisConsole = GetConsoleWindow();

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]

static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
const int HIDE = 0;
const int MAXIMIZE = 3;
const int MINIMIZE = 6;
const int RESTORE = 9;

ShowWindow(ThisConsole, MAXIMIZE);
Jan
  • 2,165
  • 1
  • 18
  • 13
1
Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight)
Mahesh Waghmare
  • 726
  • 9
  • 27