delphi.gif (306 バイト) 非ビジュアルコンポーネント


非ビジュアルコンポーネントは、実行時に表示されないコンポーネントです。

 

toach.gif (917 バイト) 非ビジュアルコンポーネントプロジェクトを作成するには

  1. 「ファイル/新規作成」を実行してプロジェクトギャラリーを表示します。
  2. コンポーネントを選ぶとコンポーネントウィザードが表示されるので、上位クラスとしてTComponentを入力します。
  3. クラス名を適切な名称に変更し、さらにユニットファイル名を適切なものに変更します。
  4. 「ユニット作成」を行うと編集ウィンドウに基本的なコンポーネントコードが作成されます。

 

toach.gif (917 バイト) 非ビジュアルコンポーネントの基本的なプログラミング

次の例はシステムメトリックを得るためのコンポーネントです。Indexプロパティで調べたい項目を指定して、Valueプロパティでその値を読み取ります。

unit SystemMetrics;

interface

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

type
  TSystemMetrics = class(TComponent)
  private
    { Private 宣言 }
	anIndex: Integer;
	aValue: Integer;
	function GetSysMetrics: Integer;
    function GetIndex: Integer;
    procedure SetIndex(index: Integer);
  protected
    { Protected 宣言 }
  public
    { Public 宣言 }
    constructor Create;
  published
    { Published 宣言 }
    property Index: Integer read GetIndex write SetIndex;
    property Value: Integer read GetSysMetrics;
  end;

procedure Register;

implementation


procedure Register;
begin
  RegisterComponents('Private', [TSystemMetrics]);
end;


constructor TSystemMetrics.Create;
begin
    anIndex := 0;
    aValue := 0;
    inherited Create;
end;

function TSystemMetrics.GetSysMetrics: Integer;
begin
   Result := aValue;
end;

function TSystemMetrics.GetIndex: Integer;
begin
   Result := anIndex;
end;

procedure TSystemMetrics.SetIndex(index: Integer);
begin
   aValue := GetSystemMetrics(index);
end;


end.

 

toach.gif (917 バイト) 非ビジュアルコンポーネントのインストール

  1. 「コンポーネント/コンポーネントのインストール」を実行してインストールダイアログを表示させます。
  2. 設定を確認してOKボタンを押してインストールを実行します。

 

toach.gif (917 バイト) 非ビジュアルコンポーネントの使用例

この例は上のTSystemMetricsを使用したフォームの例です。

unit frmTest;

interface

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

type
  TForm1 = class(TForm)
    Label1: TLabel;
    txtValue: TEdit;
    Label2: TLabel;
    ComboBox1: TComboBox;
    cmdOK: TButton;
    procedure FormCreate(Sender: TObject);
    procedure cmdOKClick(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;
  indexValue: array[0..99] of Integer;
  sysmet: TSystemMetrics;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin
    indexValue[0] := SM_CMOUSEBUTTONS;
	indexValue[1] := SM_CXBORDER;
	indexValue[2] := SM_CYBORDER;
	indexValue[3] := SM_CXCURSOR;
	indexValue[4] := SM_CYCURSOR;
	indexValue[5] := SM_CXDLGFRAME;
	indexValue[6] := SM_CYDLGFRAME;
	indexValue[7] := SM_CXICON;
	indexValue[8] := SM_CYICON;
	indexValue[9] := SM_CXSCREEN;
	indexValue[10] := SM_CYSCREEN;
	indexValue[11] := SM_CYSMCAPTION;
	indexValue[12] := SM_CYMENU;
	sysmet := TSystemMetrics.Create;
end;


procedure TForm1.cmdOKClick(Sender: TObject);
begin
    sysmet.Index := indexValue[ComboBox1.ItemIndex];
    txtValue.Text := IntToStr(sysmet.Value);
end;


end.