Disable a window's Close button
By: Lorenzo Dalla Vecchia  Date: 21/10/2001

This tip describes how to disable the little X (formaly called Close button) on the top-right corner of almost every window.
This is done through deleting the Close item from the windw's Control menu (that pops up by clicking the icon on the top-left).

As always, the first thing you need is copy some API declarations into a module.

Public Declare Function GetSystemMenu Lib "user32" _
(ByVal hwnd As Long, ByVal bRevert As Long) As Long
Public Declare Function RemoveMenu Lib "user32" _
(ByVal hMenu As Long, ByVal nPosition As Long, _
ByVal wFlags As Long) As Long
Public Const MF_BYPOSITION = &H400&

And the following custom function definition:

Public Sub DisableCloseButton(F As Form)
   
Dim hSysMenu As Long
    hSysMenu = GetSystemMenu(F.hWnd, 0)
    RemoveMenu hSysMenu, 6, MF_BYPOSITION
    RemoveMenu hSysMenu, 5, MF_BYPOSITION
End Sub

Now, its simple to disable any window's Close button:

Call DisableCloseButton(formaname)

formaname is the name of the form of wich you want to disable the close button.

For example, this disables Form3's close button:

Call DisableCloseButton(Form3)

If you are coding inside Form3 itself, remember that you can use the Me kyword:

Call DisableCloseButton(Me)

Note that, when disabling the close button, you should plan another way to close the window.

Click Here!


Visual Basic Programming Zone is a website by Lorenzo Dalla Vecchia.
To contact the Webmaster, click here.
Hosted by InWind: www.inwind.it