Disable ‘X’ on Forms (Including MDI Child Forms)

By | 2002-06-01

This is shorter way to disable the ‘X’ or close button on a form. It also works on MDI Child forms also which I have found most other code does not.

Original Author: Mark Troutwine

Inputs

None.

Assumptions

Just copy and paste it as stated below!

Returns

None.

Side Effects

None.

API Declarations

‘ Place this in 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 SC_CLOSE = &HF060&
Public Const MF_BYCOMMAND = &H0&

Code

' Place this in the Form Load event of the form you want to disable the 'X':
Dim hSysMenu As Long
hSysMenu = GetSystemMenu(hwnd, False)
RemoveMenu hSysMenu, SC_CLOSE, MF_BYCOMMAND

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.