非ビジュアルコンポーネント
非ビジュアルコンポーネントは、実行時に表示されないコンポーネントです。
非ビジュアルコンポーネントプロジェクトを作成するには
非ビジュアルコンポーネントの基本的なプログラミング
例
次の例はシステムメトリックを得るためのコンポーネントです。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.
非ビジュアルコンポーネントのインストール
非ビジュアルコンポーネントの使用例
この例は上の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.