Can forms be manipulated at runtime?

Question:
Can forms be manipulated at runtime?

Answer:
Typically, you want to have your programs visibleand have the ability to switch to them; however, there are times when youhave a background operation that you don’t want anyone messing with onceit has started; or only when you choose to let them mess with it. Theway to accomplish this is with a WinAPI call to ShowWindow.

ShowWindowis a function that allows you change a window’s appearancewith respect to Windows environment. With it, you can maximize, minimize,hide, or show windows to your heart’s content. For a more detailed explanationof 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 youcan employ ShowWindow. It demonstrates notonly the ShowWindow procedure, but also a methodology for periodicallyquerying a user for a response. You can execute a program like this onthe fly to implement a “Register Now!” screen. Here’s what todo:

  1. Start a new project
  2. Drop two BitButtons on the form.
  3. Set the ‘Kind’ property for BitBtn1 to bkRetry and set its captionto ‘Continue’
  4. Set the ‘Kind’ property for BitBtn1 to bkCancel, leaving its captionalone.
  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 thecode.
  7. Run the form. Now every 30 seconds a message will appear on thescreen, asking whether you want to show the form.
  8. Select Cancel to quit the program or select Continue to keep hidingthe 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;interfaceuses  WinTypes, WinProcs, Messages, Controls, Forms,  Dialogs, StdCtrls, Buttons, Classes, ExtCtrls;type  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    { Private declarations }  public    { Public declarations }  end;var  Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Timer1Timer(Sender: TObject);begin  {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    begin      BitBtn1.Caption := 'Continue';      BitBtn1.Update;    end;  {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);end;procedure TForm1.BitBtn1Click(Sender: TObject);begin  ShowWindow(Self.Handle, SW_HIDE);end;procedure TForm1.BitBtn2Click(Sender: TObject);begin  Close;end;procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);begin  Action := caFree;end;procedure TForm1.FormCreate(Sender: TObject);begin  BitBtn1.Caption := 'Hide';  BitBtn1.Update;end;end.

So what gets accomplished here? Once the form is hidden, it ishidden. 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 couldbe used for monitoring certain Windows events or checking the statusof a file independent of another program. Play around withit.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin

Overview

Recent Articles: