delphi.gif (306 バイト) イベント型の使い方


イベントとはオブジェクトの手続きポインタ型です。イベントはコンポーネントパレットに登録されているコンポーネントならオブジェクトインスペクタのイベントタブでハンドラを定義するだけで簡単に追加できますが、TApplicationなどでは手動でコードを追加する必要があります。

次の例はTApplicationのOnMinimize(最小化イベント)とOnRestore(最小化されたアプリケーションを元にもどしたとき)に応答するイベントハンドラを追加するものです。

delphi1.gif (322 バイト) 

unit Event;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
    procedure OnMinimize(Sender: TObject);
    procedure OnRestore(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

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


procedure TForm1.OnMinimize(Sender: TObject);
begin
   Application.Title := 'MINIMIZED';
   Label1.Caption := Application.Title;
end;

procedure TForm1.OnRestore(Sender: TObject);
begin
    Application.Title := 'RESTORED';
    Label1.Caption := Application.Title;
end;

end.