0

I am a delphi learner. I wish to hide my Delphi application from Taskbar and Alt+Tab. So I have defined the following codes :

unit KoushikHalder01;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

type
  TMainForm = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  MainForm: TMainForm;

implementation

{$R *.dfm}

procedure TMainForm.FormActivate(Sender: TObject);
begin
  ShowWindow(Application.Handle, SW_HIDE);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  ShowWindow(Application.Handle, SW_HIDE);
  SetWindowLong(Application.Handle, GWL_EXSTYLE,
  GetWindowLong(Application.Handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW);
  ShowWindow(Application.Handle, SW_SHOW);
end;

procedure TMainForm.FormShow(Sender: TObject);
begin
  ShowWindow(Application.Handle, SW_HIDE);
end;

end.

and

program KoushikHalder;

uses
  Vcl.Forms,
  KoushikHalder01 in 'KoushikHalder01.pas' {MainForm};

{$R *.res}

begin
  Application.Initialize;
  Application.MainFormOnTaskbar := false;
  Application.CreateForm(TMainForm, MainForm);
  Application.Run;
end.

It works for the first time only. But there is some problems as follows :

  1. Application is vissible in Alt+Tab.
  2. If I minimize the application and again restore it the application is still on taskbar.
  3. When the application is vissible on Taskbar, it shows the name of the compiled exe file name but not the caption of the MaimForm. Please help me.
TLama
  • 75,147
  • 17
  • 214
  • 392
Rubi Halder
  • 583
  • 3
  • 12
  • 24
  • Why are you hiding and then showing in in `OnCreate` and hiding it again in both `FormShow` and `FormActivate`? It's not at all clear what it is you're trying to do here. Also, the whole `GetWindowLong/SetWindowLong` thing can be avoided by just setting the `TForm.BorderStyle` to `bsToolWindow` in the Object Inspector at designtime, and you can just set the `TForm.Visible` property there also to hide/show the window. – Ken White Aug 08 '12 at 17:06
  • Also, this code may or may not work depending on the value of the `TApplication.ShowMainFormOnTaskbar` property. The code is manipulating the `TApplication` window, which will only work if `ShowMainFormOnTaskbar` is false. When it is true instead, you have to manipulate the `TForm` window instead. `ShowMainFormOnTaskbar` determines which window is actually on the Taskbar. Starting in Vista, putting the `TApplication` window on the taskbar does not work so well anymore, so the `TForm` window has to be put on the taskbar instead. – Remy Lebeau Aug 08 '12 at 18:51
  • Actually I have found the same solution in [this **Forum**](http://stackoverflow.com/questions/681621/hide-the-main-form-in-a-delphi-2009-application). And tried the same. Please provide a complete solution. – Rubi Halder Aug 08 '12 at 19:34
  • 4
    @Rubi: Please stop asking for *a complete solution*. This is not a "please do my work for me and give me a full application" site. If you want someone to write an application for you, hire a contractor to do so. This is a site to ask specific, short, and clear questions and get help finding answers. It's not a code-writing service. – Ken White Aug 08 '12 at 20:33
  • *it shows the name of the compiled exe file name but not the caption of the MaimForm* - because it should never show caption of main form. period. There is Application title, that u can set in runtime or in project properties, and that is shown on taskbar. You do those your tricks with GWL_EXSTYLE upon Application Handle, not Mainform Handle. And the same is about caption, Applicaton's one has meaning to taskbar, not main form's one. – Arioch 'The Aug 09 '12 at 12:26

1 Answers1

3

This will hide from task bar:

procedure TForm1.FormActivate(Sender: TObject);
begin
  SetWindowLong(Handle, GWL_EXSTYLE, 
                GetWindowLong(Handle, GWL_EXSTYLE) or 
                WS_EX_TOOLWINDOW and not WS_EX_APPWINDOW); 
  ShowWindow(Application.Handle, SW_HIDE);
end;

Use that code in the FormActivate event...

quicoli
  • 612
  • 5
  • 12