Lazarus|delphi中,自定义控件中实现items列表操作【最小框架】

unit Dev_FA_Motion;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, Protocol_Modbus;

type
  { TMotionItem }

  TMotionItem = class(TCollectionItem)
  private
    FRepeatCount: Cardinal;
    procedure SetRepeatCount(AValue: Cardinal);
  protected
  //  function GetDisplayName: string; override;
  public
    constructor Create(ACollection: TCollection); override;
    destructor Destroy; override;
  public
    procedure Assign(Source: TPersistent); override;
  published

    property RepeatCount: Cardinal read FRepeatCount write SetRepeatCount default 1;

  end;

  TDev_FA_Motion = class;

  TDeviceEnumerator = class(TCollectionEnumerator)
  public
    function GetCurrent: TMotionItem;
    property Current: TMotionItem read GetCurrent;
  end;

  { TMotionList }

  TMotionList = class(TCollection)
  private
    FOwner: TDev_FA_Motion;
    function GetDevice(AIndex: Integer): TMotionItem;
  protected
    function GetOwner: TPersistent; override;
  public
    constructor Create(AOwner: TDev_FA_Motion);
    function GetEnumerator: TDeviceEnumerator; inline;
    property Device[AIndex: Integer]: TMotionItem read GetDevice; default;
  end;

  { TDev_FA_Motion }

  TDev_FA_Motion = class(TProtocol_Modbus)
  private
    FDevices: TMotionList;
    procedure SetDevices(AValue: TMotionList);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  public
    function Add: TMotionItem;
  published
    property Devices: TMotionList read FDevices write SetDevices;
  end;

procedure Register;

implementation

procedure Register;
begin
  {$I dev_fa_motion_icon.lrs}
  RegisterComponents('MyFun',[TDev_FA_Motion]);
end;

{ TDeviceEnumerator }

function TDeviceEnumerator.GetCurrent: TMotionItem;
begin
  Result := TMotionItem(inherited GetCurrent);
end;

{ TMotionItem }

procedure TMotionItem.Assign(Source: TPersistent);
begin
  inherited Assign(Source);
end;

constructor TMotionItem.Create(ACollection: TCollection);
begin
  inherited Create(ACollection);
  FRepeatCount := 1;
end;

destructor TMotionItem.Destroy;
begin
  inherited Destroy;
end;

procedure TMotionItem.SetRepeatCount(AValue: Cardinal);
begin
  if FRepeatCount = AValue then exit;
  FRepeatCount := AValue;
end;


{ TMotionList }

constructor TMotionList.Create(AOwner: TDev_FA_Motion);
begin
  inherited Create(TMotionItem);
  FOwner := AOwner;
end;

function TMotionList.GetEnumerator: TDeviceEnumerator;
begin
  Result := TDeviceEnumerator.Create(Self);
end;

function TMotionList.GetOwner: TPersistent;
begin
  Result := FOwner;
end;

function TMotionList.GetDevice(AIndex: Integer): TMotionItem;
begin
  Result := Items[AIndex] as TMotionItem;
end;

{ TDev_FA_Motion }

function TDev_FA_Motion.Add: TMotionItem;
begin
  Result := TMotionItem(FDevices.Add);
end;

constructor TDev_FA_Motion.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  FDevices := TMotionList.Create(Self);
end;

destructor TDev_FA_Motion.Destroy;
begin
  FreeAndNil(FDevices);
  inherited Destroy;
end;

procedure TDev_FA_Motion.SetDevices(AValue: TMotionList);
begin
  if FDevices = AValue then exit;
  FDevices := AValue;
end;

end.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lijinjie

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值