Graying out a window's Close button

Hello,

I’m trying to temporarily gray out the close button for a window, however setting the window’s CloseButton property to False at runtime seems to have no effect.

Using 2017R3.

Is there another way to do it?

Here you go:

https://www.dropbox.com/s/m8r3340grtzpqys/WindowDisableCloseButton.xojo_binary_project?dl=1

I have no idea why the framework doesn’t do this behind the scenes.

Here’s the code if someone searches the forum later, add the code to a module or remove the “Extends” keyword:

[code]Public Sub DisableCloseButton(Extends w as Window)
'Declares created with https://forum.xojo.com/47389-xojo-ide-reformat-code-script or https://blog./2017/01/22/windows-to-xojo-data-type-conversion/
Const CS_NOCLOSE = &h0200
Const GCL_STYLE = -26

Declare Function SetClassLong Lib “User32” Alias “SetClassLongW” (hWnd As Integer, nIndex As Int32, dwNewLong As Int32) As UInt32
Declare Function GetClassLong Lib “User32” Alias “GetClassLongW” (hWnd As Integer, nIndex As Int32) As UInt32

Call SetClassLong(w.handle, GCL_STYLE, GetClassLong(w.handle, GCL_STYLE) Or CS_NOCLOSE)
End Sub
[/code]

[code]Public Sub EnableCloseButton(Extends w as Window)
'Declares created with https://forum.xojo.com/47389-xojo-ide-reformat-code-script or https://blog./2017/01/22/windows-to-xojo-data-type-conversion/
Const CS_NOCLOSE = &h0200
Const GCL_STYLE = -26

Declare Function SetClassLong Lib “User32” Alias “SetClassLongW” (hWnd As Integer, nIndex As Int32, dwNewLong As Int32) As UInt32
Declare Function GetClassLong Lib “User32” Alias “GetClassLongW” (hWnd As Integer, nIndex As Int32) As UInt32

Call SetClassLong(w.handle, GCL_STYLE, GetClassLong(w.handle, GCL_STYLE) And Not CS_NOCLOSE)
End Sub
[/code]

Julian is faster than I.

Here is my version:

CloseButton.zip

Here is the link to the forum discussion: window closebutton cant be changed at runtime

Eugene’s solution using EnableMenuItem should be preferred in most cases as it only enables/disables the button on the current window.

SetClassLong redefines all instances of the window. Using it to enable/disable the close button works OK until you have more than one instance of the window.

Thanks everyone!

Thanks Andrew, I totally forgot about that. Yes Eugene’s method is great as it also greys the windows drop down menu entry for Close.

You might want to put this in Window.KeyDown to disable the use of ALT+F4 when the close button is disabled/greyed.

Const SC_CLOSE = &hF060 Const MF_GRAYED = 1 Const MF_DISABLED = 2 Declare Function GetSystemMenu Lib "User32" (hWnd As Integer, bRevert As Boolean) As Integer Declare Function GetMenuState Lib "User32" (hMenu As Integer, uId As UInt32, uFlags As UInt32) As UInt32 Dim state As UInt32 = GetMenuState(GetSystemMenu(Self.handle, False), SC_CLOSE, 0) If Keyboard.AltKey And Asc(key) = 203 And (state And (MF_DISABLED Or MF_GRAYED)) = 1 Then Return True