Setting the Vista Taskbar to Autohide from Delphi 2009
Banged my head against this for quite some time. What I really wanted was a fullscreen app that appeared over the taskbar. While it appears that this is simple in XP, in Vista not so much. However, setting the form’s FormStyle to fsStayOnTop and WindowState to wsMaximized in combination with the code below to turn the Taskbar’s auto hide flag on achieves what I needed, and a bit more pleasantly as well (the Taskbar animates nicely out of the way).
Here’s the code from the FormShow event handler (you’ll need ShellAPI in your ‘uses’ statement):
procedure TMainForm.FormShow(Sender: TObject);
var
AppBarData: TAppBarData;
AutoHideOn: boolean;
begin
//check if autohide on already
AppBarData.cbSize := SizeOf(AppBarData) ;
AutoHideOn :=(SHAppBarMessage(ABM_GETSTATE, AppBarData)
and ABS_AUTOHIDE) > 0;
OrigTaskBarParam := -1;
//if it isn't, then we will turn it on temporarily
if not AutoHideOn then
begin
AppBarData.hWnd := FindWindow('SHELL_TRAYWND', nil);
//we'll also save the user's taskbar settings and
//restore them to the way they had them when we close
OrigTaskBarParam := SHAppBarMessage(ABM_GETSTATE, AppBarData);
//now we'll set it the way we want it
AppBarData.lParam := LParam(ABS_ALWAYSONTOP or ABS_AUTOHIDE);
SHAppBarMessage(ABM_SETSTATE, AppBarData);
end;
end;
Now, to be nice we’re saving the user’s original taskbar settings (before we fucked around with them) in a private field (OrigTaskBarParam) of our form. We’ll use this again in the form’s destroy handler:
procedure TMainForm.FormDestroy(Sender: TObject);
var
AppBarData: TAppBarData;
begin
//if we modified this we'll set it back to the original state
if OrigTaskBarParam <> -1 then
begin
AppBarData.cbSize := SizeOf(AppBarData) ;
AppBarData.lParam := OrigTaskBarParam;
AppBarData.hWnd := FindWindow('SHELL_TRAYWND', nil);
SHAppBarMessage(ABM_SETSTATE, AppBarData);
end;
end;
