Disabling the close button

Sometimes (for example, in a pc-lock program, or error dialog where a decision must be made) you will not want your users to be able to close a window simply by pressing the X button. This code disables the X button, and also the Close item on the system menu that is displayed when you right-click on the title bar)

Private Declare Function DeleteMenu Lib "user32" (ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Declare Function GetSystemMenu Lib "user32" (ByVal hWnd As Long, ByVal bRevert As Long) As Long

Private Const MF_BYPOSITION = &H400&

Private Sub Form_Load()
    '// remove the close menu (which then disables the close button)
    RemoveMenus
End Sub

Private Sub RemoveMenus()
    Dim hMenu As Long
    ' Get the form's system menu handle.
    hMenu = GetSystemMenu(hWnd, False)
    DeleteMenu hMenu, 6, MF_BYPOSITION
End Sub

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“The difference between theory and practice is smaller in theory than in practice.”