How to, make an App invisible?

Does anyone know a good method of making a Delphi application invisible?

I've searched the web and have come up with several solutions which I have amalgamated as follows:

The main program source looks like this -

  RegisterServiceProcess(0, 1);
  Application.Initialize    
  Application.ShowMainForm := false
  ShowWindow(Application.Handle, SW_HIDE);
  ShowWindowAsync(Application.Handle, SW_HIDE);
  WaitForm := WaitForm.Create(Application);

  //Do something ............

  ShowWindow(Application.Handle, SW_HIDE);
  ShowWindowAsync(Application.Handle, SW_HIDE);
  WaitForm.Free;

  //Do something else .............

  Application.Run;

Also in the WaitForm's initialization section and in the OnActivate, OnShow & OnCreate events I've put:

  ShowWindow(Application.Handle, SW_HIDE);
  ShowWindowAsync(Application.Handle, SW_HIDE);

This combination produces the best results but there are two problems:

1) Very occasionaly the app becomes visible on the taskbar then dissappears.

2) If there are already a number of apps running so that the taskbar is full then all of the buttons move left to make room for the newcomer, even though it may not appear.

Thanks for any help.

Walter McKie
LVL 1
wmckieAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

rwilson032697Commented:
Here is the answer I give for this Q:

You can do it like this:

    unit Unit1;

    interface

    uses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;

    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure FormDestroy(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;

    var
      Form1: TForm1;

    implementation

    {$R *.DFM}
    const
      RSP_SIMPLE_SERVICE = 1;
      RSP_UNREGISTER_SERVICE = 0;

    function  RegisterServiceProcess(dwProcessID,dwType : DWORD) : DWORD;
    stdcall; external 'KERNEL32.DLL';

    procedure TForm1.FormDestroy(Sender: TObject);
    begin
      RegisterServiceProcess(GetCurrentProcessID,RSP_UNREGISTER_SERVICE);
    end;

    procedure TForm1.FormCreate(Sender: TObject);
    begin
      RegisterServiceProcess(GetCurrentProcessID,RSP_SIMPLE_SERVICE);
    end;

    end.

You already know this, but call it in a different way.

There is also this, which you have not listed:

create a unit ex. called RunFirst.pas, where the only conents is that the gobal variabel IsLibrary is set to true... hereby the Application thinks that it exists inside a DLL file, and will not create the icon. After this the IsLibrary variabel should be set to false in your project file (DPR).

Exampel:


unit RunFirst;



interface



implementation



initialization

  IsLibrary := True;

end.

I reset the IsLibrary just after telling the Application that it should not show my main form, like this :


begin

  Application.Initialize;

  Application.ShowMainForm := False;

  IsLibrary := False;

  ...

end;

Cheers,

Raymond.
0
florisbCommented:
ShowWindow(application.handle, SW_HIDE);
ShowWindow(FindWindow(nil, @Application.Title[1]), SW_HIDE);

should do the trick, without making it a service. RegisterService like above doesn;t work on NT I believe.

I've been asking the same here some months ago... ...do check those messages.

F.
0
wmckieAuthor Commented:
Thanks for the replies to my question. I've tried both suggestions but the two effects are still evident so the app is not truly invisible.

I've tried replacing:

  WaitForm := WaitForm.Create(Application);
  .......
  WaitForm.Free;

with:
   
  Application.CreateForm(TWaitForm, WaitForm);
  .......
  WaitForm.Release;

This change seems to improve the first of the two problems, its difficult to tell because it dosn't happen every time and the time duration that the app is visible on the taskbar varies.

The second problem hasn't changed at all.

Thanks for the advice - Walter
0
Bootstrap 4: Exploring New Features

Learn how to use and navigate the new features included in Bootstrap 4, the most popular HTML, CSS, and JavaScript framework for developing responsive, mobile-first websites.

craig_capelCommented:
Also, it does not work on Win2000 / NT5 RegisterServices that is....
0
MadshiCommented:
Try this:

begin
  SetWindowLong(Application.handle, GWL_EXSTYLE, GetWindowLong(Application.handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW);
  Application.Initialize;
  ...
end.

Regards, Madshi.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
craig_capelCommented:
where is the code i pasted?????????????? well here it is again... this is worse than yahoo (without hackers pinging it) for eating my words....

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

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

  function  RegisterServiceProcess(dwProcessID,dwType : DWORD) :
                      DWORD; stdcall; external 'KERNEL32.DLL';

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
  RegisterServiceProcess(GetCurrentProcessID,1);
end;

procedure TForm1.FormActivate(Sender: TObject);
var
  w: hwnd;
begin
 // showwindow(form1.handle,sw_hide);
 // w:=findwindow('tapplication',nil);
 // showwindow(w,sw_hide);

 {To Totally go Invisible}
end;

end.


ok thats the lazy way of doing it.....
0
bogieman_Commented:
Listening...
0
duke_nCommented:
monitoring...
0
rwilson032697Commented:
Have you tried the IsLibrary thing...

Cheers,

Raymond.
0
wmckieAuthor Commented:
Problem solved! (on Win95) The answer from madshi did the trick, the points are yours.

The main program source now looks something like this -

  RegisterServiceProcess(0, 1);
  SetWindowLong(Application.Handle,
                GWL_EXSTYLE,
                GetWindowLong(Application.handle, GWL_EXSTYLE) or WS_EX_TOOLWINDOW);
  Application.Initialize
  Application.CreateForm(TWaitForm, WaitForm);

  //Do something ............
 
  WaitForm.Release;

  //Do something else .............

  Application.Run;

  RegisterServiceProcess(0, 0);

I've also removed all the ShowWindow API calls from the form events and the initialize section.

In answer to your question Raymond, yes I did try your IsLibrary solution in various places in my project, but there was no effect.

In an old copy of The Delphi Magazine I found an answer in the Clinic which sheds some light on what's going on. It seems that whatever you design as your main application form/window Delphi always creates an application window (via the application object) which owns these forms/windows and thats what you see on the task bar.

I think that RegisterServiceProcess call removes the app from the displayable task list so that Ctrl/Alt/Del will not reveal it but the application object still creates its "managing" window for the taskbar. Using ShowWindow to hide it works but has the side effects that I've described. I think that the sometimes brief display on the taskbar is a timing effect caused by the PC doing extra work. I did at one point put the ShowWindow calls into a procedure which was called from various places that definately made things worse.

Thanks to everyone for the help - Walter
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.