具有可变数量TPictures的Delphi组件 [英] Delphi component with a variable amount of TPictures

查看:68
本文介绍了具有可变数量TPictures的Delphi组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个从TImage继承的组件,不同之处在于我可以在属性列表中分配可变数量的TPictures(而不是通过代码分配TPictures),并通过代码激活其中的一个以在其中显示.TImage.

I'm trying to create a component descending from TImage, with the difference that I can assign a variable number of TPictures in the property list (not assign the TPictures by code) and activate one of them by code to be displayed in the TImage.

如果要在属性中分配所有TPicture,则有必要设置一个属性来设置TPicture的总数(动态数组的长度),这不是问题.

It would not be a problem to have a property that sets the overall number of TPictures (length of the dynamic array), if that's necessary to have all the TPictures assignable within the properties.

unit ImageMultiStates;

interface

uses
  Vcl.Graphics, Vcl.StdCtrls, System.SysUtils, System.Classes, Vcl.Controls, Vcl.ExtCtrls, Forms;

type
  TPictures = Array of TPicture;
  TImageMultiStates = class(TImage)
  private
      FPictures: TPictures;
      procedure SetPicture(Which: Integer; APicture: TPicture);
  public
      constructor Create(AOwner: TComponent); override;
      destructor Destroy; override;
      procedure Activate(Which: Integer);
  published
    property Images: TPictures read FPictures write FPictures; default;
  end;

procedure Register;

implementation

constructor TImageMultiStates.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  for TPicture in FPictures do
    TPicture := TPicture.Create;
end;

destructor TImageMultiStates.Destroy;
var
  APicture: TPicture;
begin
  for APicture in FPictures do
    APicture.Free;
  inherited Destroy;
end;

procedure TImageMultiStates.Activate(Which: Integer);
begin
  Picture.Assign(FPictures[Which]);
end;

procedure TImageMultiStates.SetPicture(Which: Integer; APicture: TPicture);
begin // i would also like to use SetPicture instead of "write FPictures"
  FPictures[Which].Assign(APicture);
  if Which=0 then // because: First Picture will be displayed in the VCL editor
    Picture.Assign(FPictures[Which]);
end;

procedure Register;
begin
  RegisterComponents('Standard', [TImageMultiStates]);
end;

end.

我以多种方式将这段代码改掉了,但是我什么都无法真正工作.

I turned this code around in many ways, but i just can't get anything to work really.

我在组件"Image2States"中确实有同样的想法.然后我需要"Image4States",依此类推,直到我确定我绝对需要可变数量的TPictures ...

I do have this exact same idea already working in my component 'Image2States'. Then I needed 'Image4States' and so on, until I decided that I absolutely need this with a variable amount of TPictures...

推荐答案

您告诉我们您的方法行不通",(我假设您的意思是未编译"):那是由于一些语法错误,并且因为您没有使用正确的工具进行这项工作.

You told us that your approach "didn't work" (I assume you meant "didn't compile"): That is because of a few syntactical errors and because you are not using the right tool for the job.

如果您只有相同大小的图片,请考虑使用已经存在的,经过良好测试且受IDE支持的 TImageList ,不要尝试重新发明轮.

If you only have pictures of the same size, then think about using the already existing, well tested and IDE-supported TImageList and don't try to reinvent the wheel.

但是,如果您必须具有不同大小的图片列表,则使用 TObjectList ,而不要使用数组TPicture .TObjectLists允许您添加,删除,查询等对象,并且可以根据需要自动释放它们.

If, however, you must have a list of pictures of different sizes, then use a TObjectList and not an array of TPicture. TObjectLists allow you to add, remove, query, etc. objects and they can automatically free them, if you wish.

如果您的编译器支持泛型,则包括 System.Generics.Collections 并使用 TObjectList< TPicture> 来管理图片.这样,您就不必强制转换为 TPicture ,因为泛型列表是类型安全的.

If your compiler supports generics, then include System.Generics.Collections and use a TObjectList<TPicture> to manage your pictures. That way, you don't have to cast to TPicture, because the generics lists are type safe.

如果它不支持它们,则包括单元 Contnrs 并使用 TObjectList .从该列表中读取时,您将必须使用 as 进行投射,即 as TPicture ,但是否则您可以做类似的事情.

If it doesn't support them, include the unit Contnrs and use a TObjectList. When reading from that list, you will have to cast using as, i.e. as TPicture, but otherwise you can do similar things.

您所输入的名称使我认为您只需为某个控件使用多个状态.在那种情况下,我认为 TImageList 是完成这项工作的最佳工具(并且已经是具有类似需求的其他控件的工具),因此无需自己制作一个.但是,如果您要自己制作,请不要使用动态数组,也不要像在FPictures中使用Tcode的那样产生循环..帮自己一个忙,并使用对象列表.

The name of your type makes me think you only need multiple states for a certain control. In that case, I think a TImageList is the best tool for the job (and it already is for other controls with similar needs) and there is no need to make your own one. But if you want to make your own, don't use a dynamic array and don't produce loops like the one with for TPicture in FPictures do. Do yourself a favour and use an object list.

这篇关于具有可变数量TPictures的Delphi组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