Help understanding code to hide taskbar

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
jmorr
Posts: 40
Joined: 23 Mar 2022, 15:26

Help understanding code to hide taskbar

Post by jmorr » 08 Dec 2024, 02:09

The following was posted by @ntepa in response to a request for a script to hide/show the taskbar in Win11:

Code: Select all

HideShowTaskbar() {
    static ABM_SETSTATE := 0xA, ABS_AUTOHIDE := 0x1, ABS_ALWAYSONTOP := 0x2
    static hide := 0
    hide := !hide
    APPBARDATA := Buffer(size := 2*A_PtrSize + 2*4 + 16 + A_PtrSize, 0)
    NumPut("UInt", size, APPBARDATA), NumPut("Ptr", WinExist("ahk_class Shell_TrayWnd"), APPBARDATA, A_PtrSize)
    NumPut("UInt", hide ? ABS_AUTOHIDE : ABS_ALWAYSONTOP, APPBARDATA, size - A_PtrSize)
    DllCall("Shell32\SHAppBarMessage", "UInt", ABM_SETSTATE, "Ptr", APPBARDATA)
}
I don't understand the function of lines 7 or 4. The ? ... : in 7 is shorthand for an if statement, I think. So is it saying that if the value of hide is non-zero then put the value of ABS_AUTOHIDE, otherwise put the value of ABS_ALWAYSONTOP? By lines 3 and 4, isn't the value of hide always !0? Does that stand for an unspecified non-zero value?

Rohwedder
Posts: 7929
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Help understanding code to hide taskbar

Post by Rohwedder » 08 Dec 2024, 05:49

Hallo,
test it yourself.
The tooltip shows the respective value of the variable hide:

Code: Select all

#Requires AutoHotkey v2.0
q::HideShowTaskbar()
HideShowTaskbar() {
    static ABM_SETSTATE := 0xA, ABS_AUTOHIDE := 0x1, ABS_ALWAYSONTOP := 0x2
    static hide := 0
    ToolTip hide := !hide
    APPBARDATA := Buffer(size := 2*A_PtrSize + 2*4 + 16 + A_PtrSize, 0)
    NumPut("UInt", size, APPBARDATA), NumPut("Ptr", WinExist("ahk_class Shell_TrayWnd"), APPBARDATA, A_PtrSize)
    NumPut("UInt", hide ? ABS_AUTOHIDE : ABS_ALWAYSONTOP, APPBARDATA, size - A_PtrSize)
    DllCall("Shell32\SHAppBarMessage", "UInt", ABM_SETSTATE, "Ptr", APPBARDATA)
}

jmorr
Posts: 40
Joined: 23 Mar 2022, 15:26

Re: Help understanding code to hide taskbar

Post by jmorr » 08 Dec 2024, 07:01

Thanks, but it doesn't run in v1. If the v2 functions are taken out the tooltip just says hide := !hide.

Rohwedder
Posts: 7929
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Help understanding code to hide taskbar

Post by Rohwedder » 08 Dec 2024, 07:33

Above all, it does not run in v1 because v1 does not recognize a buffer object. This is how it works in v1:

Code: Select all

q::HideShowTaskbar()
HideShowTaskbar()
{
	static ABM_SETSTATE := 0xA, ABS_AUTOHIDE := 0x1, ABS_ALWAYSONTOP := 0x2
	static hide := 0
	ToolTip,% hide := !hide
	VarSetCapacity(APPBARDATA, size := 2*A_PtrSize + 2*4 + 16 + A_PtrSize, 0)
	NumPut(size, APPBARDATA), NumPut(WinExist("ahk_class Shell_TrayWnd"), APPBARDATA, A_PtrSize)
	NumPut(hide ? ABS_AUTOHIDE : ABS_ALWAYSONTOP, APPBARDATA, size - A_PtrSize)
	DllCall("Shell32\SHAppBarMessage", UInt, ABM_SETSTATE, Ptr, &APPBARDATA)
}

jmorr
Posts: 40
Joined: 23 Mar 2022, 15:26

Re: Help understanding code to hide taskbar

Post by jmorr » 08 Dec 2024, 09:25

I see, thanks very much. So it was really the "static" that I wasn't understanding. hide is not reset to 0 on each call but is initialized at 0. !hide treats it as boolean so the value flipflops between 0 and 1, but starting at 1 on the first call, meaning the function is assuming autohide is off to start with. So if I want to adapt this as an on or off function (rather than a toggle), I can either pass a value to it or separate the code out into two different functions. In the meantime I have found the documentation for the "ternary operator" so I understand how this works now. Thanks again.

Post Reply

Return to “Ask for Help (v1)”