1

Excuse me for the question, here I put the cmd in fixed size, that the user cannot resize it with the mouse,

I had the same case in C++.

HWND consoleWindow = GetConsoleWindow(); SetWindowLong(consoleWindow, GWL_STYLE, GetWindowLong(consoleWindow, GWL_STYLE) & ~WS_MAXIMIZEBOX & ~WS_SIZEBOX); 

But I don't have any idea for C#, in the meantime I made the principle of launching a threading

int x = Console.WindowWidth; int y = Console.WindowHeight; while (true) { if (Console.WindowWidth != x && Console.WindowHeight != y) { Console.SetWindowSize(x, y); } } 

But if you know another method, impossible to find in the microsoft doc, or other. Unless I looked wrong.

Thanks for your help

3
  • You want to prevent the user from resizing the console window? Can I ask why you'd force this? Commented Nov 18, 2020 at 15:33
  • because once the dimensions are set I don't want the user to be able to modify the window, if I display the text in the middle I don't want the user to be able to shrink the window and everything will be unsettled. Commented Nov 18, 2020 at 15:37
  • That && should be an ||, otherwise they can change the size in one direction and then the other. Commented Nov 18, 2020 at 15:55

2 Answers 2

1

Finally I misapplied what I found, I restart it if I'm not the only one in this case, I did as in C++, I used the win32 api,

class Affichage { #region win32 private const int MF_BYCOMMAND = 0x00000000; public const int SC_MINIMIZE = 0xF020; public const int SC_MAXIMIZE = 0xF030; public 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(); #endregion 

You can have more orders here Microsoft

IntPtr handle = GetConsoleWindow(); IntPtr sysMenu = GetSystemMenu(handle, false); if (handle != IntPtr.Zero) { DeleteMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND); DeleteMenu(sysMenu, SC_SIZE, MF_BYCOMMAND); } 
Sign up to request clarification or add additional context in comments.

Comments

1

This seems to work, hope it will help. Taken from MSDN

class Program { private const int MF_BYCOMMAND = 0x00000000; public const int SC_CLOSE = 0xF060; public const int SC_MINIMIZE = 0xF020; public const int SC_MAXIMIZE = 0xF030; public 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) { IntPtr handle = GetConsoleWindow(); IntPtr sysMenu = GetSystemMenu(handle, false); if (handle != IntPtr.Zero) { DeleteMenu(sysMenu, SC_CLOSE, MF_BYCOMMAND); DeleteMenu(sysMenu, SC_MINIMIZE, MF_BYCOMMAND); DeleteMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND); DeleteMenu(sysMenu, SC_SIZE, MF_BYCOMMAND); } Console.Read(); } } 

You need this:

DeleteMenu(sysMenu, SC_CLOSE, MF_BYCOMMAND); DeleteMenu(sysMenu, SC_MINIMIZE, MF_BYCOMMAND); DeleteMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND); DeleteMenu(sysMenu, SC_SIZE, MF_BYCOMMAND); 

The first one prevent user from closing console window, the second one prevent user from minimizing console window, the third one prevent user from maximizing console window and the last one prevent the use from re-sizing console window.

3 Comments

Thank you, yes that's what I just used, it's very functional. I wanted to publish too quickly
Nice! I'm glad you made it!
Links to external sites are discouraged since they may be broken over time. Please try to include the relevant code sample directly in your answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.