This sample will show you how to disable the little 'X' close button found on the upper right of
every window. To do this what we will do is use a couple of API calls to remove the Close menu
item from the windows system menu. Doing this will disable the close button. When you try
out this sample, you may want to have an alternate way to close your window.
Insert this code into a .bas module
Option Explicit
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&
Public Sub DisableCloseWindowButton(frm As Form)
Dim hSysMenu As Long
'Get the handle to this windows
'system menu
hSysMenu = GetSystemMenu(frm.hwnd, 0)
'Remove the Close menu item
'This will also disable the close button
RemoveMenu hSysMenu, 6, MF_BYPOSITION
'Lastly, we remove the seperator bar
RemoveMenu hSysMenu, 5, MF_BYPOSITION
End Sub
'--end code block
Now call the DisableCloseWindowButton from your forms load event.
Private Sub Form_Load()
DisableCloseWindowButton Me
End Sub
'--end code block