Search code examples
vb.netborderless

(VB.net - Visual Basic) - Open Notepad Borderless Window


What is the easiest and quickest way to open notepad without a border. What I want is to remove the blue markings you see in the image below:

enter image description here

The code I've got so far [Opens notepad]:

Private Sub Button_StartQuiz_Click(sender As Object, e As EventArgs) Handles Button_StartQuiz.Click

    Dim vPID As Object
    vPID = Shell("notepad.exe", vbNormalFocus)

End Sub

Solution

  • The results here are not perfect, so you may need to mess with the API parameters, but something along these lines (a VB.NET port of this answer) may be close.

    Dim p = Process.Start("notepad.exe")
    p.WaitForInputIdle()
    
    Dim styles As WindowStyles = GetWindowLong(p.MainWindowHandle, GWL_STYLE)
    styles = styles And Not (WindowStyles.WS_CAPTION Or WindowStyles.WS_THICKFRAME Or WindowStyles.WS_MINIMIZE Or WindowStyles.WS_MAXIMIZE Or WindowStyles.WS_SYSMENU)
    SetWindowLong(p.MainWindowHandle, GWL_STYLE, styles)
    
    Dim stylesex As ExtendedWindowStyles = GetWindowLong(p.MainWindowHandle, GWL_EXSTYLE)
    stylesex = stylesex And Not (ExtendedWindowStyles.WS_EX_DLGMODALFRAME Or ExtendedWindowStyles.WS_EX_CLIENTEDGE Or ExtendedWindowStyles.WS_EX_STATICEDGE)
    SetWindowLong(p.MainWindowHandle, GWL_EXSTYLE, stylesex)
    
    SetWindowPos(p.MainWindowHandle, IntPtr.Zero, 0, 0, 0, 0, SetWindowPosFlags.SWP_FRAMECHANGED Or SetWindowPosFlags.SWP_NOMOVE Or SetWindowPosFlags.SWP_NOSIZE Or SetWindowPosFlags.SWP_NOZORDER Or SetWindowPosFlags.SWP_NOOWNERZORDER)
    
    
    <DllImport("User32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Shared Function GetWindowLong(hWnd As IntPtr, nIndex As Int16) As Int32
    End Function
    
    <DllImport("User32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Shared Function SetWindowLong(hWnd As IntPtr, nIndex As Int16, dwNewLong As Int32) As Int32
    End Function
    
    <DllImport("User32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
    Shared Function SetWindowPos(hWnd As IntPtr, hWndInsertAfter As IntPtr, X As Int16, Y As Int16, cx As Int16, cy As Int16, uFlags As UInt16) As Boolean
    End Function
    
    Const GWL_STYLE As Int16 = -16
    Const GWL_EXSTYLE As Int16 = -20
    
    <Flags>
    Enum WindowStyles
        WS_CAPTION = &HC00000
        WS_THICKFRAME = &H40000
        WS_MINIMIZE = &H20000000
        WS_MAXIMIZE = &H1000000
        WS_SYSMENU = &H80000
    End Enum
    
    <Flags>
    Enum ExtendedWindowStyles
        WS_EX_DLGMODALFRAME = &h00000001
        WS_EX_CLIENTEDGE = &h00000200
        WS_EX_STATICEDGE = &h00020000
    End Enum
    
    <Flags>
    Enum SetWindowPosFlags As UInt16
        SWP_FRAMECHANGED = &h0020
        SWP_NOMOVE = &h0002
        SWP_NOSIZE = &h0001
        SWP_NOZORDER = &h0004
        SWP_NOOWNERZORDER = &h0200
    End Enum
    

    The result is a little funky on my PC (the blue is my desktop color), since the menu is still showing and everything has shifted to take up the space occupied by the border, but not expanded to take up the extra room. Maybe different parameters will fix that?

    Borderless Notepad.exe