MajedKM
MajedKM

Reputation: 31

How do I remove all the buttons on the console application?

Im trying to remove all the buttons on the console application (minimise, maximise, and close) however cant find anything online thats useful. I tried this code which i converted from C# but it didnt work either:

Class Program
        Private Const MF_BYCOMMAND As Integer = &H0
        Public Const SC_CLOSE As Integer = &HF060
        <DllImport("user32.dll")>
        Public Shared Function DeleteMenu(ByVal hMenu As IntPtr, ByVal nPosition As Integer, ByVal wFlags As Integer) As Integer
        End Function

        <DllImport("user32.dll")>
        Private Shared Function GetSystemMenu(ByVal hWnd As IntPtr, ByVal bRevert As Boolean) As IntPtr
        End Function

        <DllImport("kernel32.dll", ExactSpelling:=True)>
        Private Shared Function GetConsoleWindow() As IntPtr
        End Function

        Private Shared Sub Main(ByVal args As String())
            DeleteMenu(GetSystemMenu(GetConsoleWindow(), False), SC_CLOSE, MF_BYCOMMAND)
            Console.Read()
        End Sub
    End Class

Upvotes: 1

Views: 85

Answers (1)

Jonathan Barraone
Jonathan Barraone

Reputation: 487

And this class to your code file:

Public Class HelperClass
Const WS_BORDER As Integer = 8388608
Const WS_DLGFRAME As Integer = 4194304
Const WS_CAPTION As Integer = WS_BORDER Or WS_DLGFRAME
Const WS_SYSMENU As Integer = 524288
Const WS_THICKFRAME As Integer = 262144
Const WS_MINIMIZE As Integer = 536870912
Const WS_MAXIMIZEBOX As Integer = 65536
Const GWL_STYLE As Integer = -16
Const GWL_EXSTYLE As Integer = -20
Const WS_EX_DLGMODALFRAME As Integer = &H1
Const SWP_NOMOVE As Integer = &H2
Const SWP_NOSIZE As Integer = &H1
Const SWP_FRAMECHANGED As Integer = &H20

<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=False)>
Private Shared Function GetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer) As Integer
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=False)>
Private Shared Function SetWindowLong(ByVal hWnd As IntPtr, ByVal nIndex As Integer, ByVal dwNewLong As Integer) As Integer
End Function

<DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=False)>
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 Integer) As Boolean
End Function
Public Shared Sub MakeExternalWindowBorderless(ByVal MainWindowHandle As IntPtr)
    Dim Style As Integer = 0
    Style = GetWindowLong(MainWindowHandle, GWL_STYLE)
    Style = Style And Not WS_CAPTION
    Style = Style And Not WS_SYSMENU
    Style = Style And Not WS_THICKFRAME
    Style = Style And Not WS_MINIMIZE
    Style = Style And Not WS_MAXIMIZEBOX
    SetWindowLong(MainWindowHandle, GWL_STYLE, Style)
    Style = GetWindowLong(MainWindowHandle, GWL_EXSTYLE)
    SetWindowLong(MainWindowHandle, GWL_EXSTYLE, Style Or WS_EX_DLGMODALFRAME)
    SetWindowPos(MainWindowHandle, New IntPtr(0), 0, 0, 0, 0, SWP_NOMOVE Or SWP_NOSIZE Or SWP_FRAMECHANGED)
End Sub
End Class

Next in your main method add this:

HelperClass.MakeExternalWindowBorderless(Process.GetCurrentProcess().MainWindowHandle)
Console.Read()

Upvotes: 1

Related Questions