Disable Exit SysMenu Item

Share your advanced PureBasic knowledge/code with the community.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Disable Exit SysMenu Item

Post by BackupUser »

Code updated for 5.20+

Restored from previous forum. Originally posted by Franco.

Don't know if somebody else has posted such a thing...

Code: Select all

; (c) 2002 - Franco's template - absolutely freeware
; Procedure to disable the close button of your window.
; Needs the handle of the window
; Very useful for ChildWindows...
; Only your main Window can close your application :)
; Remember: If you make a 'GetSystemMenu_(WindowID,#TRUE)' API call
; after you call this Procedure you get the ExitMenuItem back!
; Than you have to remove it again...
; Remember: If you add your Items to a SysMenu you have to call this Procedure first.
; Otherwise you will remove your own Items (this Procedure counts from the back...)

Procedure DisableExitSysMenuItem(WindowID)
  SysMenu = GetSystemMenu_(WindowID, #False)   ;change to #True to re-enable
  SysMenuItemCount = GetMenuItemCount_(SysMenu)
  RemoveMenu_(SysMenu, SysMenuItemCount -1, #MF_DISABLED | #MF_BYPOSITION)
  RemoveMenu_(SysMenu, SysMenuItemCount -2, #MF_DISABLED | #MF_BYPOSITION)
  DrawMenuBar_(WindowID)
EndProcedure

OpenWindow(0, #PB_Any, #PB_Any, 200, 100, "Disable Close Button",
           #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
ButtonGadget(1, 30, 30, 140, 30, "Close Window Here")
DisableExitSysMenuItem(WindowID(0))

Repeat
  Select WaitWindowEvent()
    Case #PB_Event_Gadget
      Select EventGadget()
        Case 1
          appQuit = 1
      EndSelect
  EndSelect
Until appQuit = 1
Have a nice day...
Franco

Sometimes you have to go a lonely way to accomplish genius things.
BackupUser
PureBasic Guru
PureBasic Guru
Posts: 16777133
Joined: Tue Apr 22, 2003 7:42 pm

Post by BackupUser »

Restored from previous forum. Originally posted by PB.

> Don't know if somebody else has posted such a thing...

Yes, actually. But the one I posted wasn't as "proper" as yours, and
although I fixed mine for vis2pure, I didn't bother reposting the new version
here. So it's good that you did, because it's the proper way to do it, unlike
my post here.


PB - Registered PureBasic Coder
Post Reply