c# - Is there a way to make a fixed (height/width) console?

10,263

Solution 1

In order to achieve this effect, you need to use some C++ class library:

const int MF_BYCOMMAND = 0x00000000;
const int SC_MINIMIZE = 0xF020;
const int SC_MAXIMIZE = 0xF030;
const int SC_SIZE = 0xF000;

[DllImport("user32.dll")]
public static extern int DeleteMenu(IntPtr hMenu, int nPosition, int wFlags);

[DllImport("user32.dll")]
private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

[DllImport("kernel32.dll", ExactSpelling = true)]
private static extern IntPtr GetConsoleWindow();

static void Main(string[] args)
{
    Console.WindowHeight = 25;
    Console.WindowWidth = 80;

    DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_MINIMIZE, MF_BYCOMMAND);
    DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_MAXIMIZE, MF_BYCOMMAND);
    DeleteMenu(GetSystemMenu(GetConsoleWindow(), false), SC_SIZE, MF_BYCOMMAND);

    Console.WriteLine("Yes, its fixed!");
    Console.ReadLine();
}

Hope this is useful.

Solution 2

Console.SetWindowSize will be your friend.

Solution 3

This is an absolutely horrible solution but you could call throughout your code a "check size" method that checks Console.WindowHeight and Console.WindowWidth and resets them if they are not equal, it won't stop people resizing, but will at least keep it to the size you want.

Edit: The reason I post what I feel is a horrible solution, is because as far as i'm aware there is no builtin functionality to allow what your trying to do, i'm giving you a workaround.

private void CheckAndResetWindowSize(){
    if(Console.WindowHeight != 300 || Console.WindowWidth != 500) {
        Console.SetWindowSize(500, 300);
    }
}
Share:
10,263
Donavon
Author by

Donavon

Updated on June 05, 2022

Comments

  • Donavon
    Donavon almost 2 years

    I've been messing around in microsoft visual studio 2013, and I made a console application, but I was wonder if there was a way to make a fixed size for it (height/width), meaning it can't be resized whatsoever? If anyone knows if this is possible, I'd appreciate if you can help me. Thank you!