Kill the Windowz Close Button (X)

By | 2002-06-01

This subroutine will disable the Windows 9x/NT
‘ close button, also known as the little X
‘ button. It’s really useful when you have a
‘ form that you don’t want to set its ControlBox
‘ property to False. Write this code in a
‘ standard module (BAS).

Original Author: Ultimatum

Assumptions

Should be familiar with the Windows API.

Side Effects

If you’re playing around with the system
‘ menu in other ways in your program, you
‘ might have to change the position number
‘ in your RemoveMenu function calls. Also,
‘ you could have problems running this
‘ with a MDI child.

API Declarations

Private Declare Function GetSystemMenu Lib “user32” _
(ByVal hWnd As Long, ByVal bRevert As Long) As Long
Private Declare Function RemoveMenu Lib “user32” _
(ByVal hMenu As Long, ByVal nPosition As Long, ByVal wFlags As Long) As Long
Private Const MF_BYPOS = &H400&

Code

Public Sub KillCloseButton(hWnd As Long)
Dim hSysMenu As Long
hSysMenu = GetSystemMenu(hWnd, 0)
Call RemoveMenu(hSysMenu, 6, MF_BYPOS)
Call RemoveMenu(hSysMenu, 5, MF_BYPOS)
End Sub
'Call the above function from a form as it's being loaded
Private Sub Form_Load()
KillCloseButton Me.hWnd
End Sub

Author: dwirch

Derek Wirch is a seasoned IT professional with an impressive career dating back to 1986. He brings a wealth of knowledge and hands-on experience that is invaluable to those embarking on their journey in the tech industry.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.