トップ回答者
Maximizing the Console Window!?

質問
回答
-
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.- 回答としてマーク Michael Sun [MSFT]Microsoft employee, Moderator 2008年8月6日 3:30
-
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.- 回答としてマーク Michael Sun [MSFT]Microsoft employee, Moderator 2008年8月6日 3:29
-
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.- 回答としてマーク Michael Sun [MSFT]Microsoft employee, Moderator 2008年8月6日 3:29
すべての返信
-
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.- 回答としてマーク Michael Sun [MSFT]Microsoft employee, Moderator 2008年8月6日 3:30
-
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.- 回答としてマーク Michael Sun [MSFT]Microsoft employee, Moderator 2008年8月6日 3:29
-
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.- 回答としてマーク Michael Sun [MSFT]Microsoft employee, Moderator 2008年8月6日 3:29
-
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.
-
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