2.20.2014 How to Show / Hide the Windows Taskbar with Lazarus

If you want to build some uncommon software which needs hiding the taskbar then check out this article.


Windows Taskbar is the bar which sits at the very bottom of the screen. This bar has the Start orb (or the Start Button) and some of our favorite program icons and some other Tray icons on the right as well. It is a prominent feature of a windows desktop.

There are times when we need to hide it, and may be show it again. For example, if you are doing a fullscreen and for some reason the taskbar keeps popping and annoying you or if you just want to have some fun, hiding the taskbar is a nice thing to do.

In this short tutorial, we'll see how to hide the taskbar and show it again. For hiding and showing a window which is not from our project, we will have to use the Windows API. And basically every form, window or component we see in the screen is a "window" to the Windows API. When a form, window or component appears in the screen, windows gives it a special ID to it. It is called HWND. An HWND is like a roll number. As each student in a class have one unique number, HWND is just like that. We can use this HWND to identify a window. We'll learn about that in a minute.

The code

First of all we are using Windows API. So we will have to add windows unit to our uses clause:
uses
  ..., ..., windows;

Hiding the taskbar (including the start orb)

The code for hiding the Taskbar is:
ShowWindow(
    FindWindowEx(0, 0, MAKEINTATOM($C017), 'Start'),
    SW_HIDE);
ShowWindow(FindWindow('Shell_TrayWnd', nil), SW_HIDE);

The first ShowWindow command hides the start orb. ShowWindow has the following syntax:
function ShowWindow(hWnd:HWND; nCmdShow:longint):WINBOOL;

The first parameter is the HWND that we want to show or hide. We don't have a fixed HWND for Start orb. So we need to find out the HWND of the start orb. The "FindWindowEx(0, 0, MAKEINTATOM($C017), 'Start')" part has been used to find the HWND of the start orb.

And then the second parameter in the ShowWindow function is the nCmdShow flag. (A flag is usually a number to say what we want to do with our command. We can sometimes combine multiple flags.) The interesting thing is that ShowWindow is used for both showing and hiding a window. Look at the flags to further understand:

SW_HIDE = 0;
SW_MAXIMIZE = 3;
SW_MINIMIZE = 6;
SW_NORMAL = 1;
SW_RESTORE = 9;
SW_SHOW = 5;
SW_SHOWDEFAULT = 10;
SW_SHOWMAXIMIZED = 3;
SW_SHOWMINIMIZED = 2;
SW_SHOWMINNOACTIVE = 7;
SW_SHOWNA = 8;
SW_SHOWNOACTIVATE = 4;
SW_SHOWNORMAL = 1;
WPF_RESTORETOMAXIMIZED = 2;
WPF_SETMINPOSITION = 1;
(taken from defines.inc. You can find further explanation of the flags in this MSDN page)

So, you can see that there is SW_HIDE to hide a window or SW_SHOW to show a hidden window.

The second ShowWindow command is similar and more easier to understand:
ShowWindow(FindWindow('Shell_TrayWnd', nil), SW_HIDE);

It is for the rest of the Taskbar. Taskbar has a class name "Shell_TrayWnd". We can find out the Taskbar's HWND through FindWindow function. We give FindWindow the Class name. It finds the HWND for us. Then we use ShowWindow to hide the window (or our taskbar).

We had to hide both Task bar and start orb because if we hide the taskbar, the start orb doesn't hide. It is a separate window. So we had to write another line for that.

It is always a good idea to show the taskbar once your program exits. Because it you don't do so, when it exits, the user will be left with no taskbar. That's what we are going to see next.

Showing the Taskbar (including start orb)

The code for showing the Taskbar is:
ShowWindow(FindWindow('Shell_TrayWnd', nil), SW_SHOW);
ShowWindow(
      FindWindowEx(0, 0, MAKEINTATOM($C017), 'Start'),
      SW_SHOW);

We do the same thing but instead of SW_HIDE, we use SW_SHOW.

I have tested the code in Windows 7 (Ultimate). It should work for other windows versions as well.

Tutorial

Start Lazarus.

Create a New Application Project (Project -> New Project -> Application -> OK).

Draw a TButton on the form. It wil be named Button1 automatically. Set it's caption to "Hide Taskbar". Size and position it as you like.

Hide Taskbar Button in lazarus, windows

Now draw another TButton. (Or you can copy and paste Button1 as another option.) This button should automatically be named Button2. Set it's caption to "Show Taskbar". Size and position it as you like.


Show Taskbar Button in lazarus, windows

You can change the name of the buttons as you like but then change the code accordingly.

Now press F12 to switch to code view. Add the windows unit to the uses clause:

uses
  Classes, SysUtils, ..., ...,
  windows;

Press F12 to switch to Form view again.

Double click Button1 and enter the following code:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowWindow(
      FindWindowEx(0, 0, MAKEINTATOM($C017), 'Start'),
      SW_HIDE);
  ShowWindow(FindWindow('Shell_TrayWnd', nil), SW_HIDE);
end;

