Share this page 

Remove the Close item in the system menu.Tag(s): WinAPI/Registry


[external function declaration]
FUNCTION ulong GetSystemMenu( ulong hWnd, boolean bRevert ) &
  LIBRARY "user32.dll"
FUNCTION boolean DeleteMenu( ulong hMenu, uint uPosition, uint uFlags ) &
  LIBRARY "user32.dll"
FUNCTION boolean DrawMenuBar( ulong hWnd ) LIBRARY "user32.dll"
FUNCTION boolean InsertMenuA( ulong hMenu, uint uPosition, uint uFlags, & 
  uint uIDNewItem, string lpNewItem ) &
  LIBRARY "user32.dll"

{Powerscript (to remove) ]
CONSTANT uint SC_CLOSE = 61536
CONSTANT uint MF_BYCOMMAND = 0

ulong    hWnd, hMenu

hWnd = Handle( this )
hMenu = GetSystemMenu( hWnd, FALSE ) 
IF hMenu > 0 THEN
    DeleteMenu( hMenu, SC_CLOSE , MF_BYCOMMAND)
    DrawMenuBar( hWnd )
END IF

{Powerscript (to insert) ]
InsertMenuA &
  ( hMenu, SC_CLOSE , MF_BYCOMMAND, SC_CLOSE, "&Close~tAlt+F4" ) 

NOTE: By removing the Close item in the system menu, the top-right 'X' is disable.

While the Close item is gone, the user still can do an ALT-F4 to close the window. To trap ALT-F4 you need to process the SC_CLOSE Windows message. Here how it can be done.

[ue_syscommand mapped to pbm_syscommand]
CONSTANT unsignedlong SC_CLOSE = 61536

IF commandType = SC_CLOSE THEN
   RETURN 1
END IF
NOTE : Other possible values : 61472 (Minimize), 61458 (Title Bar/Move), 61728 (Restore)