お知らせ

電子会議

ライブラリ

パレット

Delphi FAQ検索

Delphi FAQ一覧

サンプル蔵





FDelphi FAQ
16番会議室「玉石混淆みんなで作るSample蔵」に寄せられたサンプル

"TCollectionの使い方の例"



D3用のサンプルです.

 任意個の要素をpublishedで保存する,
 プロパティエディタで扱う,

ための最小構成例.



unit LightBmp;

interface

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

const
  MAXCOLORNUM = 16;
type
  TColorItem = class(TCollectionItem)
  protected
    FColor: TColor;
  public
    procedure Assign(Source: TPersistent); override;
  published
    property Color: TColor read FColor write FColor;
  end;

  TColors = class(TCollection)
  protected
    FOwner: TPersistent;
    function GetOwner: TPersistent; override;
  end;

  TLightBmp = class(TComponent)
  private
    { Private 宣言 }
  protected
    { Protected 宣言 }
    FColorItems: TColors;
    procedure SetColors(AIndex: integer; Value: TColor);
    function GetColors(AIndex: integer): TColor;
  public
    { Public 宣言 }
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    procedure AddColor(AColor: TColor);
    property Colors[AIndex: integer]: TColor
             read GetColors write SetColors;
  published
    { Published 宣言 }
    property ColorItems: TColors
             read FColorItems write FColorItems;
  end;

procedure Register;

implementation

procedure TColorItem.Assign(Source: TPersistent);
begin
  FColor := (Source as TColorItem).FColor;
end;

///////////////////////////////////////////////////////////
function TColors.GetOwner: TPersistent;
begin
  Result := FOwner;
end;

///////////////////////////////////////////////////////////
constructor TLightBmp.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FColorItems := TColors.Create(TColorItem);
  FColorItems.FOwner := Self;
end;

destructor TLightBmp.Destroy;
begin
  FColorItems.Free;
  inherited Destroy;
end;

procedure TLightBmp.AddColor(AColor: TColor);
begin
  if FColorItems.Count >= MAXCOLORNUM then begin
    raise Exception.Create('もう16色そろってます');
  end else begin
    TColorItem(FColorItems.Add).Color := AColor;
  end;
end;

procedure TLightBmp.SetColors(AIndex: integer; Value: TColor);
begin
  TColorItem(FColorItems.Items[AIndex]).Color := Value;
end;

function TLightBmp.GetColors(AIndex: integer): TColor;
begin
  Result := TColorItem(FColorItems.Items[AIndex]).Color;
end;

procedure Register;
begin
  RegisterComponents('Samples', [TLightBmp]);
end;

end.
                                                  TN(CQJ01721)

Original document by TN            氏 ID:(CQJ01721)


ここにあるドキュメントは NIFTY SERVEの Delphi Users' Forum の16番会議室「玉石混淆みんなで作るSample蔵」に投稿されたサンプルです。これらのサンプルはボーランド株式会社がサポートする公式のものではありません。また、必ずしも動作が検証されているものではありません。これらのサンプルを使用したことに起因するいかなる損害も投稿者、およびフォーラムスタッフはその責めを負いません。使用者のリスクの範疇でご使用下さい。

Copyright 1996-2002 Delphi Users' Forum