Controlling a Form's Visibility at Runtime

Hiding a form from the TaskBar

Typically, you want to have your programs visible and have the ability to switch to them; however, there are times when you have a background operation that you don't want anyone messing with once it has started; or only when you choose to let them mess with it. The way to accomplish this is with a WinAPI call to ShowWindow.

ShowWindow is a function that allows you change a window's appearance with respect to Windows environment. With it, you can maximize, minimize, hide, or show windows to your heart's content. For a more detailed explanation of the function, check out the Delphi help under WinAPI. Search on ShowWindow, and you'll be on your way.

The program below is a simple example of how you can employ ShowWindow. It demonstrates not only the ShowWindow procedure, but also a methodology for periodically querying a user for a response. You can execute a program like this on the fly to implement a "Register Now!" screen. Here's what to do:

  1. Start a new project
  2. Drop two BitButtons on the form.
  3. Set the 'Kind' property for BitBtn1 to bkRetry and set its caption to 'Continue'
  4. Set the 'Kind' property for BitBtn1 to bkCancel, leaving its caption alone.
  5. Drop a timer onto the form, and set its 'Interval' property to 30000 (30 secs).
  6. Copy the code from the listing below into each event listed in the code.
  7. Run the form. Now every 30 seconds a message will appear on the screen, asking whether you want to show the form.
  8. Select Cancel to quit the program or select Continue to keep hiding the form.
 This unit demonstrates how you can hide a window from the task manager
 and keep the user from being able to Alt-Tab to it.
unit Hidemain;



  WinTypes, WinProcs, Messages, Controls, Forms,
  Dialogs, StdCtrls, Buttons, Classes, ExtCtrls;

  TForm1 = class(TForm)
    BitBtn1: TBitBtn;
    BitBtn2: TBitBtn;
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure BitBtn2Click(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);
    { Private declarations }
    { Public declarations }

  Form1: TForm1;

{$R *.DFM}

procedure TForm1.Timer1Timer(Sender: TObject);
  {First, check the button's label. If it's 'Hide' then
   change it to 'Continue'. First value is only for initial
   program execution}
  if (BitBtn1.Caption = 'Hide') then
      BitBtn1.Caption := 'Continue';

  {If the window's hidden, ask the user if he/she wants to show it. If
   so, show it with its default display parameters.}
  if NOT IsWindowVisible(Self.Handle) then
    if (MessageDlg('Do you want to show the hidden application?',
                  mtConfirmation, [mbYes,mbNo], 0) = mrYes) then
      ShowWindow(Self.Handle, SW_SHOWNORMAL);

procedure TForm1.BitBtn1Click(Sender: TObject);
  ShowWindow(Self.Handle, SW_HIDE);

rocedure TForm1.BitBtn2Click(Sender: TObject);

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
  Action := caFree;

procedure TForm1.FormCreate(Sender: TObject);
  BitBtn1.Caption := 'Hide';


Notice that I used another call to the WinAPI called IsWindowVisible. This function returns the visibility state of any window. Just pass its handle to the function and it will return a Boolean value: True if the window is visible; False if the window is hidden. This is a quick and painless way of getting the visibility state of a form you're interested in.

So what gets accomplished here? Once the form is hidden, it is hidden. You won't be able to Alt-Tab or Ctrl-Esc to the Task Manager. This is one of those point of no return scenarios, but it's useful. Something like this could be used for monitoring certain Windows events or checking the status of a file independent of another program. Play around with it.