28 July, 2006

Enable Taskbar's auto hide feature from Delphi code

Hi. Recently I was looking for an easy solution on how to enable Taskbar's auto hide and always on top features. The only thing I have found was the solution on how to find out "Is Windows taskbar's auto hide feature enabled?". Here is the solution:


uses ShellAPI;

function IsTaskbarAutoHideOn : boolean;
var
ABData : TAppBarData;
begin
ABData.cbSize := sizeof(ABData) ;
Result :=
(SHAppBarMessage(ABM_GETSTATE, ABData)
and ABS_AUTOHIDE) > 0;
end;

But as you understand this is good but not what I was looking for.
Later I have found something similar to the solution, but once again it was not proper because it was just "cheating" :). Here is it:

procedure TForm1.Button1Click(Sender: TObject);
var
hTaskbar: THandle;
begin
hTaskbar := FindWindow('Shell_TrayWnd', Nil);
ShowWindow(hTaskbar, SW_HIDE);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
hTaskbar: THandle;
begin
hTaskbar := FindWindow('Shell_TrayWnd', Nil);
ShowWindow(hTaskbar, SW_SHOWNORMAL);
end;

That's why I had to dig deep into the MSDN and write my own solution. So, take a look how I have solved the problem.

procedure TForm1.Button1Click(Sender: TObject);
var
ABData: TAppBarData;
begin
ABData.cbSize := sizeof(tappbardata);
ABData.hWnd := FindWindow('SHELL_TRAYWND', nil);
ABData.lParam := LParam(ABS_ALWAYSONTOP or ABS_AUTOHIDE);
SHAppBarMessage($0000000a, ABData);
end;

As it turned out, the main problem was to find ABM_SETSTATE constant which equal to $0000000a and not declared in Delphi.
That's it!

10 comments:

Anonymous said...

Awesome, thanks for posting.

Anonymous said...

This works great! thanks so much DelphiGeek!

we have been debating how this could be done with Group Policies or VB for this one application that goes against our standards. It requires Auto-hide to be enabled. As we know this is a user setting and doing it any other way would be very difficult.

THANK YOU!!

iSkomorokh said...

I am always happy to help!

Anonymous said...

Thanks a lot!!

Anonymous said...

very useful code...thanks.
so how do i disable it programmatically?

Anonymous said...

great! thanks. so how do i disable it programmatically?

Anonymous said...

Thanks a lot

ND2007 said...

To disable auto hide feature programmatically just set LParam parameter to 0 (zero). So the code is:
var
ABData: TAppBarData;
begin
ABData.cbSize := SizeOf(TAppBarData);
ABData.hWnd := FindWindow('SHELL_TRAYWND', nil);
ABData.lParam := LParam(0);
SHAppBarMessage($0000000a, ABData);
end;

Anonymous said...

Combine it will give:

procedure TForm1.ShowHideTaskBar(Show: Boolean);
var ABData: TAppBarData;
begin
ABData.cbSize := sizeof(tappbardata);
ABData.hWnd := FindWindow('SHELL_TRAYWND', nil);
if Show then
ABData.lParam := LParam(0)
else
ABData.lParam := LParam(ABS_ALWAYSONTOP or ABS_AUTOHIDE);
SHAppBarMessage($0000000a, ABData);
end;

Unknown said...

You can put everything in dpr and check whether autohide is on.
Pin to taskbar and use one click to switch between autohide on and off.

program AutohideTaskbar;

uses
Windows,
ShellAPI;

{$R *.res}

function IsTaskbarAutoHideOn: boolean;
var
ABData : TAppBarData;
begin
ABData.cbSize := sizeof(ABData);
Result :=(SHAppBarMessage(ABM_GETSTATE, ABData) AND ABS_AUTOHIDE) > 0;
end;

procedure SetTaskbar;
var ABData: TAppBarData;
begin
ABData.cbSize := sizeof(tappbardata);
ABData.hWnd := FindWindow('SHELL_TRAYWND', nil);
if IsTaskbarAutoHideOn then
ABData.lParam := LParam(0)
else
ABData.lParam := LParam(ABS_ALWAYSONTOP or ABS_AUTOHIDE);
SHAppBarMessage($0000000a, ABData);
end;

begin
SetTaskbar;
end.