none
Maximizing the Console Window!?

    Question

  • In the console I need to maximize the window, how do I do that?

    I understand you may use a function to send a fake key command......!?




    On another note, is there an easy way to make the console appear in full screen mode?

    Saturday, August 02, 2008 3:19 PM

Answers

  • Here's some sample code:

    using System;
    using System.Runtime.InteropServices;

    namespace ConsoleApplication1 {
      class Program {
        static void Main(string[] args) {
          Console.WriteLine("Press Enter to cycle through the options");
          Console.ReadLine();
          IntPtr hConsole = GetStdHandle(-11);
          CONSOLE_SCREEN_BUFFER_INFO info;
          GetConsoleScreenBufferInfo(hConsole, out info);
          SMALL_RECT rc;
          rc.Left = rc.Top = 0;
          rc.Right = (short)(Math.Min(info.MaximumWindowSize.X, info.Size.X) - 1);
          rc.Bottom = (short)(Math.Min(info.MaximumWindowSize.Y, info.Size.Y) - 1);
          SetConsoleWindowInfo(hConsole, true, ref rc);
          Console.ReadLine();
          SetConsoleDisplayMode(hConsole, 1);
          Console.ReadLine();
          SetConsoleDisplayMode(hConsole, 2);
          Console.WriteLine("Goodbye");
          Console.ReadLine();
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct COORD {
          public short X;
          public short Y;
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct SMALL_RECT {
          public short Left;
          public short Top;
          public short Right;
          public short Bottom;
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct CONSOLE_SCREEN_BUFFER_INFO {
          public COORD Size;
          public COORD CursorPosition;
          public short Attributes;
          public SMALL_RECT Window;
          public COORD MaximumWindowSize;
        }
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetStdHandle(int handle);
        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleDisplayMode(IntPtr hConsole, int mode);
        [DllImport("kernel32.dll")]
        private static extern bool GetConsoleScreenBufferInfo(IntPtr hConsole, out CONSOLE_SCREEN_BUFFER_INFO info);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetConsoleWindowInfo(IntPtr hConsole, bool absolute, ref SMALL_RECT rect);
      }
    }


    Hans Passant.
    Saturday, August 02, 2008 4:36 PM
    Moderator
  • Yup, the true maximize feature is not available through the API.  Once you start fretting about how the console mode window looks, it is time to start taking a look at writing a true Windows program.  Windows Forms makes it pretty easy with an excellent designer.  It lets you create a user interface that 99.9% of all Windows users expect to get from a piece of software they'd consider paying money for.  Best of all, controlling the window size and state is trivial without having to P/Invoke an obscure API function.  Recommended.
    Hans Passant.
    Sunday, August 03, 2008 9:58 PM
    Moderator
  • It's in my code sample, SetConsoleDisplayMode().  It takes hardware support and that support has been eroding.  Vista added a new API to check if the hardware actually allows full screen mode, that's how rare it is getting.  You don't want to go there, it is redrum on LCD panels.
    Hans Passant.
    Sunday, August 03, 2008 10:59 PM
    Moderator

All replies

  • You can call ShellExecute with SW_MAXIMIZE
    Saturday, August 02, 2008 4:25 PM
  • Here's some sample code:

    using System;
    using System.Runtime.InteropServices;

    namespace ConsoleApplication1 {
      class Program {
        static void Main(string[] args) {
          Console.WriteLine("Press Enter to cycle through the options");
          Console.ReadLine();
          IntPtr hConsole = GetStdHandle(-11);
          CONSOLE_SCREEN_BUFFER_INFO info;
          GetConsoleScreenBufferInfo(hConsole, out info);
          SMALL_RECT rc;
          rc.Left = rc.Top = 0;
          rc.Right = (short)(Math.Min(info.MaximumWindowSize.X, info.Size.X) - 1);
          rc.Bottom = (short)(Math.Min(info.MaximumWindowSize.Y, info.Size.Y) - 1);
          SetConsoleWindowInfo(hConsole, true, ref rc);
          Console.ReadLine();
          SetConsoleDisplayMode(hConsole, 1);
          Console.ReadLine();
          SetConsoleDisplayMode(hConsole, 2);
          Console.WriteLine("Goodbye");
          Console.ReadLine();
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct COORD {
          public short X;
          public short Y;
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct SMALL_RECT {
          public short Left;
          public short Top;
          public short Right;
          public short Bottom;
        }
        [StructLayout(LayoutKind.Sequential)]
        private struct CONSOLE_SCREEN_BUFFER_INFO {
          public COORD Size;
          public COORD CursorPosition;
          public short Attributes;
          public SMALL_RECT Window;
          public COORD MaximumWindowSize;
        }
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetStdHandle(int handle);
        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleDisplayMode(IntPtr hConsole, int mode);
        [DllImport("kernel32.dll")]
        private static extern bool GetConsoleScreenBufferInfo(IntPtr hConsole, out CONSOLE_SCREEN_BUFFER_INFO info);
        [DllImport("kernel32.dll", SetLastError = true)]
        private static extern bool SetConsoleWindowInfo(IntPtr hConsole, bool absolute, ref SMALL_RECT rect);
      }
    }


    Hans Passant.
    Saturday, August 02, 2008 4:36 PM
    Moderator
  • Thanks for the info!

    But this code doesn't make sense to me....
    I'm still kinda new to C#...

    I am still trying to find out just how to include that windows.h for "SW_MAXIMIZE" though.
    Sunday, August 03, 2008 1:42 AM
  • You don't include anything in C#. That #include  is for C++. You run the code nobugs posted. It will work.
    AlexB
    Sunday, August 03, 2008 2:31 AM
  • Yeah, I meant to add a reference to it...

    But the only change I see this code do is to makes the hight of the window higher. That's not maximizing as far as I know.

    I'm using vista if that matters anyhow, don't think it does tough.
    Sunday, August 03, 2008 8:47 PM
  • That's the way how console window looks maximized.
    Even if you open console window from "run" dialog and try to maximize it, only the height will change.
    Sunday, August 03, 2008 9:18 PM
  • Yup, the true maximize feature is not available through the API.  Once you start fretting about how the console mode window looks, it is time to start taking a look at writing a true Windows program.  Windows Forms makes it pretty easy with an excellent designer.  It lets you create a user interface that 99.9% of all Windows users expect to get from a piece of software they'd consider paying money for.  Best of all, controlling the window size and state is trivial without having to P/Invoke an obscure API function.  Recommended.
    Hans Passant.
    Sunday, August 03, 2008 9:58 PM
    Moderator
  • OK, nevermind.

    Well, then, how do you make a console application run in full screen mode?
    Sunday, August 03, 2008 10:42 PM
  • It's in my code sample, SetConsoleDisplayMode().  It takes hardware support and that support has been eroding.  Vista added a new API to check if the hardware actually allows full screen mode, that's how rare it is getting.  You don't want to go there, it is redrum on LCD panels.
    Hans Passant.
    Sunday, August 03, 2008 10:59 PM
    Moderator
  • To just maximize the window (not make it full screen, which is a like trying to wrestle a bull to the ground covered in baby-oil...don't ask)

    you only need one line of code:

    ShowWindow( GetConsoleWindow(), SW_MAXIMIZE);

    Thar ya go.

    Full screen is really, really hard, as you've no doubt found out, but I'm interested, personally. Good luck to you all.
    Tuesday, March 17, 2009 8:24 AM
  •  ShowWindow( GetConsoleWindow(), SW_MAXIMIZE);

    Just curious if that will do the trick, You can P/Invoke the buffer size as well as the width and hight of Console window and then maximize it. It would not be the true full screen though because it won't be docked but otherwise visuallly it will be full screen.
     

    Edit: I think nobugz is doing just that but I don't see the docking.

    AlexB
    Tuesday, March 17, 2009 1:33 PM
  • You can use

    Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);

    to maximize the console window.

     


    Regards, Anindya Chatterjee http://abstractclass.org
    • Proposed as answer by demoxz Wednesday, November 02, 2016 2:33 PM
    Friday, July 16, 2010 2:35 PM
  • You can use

    Console.SetWindowSize(Console.LargestWindowWidth, Console.LargestWindowHeight);

    to maximize the console window.

     


    Regards, Anindya Chatterjee http://abstractclass.org
    Nice, that worked!
    Wednesday, November 02, 2016 2:34 PM