8

The following code works fine in Delphi 7. However, in Delphi 2009 the form does remain hidden but the button on the taskbar is now appearing.

ShowWindow(Handle, SW_HIDE);
SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW );
ShowWindow(Handle, SW_SHOW);

The above code is called in the FormCreate method.

stukelly
  • 4,257
  • 3
  • 37
  • 44
James
  • 80,725
  • 18
  • 167
  • 237
  • The form does NOT remain hidden for me in D2007 nor D2009 with your exact code. Which seems what you want for whatever reason if you put this: ShowWindow(Handle, SW_SHOW); Seemed to me you wanted to SHOW it not HIDE it. Please update and precise your question as it appears you want the opposite.. – Francesca Mar 26 '09 at 01:54
  • The above code is used to remove the form button from the taskbar pre 2009. The goal is just to get the form's taskbar button to stop appearing. I have writtin a small test app and that code seems to work fine. However, in this particular instance it seems as the it is the Applications hidden form. – James Mar 26 '09 at 09:08
  • Deleted my answer, since it didn't seem to work... – onnodb Mar 26 '09 at 10:56

2 Answers2

10

Turns out the reason we were seeing the Application window on the taskbar was a simple setting similar to stukelly's answer but not quite.

To get the main form to appear on the task bar and hide the application menu you apply:

Application.MainFormOnTaskbar := True;
Application.ShowMainForm := False;

No code behind the form create or anything required.

James
  • 80,725
  • 18
  • 167
  • 237
  • Thanks for posting your solution back on stack overflow. I thought you wanted to hide the taskbar button and the main form. – stukelly Apr 28 '09 at 08:59
  • This doesn't work if you have code in FormActivate(). The form never shows so the FormActivate() never gets called. – TheSteven Nov 21 '12 at 23:43
  • @TheSteven that makes complete sense, the form should never become active...it's hidden. If you have code in `FormActivate` for a hidden form then it's obviously the wrong place for it. – James Nov 22 '12 at 08:57
5

You need to set the ShowMainForm and MainFormOnTaskBar properties to False before the form is created.

Open your project source and set MainFormOnTaskBar and ShowMainForm to False, before the form is created.

Application.Initialize;
Application.MainFormOnTaskbar := false;
Application.ShowMainForm := false;
Application.CreateForm(TForm1, Form1);

Then on your main form add the following code to the FormActivate and FormShow events.

procedure TForm1.FormActivate(Sender: TObject);
begin
 // hide taskbar button
 ShowWindow(Application.Handle, SW_HIDE);
end;
procedure TForm1.FormShow(Sender: TObject);
begin
 // hide taskbar button
 ShowWindow(Application.Handle, SW_HIDE);
end;

I have tested with Dephi 2007 and 2009. Additional information is available here.

stukelly
  • 4,257
  • 3
  • 37
  • 44