none
Maximizing the Console Window!? RRS feed

  • 質問

  • 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?

    2008年8月2日 15:19

回答

  • 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.
    2008年8月2日 16:36
    モデレータ
  • 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.
    2008年8月3日 21:58
    モデレータ
  • 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.
    2008年8月3日 22:59
    モデレータ

すべての返信

  • You can call ShellExecute with SW_MAXIMIZE
    2008年8月2日 16:25
  • 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.
    2008年8月2日 16:36
    モデレータ
  • 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.
    2008年8月3日 1:42
  • You don't include anything in C#. That #include  is for C++. You run the code nobugs posted. It will work.
    AlexB
    2008年8月3日 2:31
  • 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.
    2008年8月3日 20:47
  • 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.
    2008年8月3日 21:18
  • 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.
    2008年8月3日 21:58
    モデレータ
  • OK, nevermind.

    Well, then, how do you make a console application run in full screen mode?
    2008年8月3日 22:42
  • 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.
    2008年8月3日 22:59
    モデレータ
  • 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.
    2009年3月17日 8:24
  •  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
    2009年3月17日 13:33
  • You can use

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

    to maximize the console window.

     


    Regards, Anindya Chatterjee http://abstractclass.org
    • 回答の候補に設定 demoxz 2016年11月2日 14:33
    2010年7月16日 14:35
  • You can use

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

    to maximize the console window.

     


    Regards, Anindya Chatterjee http://abstractclass.org
    Nice, that worked!
    2016年11月2日 14:34
  • awesome Anindya Chatterjee!
    2019年6月5日 13:09