En-/Disable Window Min, Max & Close Buttons
Posted: Fri Jan 20, 2006 8:47 am
Code updated for 5.20+
hi folks,
i just searched the forums for something like this, but i have only found a way to (completely) remove the close menu-item, but not to disable (and gray) it or maybe i didn't searched long enough.
however, i did a quick research on msdn and here is the result:
i hope it will become usefull for you guys. have fun.
// EDIT:
i have put now everything in procedures:
have fun.
c ya,
nco2k
hi folks,
i just searched the forums for something like this, but i have only found a way to (completely) remove the close menu-item, but not to disable (and gray) it or maybe i didn't searched long enough.

however, i did a quick research on msdn and here is the result:
Code: Select all
Procedure SetWindowCloseButton(hWnd, Enabled)
If hWnd And (Enabled = #True Or Enabled = #False)
hSysMenu = GetSystemMenu_(hWnd, #False)
If hSysMenu
EnableMenuItem_(hSysMenu, #SC_CLOSE, #MF_BYCOMMAND | Enabled ! 1)
DrawMenuBar_(hWnd)
EndIf
EndIf
EndProcedure
Enumeration
#GadgetButton_DisableCloseBox
#GadgetButton_Exit
EndEnumeration
If OpenWindow(0, 0, 0, 200, 95, "Disable Window Close Box - Example", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered)
ButtonGadget(#GadgetButton_DisableCloseBox, 5, 10, 190, 25, "Disable Close Box", #PB_Button_Toggle)
ButtonGadget(#GadgetButton_Exit, 5, 60, 190, 25, "Exit")
AddKeyboardShortcut(0, #PB_Shortcut_Alt | #PB_Shortcut_F4, 0)
Repeat
WinEvent = WaitWindowEvent()
If WinEvent = #PB_Event_Gadget
Select EventGadget()
Case #GadgetButton_DisableCloseBox
SetWindowCloseButton(WindowID(0), GetGadgetState(#GadgetButton_DisableCloseBox) ! 1)
Case #GadgetButton_Exit
If GetGadgetState(#GadgetButton_DisableCloseBox) = 0
End
EndIf
EndSelect
ElseIf WinEvent = #PB_Event_Menu And EventMenu() = 0
If GetGadgetState(#GadgetButton_DisableCloseBox) = 0
End
EndIf
EndIf
Until WinEvent = #PB_Event_CloseWindow
EndIf
End

// EDIT:
i have put now everything in procedures:
Code: Select all
Procedure SetWindowMinButton(hWnd.l, Enabled.l)
If (Enabled = #True Or Enabled = #False) And hWnd
nWindowLong.l = GetWindowLong_(hWnd, #GWL_STYLE)
If nWindowLong
If Enabled = #True
SetWindowLong_(hWnd, #GWL_STYLE, nWindowLong | #WS_MINIMIZEBOX)
ElseIf Enabled = #False
SetWindowLong_(hWnd, #GWL_STYLE, nWindowLong & (#WS_MINIMIZEBOX ! - 1))
EndIf
SetWindowPos_(hWnd, 0, 0, 0, 0, 0, #SWP_NOZORDER | #SWP_NOMOVE | #SWP_NOSIZE | #SWP_FRAMECHANGED)
EndIf
EndIf
EndProcedure
Procedure SetWindowMaxButton(hWnd.l, Enabled.l)
If (Enabled = #True Or Enabled = #False) And hWnd
nWindowLong.l = GetWindowLong_(hWnd, #GWL_STYLE)
If nWindowLong
If Enabled = #True
SetWindowLong_(hWnd, #GWL_STYLE, nWindowLong | #WS_MAXIMIZEBOX)
ElseIf Enabled = #False
SetWindowLong_(hWnd, #GWL_STYLE, nWindowLong & (#WS_MAXIMIZEBOX ! - 1))
EndIf
SetWindowPos_(hWnd, 0, 0, 0, 0, 0, #SWP_NOZORDER | #SWP_NOMOVE | #SWP_NOSIZE | #SWP_FRAMECHANGED)
EndIf
EndIf
EndProcedure
Procedure SetWindowCloseButton(hWnd.l, Enabled.l)
If (Enabled = #True Or Enabled = #False) And hWnd
hSysMenu = GetSystemMenu_(hWnd, #False)
If hSysMenu
EnableMenuItem_(hSysMenu, #SC_CLOSE, #MF_BYCOMMAND | Enabled ! 1)
DrawMenuBar_(hWnd)
EndIf
EndIf
EndProcedure
Enumeration
#GadgetButton_DisableMinBox
#GadgetButton_DisableMaxBox
#GadgetButton_DisableCloseBox
#GadgetButton_Exit
EndEnumeration
If OpenWindow(0, 0, 0, 200, 145, "Disable Window Close Box - Example", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_MaximizeGadget | #PB_Window_ScreenCentered)
ButtonGadget(#GadgetButton_DisableMinBox, 5, 5, 190, 25, "Disable Minimize Box", #PB_Button_Toggle)
ButtonGadget(#GadgetButton_DisableMaxBox, 5, 35, 190, 25, "Disable Maximize Box", #PB_Button_Toggle)
ButtonGadget(#GadgetButton_DisableCloseBox, 5, 65, 190, 25, "Disable Close Box", #PB_Button_Toggle)
ButtonGadget(#GadgetButton_Exit, 5, 115, 190, 25, "Exit")
AddKeyboardShortcut(0, #PB_Shortcut_Alt | #PB_Shortcut_F4, 0)
AddKeyboardShortcut(0, #PB_Shortcut_Escape, 0)
Repeat
WinEvent.l = WaitWindowEvent()
If WinEvent = #PB_Event_Gadget
Select EventGadget()
Case #GadgetButton_DisableMinBox
SetWindowMinButton(WindowID(0), GetGadgetState(#GadgetButton_DisableMinBox) ! 1)
Case #GadgetButton_DisableMaxBox
SetWindowMaxButton(WindowID(0), GetGadgetState(#GadgetButton_DisableMaxBox) ! 1)
Case #GadgetButton_DisableCloseBox
SetWindowCloseButton(WindowID(0), GetGadgetState(#GadgetButton_DisableCloseBox) ! 1)
Case #GadgetButton_Exit
If GetGadgetState(#GadgetButton_DisableCloseBox) = 0
End
EndIf
EndSelect
ElseIf WinEvent = #PB_Event_Menu And EventMenu() = 0
If GetGadgetState(#GadgetButton_DisableCloseBox) = 0
End
EndIf
EndIf
Until WinEvent = #PB_Event_CloseWindow
EndIf
End

c ya,
nco2k