2

I need to prevent the user of my console program from resizing the window, only allowing it to be changed programmatically. If the user changes the width, everything messes up. Also, I want to disable the maximise button. Are either of these possible in Console?

This answer neatly covers how to disable resizing a form in WinForms, but it won't work for Console.

Community
  • 1
  • 1
Lou
  • 2,200
  • 2
  • 33
  • 66
  • 1
    The answer you linked to does not and *can not* apply to console apps. The significant difference is that a console app does not own its own console window, but rather uses one provided by an outside Windows process (once `csrss`, now `conhost` - [Console Host](https://blogs.technet.microsoft.com/askperf/2009/10/05/windows-7-windows-server-2008-r2-console-host/)). – dxiv Jul 04 '16 at 04:17

2 Answers2

2

I came up with a solution that prevents re-sizing of a console window application (either by dragging the corner border or by clicking on the maximize or minimize buttons). The following code is written in the form of a complete VB.Net console application (i.e., a Module):

Module Module1

    Private Const MF_BYCOMMAND As Integer = &H0
    Public Const SC_CLOSE As Integer = &HF060
    Public Const SC_MINIMIZE As Integer = &HF020
    Public Const SC_MAXIMIZE As Integer = &HF030
    Public Const SC_SIZE As Integer = &HF000

    Friend Declare Function DeleteMenu Lib "user32.dll" (ByVal hMenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
    Friend Declare Function GetSystemMenu Lib "user32.dll" (hWnd As IntPtr, bRevert As Boolean) As IntPtr


    Sub Main()

        Dim handle As IntPtr
        handle = Process.GetCurrentProcess.MainWindowHandle ' Get the handle to the console window

        Dim sysMenu As IntPtr
        sysMenu = GetSystemMenu(handle, False) ' Get the handle to the system menu of the console window

        If handle <> IntPtr.Zero Then
            DeleteMenu(sysMenu, SC_CLOSE, MF_BYCOMMAND) ' To prevent user from closing console window
            DeleteMenu(sysMenu, SC_MINIMIZE, MF_BYCOMMAND) 'To prevent user from minimizing console window
            DeleteMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND) 'To prevent user from maximizing console window
            DeleteMenu(sysMenu, SC_SIZE, MF_BYCOMMAND) 'To prevent the use from re-sizing console window
        End If

        Do Until (Console.ReadKey.Key = ConsoleKey.Escape)
            'This loop keeps the console window open until you press escape      
        Loop

    End Sub

End Module

I based this answer off of the Stack Overflow question/answer: "Disable the maximize and minimize buttons of a c# console [closed]"

Please let me know if this doesn't work for you. Good luck!

Community
  • 1
  • 1
ChicagoMike
  • 618
  • 3
  • 11
  • That should come pretty close, but I don't know that it works when resizing the console by dragging the borders. (As a sidenote, the revamped console in Windows 10 also allows resizing horizontally, not just vertically - `windows resize` paragraph at [Console Improvements in the Windows 10 Technical Preview](https://blogs.windows.com/buildingapps/2014/10/07/console-improvements-in-the-windows-10-technical-preview/)). – dxiv Jul 04 '16 at 04:21
  • It works nicely, it stops me from resizing the border and from maximising the window. Only two issues: I also can't close the program manually (X is blanked out) and if I snap the window to a corner, it still messes up all the GUI. But the latter is probably a windows specific problem. Anyway, I'm accepting because your answer works. But if you can show me how to fix the not-being-able-to-close-the-program problem, that would be awesome. – Lou Jul 04 '16 at 12:31
  • To allow the user to close the console by clicking on the "X", just comment out the first "DeleteMenu" entry (the one with SC_CLOSE). – ChicagoMike Jul 04 '16 at 15:17
  • Thanks for the help! – Lou Jul 04 '16 at 17:44
1

I wanted to comment on the accepted answer, but I lack reputation...

To prevent the console window from resizing when you snap it to a corner, you can use

Console.SetBufferSize(80, 24) ' or whatever size you're using...
  • What is the buffer, how does it differ from the window size? – Lou Jul 06 '16 at 00:07
  • +1. This is equivalent to C++'s `SetConsoleScreenBufferSize(GetStdHandle(STD_OUTPUT_HANDLE), { 80, 24 });`. Good enough for simple resize restrictions. (The accepted answer doesn't work in C++, and this works, so +1 for "compatibility and simplicity" on both languages) – amegyoushi Nov 22 '20 at 04:56