18 February, 2007

How to remove an application button from the taskbar in Delphi?

This is a question which each Delphi programmer ask himself at least once in his life. If you use Google or any other search engine you will easily find this solution:

1 procedure TForm1.CreateParams(var Params: TCreateParams);
2 begin
3 inherited CreateParams(Params);
4 with Params do begin
5 ExStyle := ExStyle or WS_EX_TOPMOST;
6 WndParent := GetDesktopwindow;
7 end;
8 end;

So now you definitely have a question: "If it is so easy to find a solution then why I am talking about this?" An answer is simple. I have merely encountered a situation when approach which has been shown above is not suitable and decided to share my solution of this case with you.

Well what is the intention of the problem? According to the MSDN if you add WS_EX_TOOLWINDOW to the window ExStyle then window will act like this:


Creates a tool window; that is, a window intended to be used as a floating toolbar. A tool window has a title bar that is shorter than a normal title bar, and the window title is drawn using a smaller font. A tool window does not appear in the taskbar or in the dialog that appears when the user presses ALT+TAB. If a tool window has a system menu, its icon is not displayed on the title bar. However, you can display the system menu by right-clicking or by typing ALT+SPACE.

But my client asked me to remove the button from the taskbar and leave the ability to see the window in the ALT+TAB dialog. Thus I had to refuse from above method and create my own. The solution in principle is not hard. Here is the list of steps you should do if you want to achieve an appropriate behavior:

  1. Set FormStyle property of your form to fsStayOnTop (this helps us to hold our window on top of the rest windows of our application);
  2. Write next piece of code:
 1 type
2 TfrmSamples = class(TForm)
3 { ... }
4 public
5 procedure FormDeactivate(Sender: TObject);
6 { Public declarations }
7 end;
9 { ... }
11 procedure TfrmSamples.FormCreate(Sender: TObject);
12 begin
13 { force floating window to be on top }
14 Application.OnDeactivate := FormDeactivate;
15 end;
17 procedure TfrmSamples.FormShow(Sender: TObject);
18 begin
19 ShowWindow(Application.Handle, SW_HIDE);
20 end;
22 procedure TfrmSamples.FormDeactivate;
23 begin
24 Application.BringToFront;
25 end;

According to the Borland help TApplication.OnDeactivate event occurs when an application becomes inactive therefore if we want to keep our window above all other applications (windows) then this is exec place to act.

That's all! Quick and easy.

If you have some other solution of this task please post them in the comments.

1 comment:

Anonymous said...

new software info at tricks4me.com
nice info about hide Running Programs from taskbar