none
Disable close, minimize and maximize buttons on console application?

    Question

  •  

    Hi all!

     

    I´ve been looking for a way to disable the close, minimize and maximize button on any console application for some time now. A couple of days ago, I found the code to disable the close button in a MS KB-article (http://support.microsoft.com/kb/818361)

     

    I modified the code somewhat so that the program accepts a command line argument, giving me the opportunity to disable the close button for any application currently running.

     

    The problem is that this code doesn´t work for disabling minimize and maximize buttons.

    Sure, I can make the minimize/maximize options in the right-click context menu disappear for any application by doing

     

    DeleteMenu(hMenu, 6, 1024)

    DeleteMenu(hMenu, 5, 1024)

    DeleteMenu(hMenu, 4, 1024)

    DeleteMenu(hMenu, 3, 1024)

     

    or putting the function inside a loop, but that still leaves the top-right window buttons activated.

     

    I´ve found a couple of suggestions on how to disable the top-right buttons aswell, but none of them seems to work for windows 2000/windows xp.

     

    Anyone know how to disable the minimize/maximize buttons for win200/winxp?

     

    Cheers

    Friday, March 07, 2008 9:15 AM

Answers

  • Dallastower,

     

    Based on your post, the KB 818361 uses PInvoke to call the related functions such as DeleteMenu, GetForegroundWindow, GetSystemMenu in user32.dll to disable the "Close" button of console application.

     

    I would like to provide you the following code snippet to disable the Maximize and Minimize button from this thread: How do you disable Minimize, Maximize, Close buttons + remove app icon on a WPF window

     

    Code Snippet

        <DllImport("user32.dll")> _

        Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer

        End Function

     

        <DllImport("user32.dll")> _

         Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer

        End Function

     

        Private Const GWL_STYLE As Integer = (-16)

     

        Private Sub RemoveControlBoxes()

            Dim hwnd As IntPtr = New Interop.WindowInteropHelper(Me).Handle

            Dim windowLong As Long = GetWindowLong(hwnd, GWL_STYLE)

            windowLong = windowLong And -131073 And -65537

            SetWindowLong(hwnd, GWL_STYLE, CInt(windowLong))

        End Sub

     

     

    The Interop.WindowInteropHelper assists interoperation between Windows Presentation Foundation (WPF) and Win32 code and can only be applied on .NET Framework 3.0/3.5.

     

    In addition, please read the article PRB: Cannot Remove Minimize or Maximize Button from Caption Bar

     

    Under Windows 95, Windows 98, and Windows NT version 4.0 and later, applications can create a window (overlapped or popup) with just the Close button (the X button) by creating the window without specifying the WS_MAXIMIZEBOX or WS_MINIMIZEBOX styles. Calling SetWindowLong(GWL_STYLE) to change or remove the minimize of the maxmize buttons dynamically still displays both buttons with one of them disabled.

     

    Hope that can help you to understand the disable issue.

    Tuesday, March 11, 2008 5:53 AM

All replies

  • Dallastower,

     

    Based on your post, the KB 818361 uses PInvoke to call the related functions such as DeleteMenu, GetForegroundWindow, GetSystemMenu in user32.dll to disable the "Close" button of console application.

     

    I would like to provide you the following code snippet to disable the Maximize and Minimize button from this thread: How do you disable Minimize, Maximize, Close buttons + remove app icon on a WPF window

     

    Code Snippet

        <DllImport("user32.dll")> _

        Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer

        End Function

     

        <DllImport("user32.dll")> _

         Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer

        End Function

     

        Private Const GWL_STYLE As Integer = (-16)

     

        Private Sub RemoveControlBoxes()

            Dim hwnd As IntPtr = New Interop.WindowInteropHelper(Me).Handle

            Dim windowLong As Long = GetWindowLong(hwnd, GWL_STYLE)

            windowLong = windowLong And -131073 And -65537

            SetWindowLong(hwnd, GWL_STYLE, CInt(windowLong))

        End Sub

     

     

    The Interop.WindowInteropHelper assists interoperation between Windows Presentation Foundation (WPF) and Win32 code and can only be applied on .NET Framework 3.0/3.5.

     

    In addition, please read the article PRB: Cannot Remove Minimize or Maximize Button from Caption Bar

     

    Under Windows 95, Windows 98, and Windows NT version 4.0 and later, applications can create a window (overlapped or popup) with just the Close button (the X button) by creating the window without specifying the WS_MAXIMIZEBOX or WS_MINIMIZEBOX styles. Calling SetWindowLong(GWL_STYLE) to change or remove the minimize of the maxmize buttons dynamically still displays both buttons with one of them disabled.

     

    Hope that can help you to understand the disable issue.

    Tuesday, March 11, 2008 5:53 AM
  • Bruno,

     

    Thanks for your reply.

     

    I acutally found a code snippet that is similar to your example:

     

    Code Snippet

    <DllImport("user32.dll")> _

    Public Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer

    End Function

     

    <DllImport("user32.dll")> _

    Public Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer

    End Function

     

    <DllImport("user32.dll")> _

    Private Shared Function SetWindowPos(ByVal hWnd As IntPtr, ByVal hWndInsertAfter As IntPtr, ByVal X As Integer, ByVal Y As Integer, ByVal cx As Integer, ByVal cy As Integer, _

    ByVal uFlags As UInteger) As Boolean

    End Function

     

    Const GWL_STYLE As Integer = -16

    Const WS_SYSMENU As Integer = 524288

     

    Public Shared Sub Main(ByVal args As String())

    ' Ommited code for ObtainWindowHandle to same space

    Dim hWnd As Integer = ObtainWindowHandle(args(0))

    Dim style As Integer = GetWindowLong(hWnd, GWL_STYLE)

    SetWindowLong(hWnd, GWL_STYLE, (style And Not WS_SYSMENU))

    SetWindowPos(hWnd, IntPtr.Zero, 0, 0, 0, 0, 55)

    End Sub

     

     

    This code removes the entire controlbox, that is minimize, maximize/restore and close for the application specified as argument. I tried it and it works fine with some applications, like windows calculator. However it does not for for the specific application that I need to disable the controlbox for...

     

    I´m guessing it has something to do with the fact that the Calculator application is win 32 and I´m pretty sure that my target application is win 16 (it´s run in a command line window).

    Tuesday, March 11, 2008 6:42 PM