Switch to Form view (F12). Double click Button2 and enter the following code:

procedure TForm1.Button2Click(Sender: TObject);
begin
  ShowWindow(FindWindow('Shell_TrayWnd', nil), SW_SHOW);
  ShowWindow(
      FindWindowEx(0, 0, MAKEINTATOM($C017), 'Start'),
      SW_SHOW);
end;

Now Run the project (Run -> Run or F9).

show hide taskbar lazarus project

Click both the buttons to show and hide task bar.

Download Sample Code ZIP

You can download the above example tutorial project's source code from here: https://db.tt/Nca39jNf
Size: 522 KB
The package contains compiled executable EXE file.

Ref: http://embarcadero.newsgroups.archived.at/public.delphi.nativeapi/201008/1008113955.html

7 comments:

Martin Nesvadba said...

uses
..... ShellApi, windows;

procedure ShowTrayWnd(Value : boolean);
const
ABM_SETSTATE = $000000A;
ABS_AUTOHIDE = 1;
ABS_ALWAYSONTOP = 2;
var
data: TAppBarData;
begin
if Value then
ShowWindow(FindWindow('Shell_TrayWnd', Nil), SW_HIDE)
else
ShowWindow(FindWindow('Shell_TrayWnd', Nil), SW_SHOWNORMAL);

if (Win32Platform = VER_PLATFORM_WIN32_NT) then
begin
if Value then
begin
data.cbSize := SizeOf(TAppBarData);
data.hWnd := FindWindow('Shell_TrayWnd', '');
data.lParam := ABS_AUTOHIDE;
SHAppBarMessage(ABM_SETSTATE, @data);
end
else
begin
data.cbSize := SizeOf(TAppBarData);
data.hWnd := FindWindow('Shell_TrayWnd', '');
data.lParam := ABS_ALWAYSONTOP;
SHAppBarMessage(ABM_SETSTATE, @data);
end;
end;
end;

Carmelo Privitera said...

Hi!
I want to know how to hide application icon from taskbar and, of course, i have seeing your blog!
But there is a problem. I have copy your code in my application and when run it, the task bar was hidden completely, including the start button of windows!! How to solve?
Regards

Adnan Shameem said...

@Carmelo

Well, first of all, Button1 code hides the taskbar and Button2 code shows it again. You need Button2 code to get your taskbar back. Please follow the whole tutorial first.

If you want to hide/show the apps list in the taskbar, you will have to use:


var h:HWND;
begin
// get the HWND of Task list
h:= FindWindowEx (FindWindow ('Shell_TrayWnd', nil), 0, 'ReBarWindow32', nil);
h:= FindWindowEx (h, 0, 'MSTaskSwWClass', nil);
h:= FindWindowEx (h, 0, 'MSTaskListWClass', nil);

// to hide
ShowWindow(h, SW_HIDE);

// keep hidden 2 seconds...
sleep(2000);

//to show
ShowWindow(h, SW_SHOW);
end;


This will keep the taskbar buttons hidden for 2 seconds. Change/use the code as you like.

The code is for Windows 7. For windows XP you may need to search "ToolbarWindow32" instead of "MSTaskListWClass".

Regards
Adnan

Ref:
Link 1
Link 2

Andreas said...

Hi,
first of all: This was a great work and it help me a lot to do the job. Many thanks for that. What I do not understand:
Instead of your code I used
"ShowWindow(FindWindow('Shell_TrayWnd',nil), SW_HIDE);"
to hide the taskbar and it worked perfect.
But when I want to use my code
"ShowWindow(FindWindow('Shell_TrayWnd',nil),SW_SHOWNA);"
(I have tried this also with only SW_SHOW)
to show the taskbar it does not.
What is wrong with that and why did you call ShowWindow a second time using FindWindowEx ?

Anyway: Now it worked fine!!
By the way: Why is the comment area in this color? Comments a very hard to read.
Regards Andreas!

Adnan Shameem said...

Hello Andreas

Thanks for reading.
I tried the code to show the taskbar from the article. It works fine. I tried the same code in the article. If you are eager you can try the project download file to get a working copy, then experiment with it.

"What is wrong with that and why did you call ShowWindow a second time using FindWindowEx ?"
The second ShowWindow is for the Start button. The taskbar and the start button are separate windows. If you hide only one, the other one will be visible. So you'll have to hide both of them while hiding, by using 2 ShowWindow calls. For showing, you'll have to make them both to be shown, with 2 ShowWindow calls.

"Why is the comment area in this color? Comments a very hard to read."
Thanks for noticing! I was so caught up with writing tutorials that I forgot about the comment section! I'll surely try to fix that after my exam is over.

Regards
Adnan

Silvia Jacinto said...

I really love your blog there's a lot to share. Keep it up.Visit my site too.

n8fan.net

www.n8fan.net

Aakash Patel said...

thanks 4 sharing really nice info for hide Running Programs from taskbar nice software at on http://tricks4me.com

 
Copyright 2013 LazPlanet
Carbon 12 Blogger template by Blogger Bits. Supported by Bloggermint