Marco Cantù 1998, Mastering Delphi 4

Project: ACTIVAPP.DPR


Project Structure


ACTIVAPP.DPR

program ActivApp;

uses
  Forms,
  MainF in 'MainF.pas' {Form1},
  SecondF in 'SecondF.pas' {Form2};

{$R *.RES}

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

MAINF.PAS

unit MainF;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  public
    procedure AppActiv(Sender: TObject);
    procedure AppDeActiv(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormActivate(Sender: TObject);
begin
  Label2.Caption := 'Form1 Active';
  Label2.Color := clRed;
end;

procedure TForm1.FormDeactivate(Sender: TObject);
begin
  Label2.Caption := 'Form1 Not Active';
  Label2.Color := clBtnFace;
end;

procedure TForm1.AppActiv(Sender: TObject);
begin
  Label1.Caption := 'Application Active';
  Label1.Color := clRed;
  Beep;
end;

procedure TForm1.AppDeActiv(Sender: TObject);
begin
  Label1.Caption := 'Application Not Active';
  Label1.Color := clBtnFace;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Application.OnActivate := AppActiv;
  Application.OnDeActivate := AppDeActiv;
end;

end.

SECONDF.PAS

unit SecondF;

interface

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

type
  TForm2 = class(TForm)
    Label2: TLabel;
    procedure FormActivate(Sender: TObject);
    procedure FormDeactivate(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form2: TForm2;

implementation

{$R *.DFM}

procedure TForm2.FormActivate(Sender: TObject);
begin
  Label2.Caption := 'Form2 Active';
  Label2.Color := clRed;
end;

procedure TForm2.FormDeactivate(Sender: TObject);
begin
  Label2.Caption := 'Form2 Not Active';
  Label2.Color := clBtnFace;
end;

end.

MAINF.DFM

object Form1: TForm1
  Left = 83
  Top = 175
  Width = 290
  Height = 191
  Caption = 'Form1'
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clBlack
  Font.Height = -19
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  OnActivate = FormActivate
  OnCreate = FormCreate
  OnDeactivate = FormDeactivate
  PixelsPerInch = 96
  TextHeight = 24
  object Label1: TLabel
    Left = 24
    Top = 24
    Width = 149
    Height = 24
    Caption = 'Application Active'
    Color = clRed
    ParentColor = False
  end
  object Label2: TLabel
    Left = 24
    Top = 64
    Width = 56
    Height = 24
    Caption = 'Label2'
  end
end

SECONDF.DFM

object Form2: TForm2
  Left = 385
  Top = 175
  Width = 300
  Height = 193
  Caption = 'Form2'
  Color = clBtnFace
  Font.Charset = ANSI_CHARSET
  Font.Color = clBlack
  Font.Height = -19
  Font.Name = 'MS Sans Serif'
  Font.Style = []
  OldCreateOrder = True
  Visible = True
  OnActivate = FormActivate
  OnDeactivate = FormDeactivate
  PixelsPerInch = 96
  TextHeight = 24
  object Label2: TLabel
    Left = 24
    Top = 64
    Width = 56
    Height = 24
    Caption = 'Label2'
  end
end


Copyright Marco Cantù 1998