Back
 

TApplication



 
TApplication encapsulates your Windows application and performs many things that help your software work the way its supposed to in the Windows environment.

In order for you to use the Application object you must include the Forms unit it in your uses clause.

The global object Application is of the class TApplication and is defined by the VCL in the Forms unit:

You may not know it but when you create a new application the windows created by the application are
 

  • A main window which has the Form1 title and is of the class TForm1.
  • If you had added a button (TButton) to the form then this button would be a child of Form1 and a child window of the class TButton.
  • Also a new hidden main window which is the Application window and it is titled Project1. This window is of the class TApplication.


You cannot see the TApplication window as it is hidden but you will notice that the taskbar icon (Some people call it a taskbar button) is called Project1 and not Form1, which is what the main form is called.
Remember the main form is the main program form and other forms are additional forms such as Form 2 and Form 3 etc.

Application Title:

You can change the Application title this way:

procedure TForm1.FormCreate(Sender: TObject);
begin
    Application.Title := 'My Program';
end;

or

begin
    Application.Initialize;
    Application.Title := 'KiwiWordPro';
    Application.CreateForm(TMainForm, MainForm);
    Application.Run;
end.

With your application loaded into Delphi you can then go to Project | Options (Shift + Ctrl + F11) and then click on the Application tab and then change the Application Title there. Note if you change the Application Title in the code like in  both examples above the Application Title text in Project | Options will not show.

If you want the form and taskbar icon to have the same title you can use this code:

procedure TForm1.FormCreate(Sender: TObject);
begin
    Application.Title := Form1.Caption;
end;

Application.Active:

The Active property will return true if your application currently has input focus.

This can be very helpful if you want to know if your application has focus or another application does.

An example is if you want to do something when your application has focus, but not do anything when your application does not have focus.

Application.ExeName:

This can return a string that identifies the fully qualified path of your application.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := Application.ExeName;
end;

Application.ComponentCount:

A simple example of using Applicatin.ComponentCount to list class names:

procedure TForm1.Button1Click(Sender: TObject);
var I : Integer;
begin
  for I := 0 to Application.ComponentCount - 1 do
    Memo1.Lines.Add(Application.Components[I].ClassName    );
end;

Application Hints:

To disable all the fly-over hints in your application you can set ShowHint to false.

procedure TForm1.Button2Click(Sender: TObject);
begin
    Application.ShowHint := False;
end;

Here are some application hint examples:

This will change the hints color

Application.HintColor := clRed;

---

How you can display hints in the statusbar

First add a TStatusbar component to your form and
then set the StatusBar1 ShowHint property to True then
add this code:

procedure TForm1.DisplayHint(Sender: TObject);
begin
    StatusBar1.SimpleText := Application.Hint;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
    Application.OnHint := DisplayHint;
end;
---
HintHidePause and HintPause:

These properties control the period of time for the display of fly-over
hints.

Delphi waits HintHidePause milliseconds before it hides the hint window.

procedure TForm1.FormCreate(Sender: TObject);
begin
    Application.HintHidePause := 2000;
end;

When you use HintHidePause, you use it to specify a wait time in milliseconds,
this should be different from the default value of 2 and a half seconds.

What Delphi does is waits the HintPause milliseconds before it displays the
hint window.

Here is an example of HintPause, which is in 200 milliseconds:

Application.HintPause := 200; 

Application.Icon:

Changing an Applications Icon:

procedure TForm1.Button1Click(Sender: TObject);
begin
  if OpenDialog1.Execute then
    begin
      Application.Icon.LoadFromFile(OpenDialog1.FileName);
    end;
end;

Application.ProcessMessages:

Example:
This example comes from Greg Liefs "SUPPRESSING MOUSE AND KEYBOARD EVENTS".

begin
   Enabled := False ;
   Form2.Show ;
   Form2.Update ;
   For c := 1 to 1000 do begin
   Form2.LMDLabelX.Caption := IntToStr(c);
   Application.ProcessMessages ;
   end ;
   Form2.Close;
   Enabled := True ;
   SetFocus ;
end;

Greg Liefs "SUPPRESSING MOUSE AND KEYBOARD EVENTS":

http://www.sandbrooksoftware.com/DPSC/Tips/GregLief/gl_q&a64.html

Application.Minimize:

(Minimize the application to the TaskBar)

procedure TForm1.Button2Click(Sender: TObject);
begin
    Application.Minimize;
end;

Application.Terminate:

Terminate will end your applications execution, in other words shut it down, what it does is it sets the Terminate property of the TApplication object to true.

Terminate waits until all event handlers and any other processing to be completed first.

procedure TForm1.Button2Click(Sender: TObject);
begin
    Application.Terminate;
end;

Application.Run:

You do not need to call the Application.Run method because Delphi automatically puts it in the main program block in the project. You should never call Application.Create and Application.Destroy either.

Example:

program DPSCSupp;

uses Forms, Unit1 in 'Unit1.pas' {Form1},

{$R *.RES}

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Application.Run;
end;

Application.CreateForm:

Is used to create the main form of the application as well as any auto-created forms.

Application.OnDeactivate:

This happens when an application becomes inactive.

Hiding your application when its not being used example:

Here is some code that will hide your application when the user clicks off your application and it loses focus.

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, StdCtrls, Buttons, ExtDlgs, JPEG, ComCtrls, Menus;

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

var Form1: TForm1; 
implementation {$R *.DFM}

procedure TForm1.HideMyForm(Sender: TObject);
begin
  Application.Minimize;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnDeactivate := HideMyForm;
end;

end.

Run the program and make sure it is not running at full screen and click on an icon on your desktop or on another program. You should see this program minimize.

TApplicationEvents:

To handle the events of the Application object you can use the component designed for this purpose, TApplicationEvents,  this is available in Delphi 5 and Delphi 6.
TApplicationEvents is used to intercept the events of the global Application object.

These are its events available in the Object Inspector:
 

  • OnActionExecute.
  • OnActionUpdate.
  • OnActivate.
  • OnDeactivate.
  • OnException.
  • OnHelp.
  • OnHint.
  • OnIdle.
  • OnMessage.
  • OnMinimize.
  • OnRestore.
  • OnShortCut.
  • OnShowHint.

Back