Disabling the system close

 

horizontal rule

Back Home Next

Author Aart Onkenhout
Date 02-10-2000

 

You can give the X in the systemmenu a disabled look by using one of the following methods.

 

First method

 

 

Declare the following Local External Functions:

Function Long GetSystemMenu( uLong hwnd, Long bRevert ) Library "user32.dll"
Function Long RemoveMenu( uLong hMenu, Long nPosition, Long wFlags ) Library "user32.dll"

 

In the Open-event of your Window:

 

Declare Long    MF_BYPOSITION = 1024

uLong hSysMenu


//Get the system menu for the form
hSysMenu = GetSystemMenu( Handle( this ), 0 )
// Remove the close item
RemoveMenu( hSysMenu, 6, MF_BYPOSITION )
// and the seperator
RemoveMenu( hSysMenu, 5, MF_BYPOSITION )

 

 

A second method

 

The fact that you totally remove the Close menuitem is a drawback of this method. If you want to enable it again, you have to insert two menuitems. So, another way to do this is disabling the menuitem instead of removing it. Use the following code to achieve this:

 

Local External Functions:
Function uLong GetSystemMenu( uLong hwnd, long bRevert ) Library "user32.dll"
Function Long ModifyMenuA( uLong hMenu, Long nPosition, Long wFlags, Long uIdNewItem, Ref String lpNewItem ) Library "user32.dll"
Function Long GetMenuStringA( uLong hMenu, Long uIdItem, Ref String lpString, int nMaxCount, Long uFlag ) Library "user32.dll"
Function Boolean DrawMenuBar( uLong hwnd ) Library "user32.dll"


Instance Variables:
Constant Long MF_BYCOMMAND = 0
Constant Long MF_GRAYED = 1
Constant Long MF_ENABLED = 0
Constant Long SC_CLOSE = 61536


To disable the X-button:

String ls_MenuName
uLong  lul_Menu

lul_Menu = GetSystemMenu( Handle( yourWindow ), 0 )

ls_MenuName = Space(256)

// This gives you the text within the Close menu-item. Needed in call to ModifyMenuA
GetMenuStringA( lul_Menu, SC_CLOSE, ls_MenuName, 256, MF_BYCOMMAND )

// Modify the Close-menu. This will also disable the X-button.
ModifyMenuA( lul_Menu, SC_CLOSE, MF_BYCOMMAND + MF_GRAYED, SC_CLOSE, ls_MenuName )

// Force the WIndow to redraw the menubar, otherwise changes won't be visible
DrawMenuBar( Handle( yourWindow ) )


 

To enable the X-button again:

String ls_MenuName
uLong  lul_Menu

lul_Menu = GetSystemMenu( Handle( yourWindow ), 0 )

ls_MenuName = Space(256)

GetMenuStringA( lul_Menu, SC_CLOSE, ls_MenuName, 256, MF_BYCOMMAND )

ModifyMenuA( lul_Menu, SC_CLOSE, MF_BYCOMMAND, SC_CLOSE, ls_MenuName )

DrawMenuBar( Handle( yourWindow ) )