3

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!

Donavon
  • 323
  • 2
  • 6
  • 11
  • There's an odd hack in this [answer](http://stackoverflow.com/a/7802086/529282) where basically we put a console inside a WinForm. Since it's trivial to lock the WinForm, perhaps it could serve your need. – Martheen Aug 18 '15 at 04:06

4 Answers4

8

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.

LuizLoyola
  • 386
  • 3
  • 20
Sven7
  • 81
  • 1
  • 1
2

Console.SetWindowSize will be your friend.

Ben
  • 2,433
  • 5
  • 39
  • 69
Infinity Challenger
  • 1,090
  • 10
  • 19
2

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);
    }
}
Nikerym
  • 515
  • 3
  • 12
-1

But one thing you will face Windows snap features.

im still finding solution for console

here is solution for Winform app

How can i programmatically use aero snap features from C# code

haseakash
  • 31
  • 1
  • 6
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31224732) – Nick Vu Mar 10 '22 at 16:28