【发布时间】:2012-04-10 06:52:50
【问题描述】:
我的问题与这里的想法相似:Replacing a component class in delphi.
但我需要根据需要更改特定组件类。
这是一些伪演示代码:
unit Unit1;
TForm1 = class(TForm)
ImageList1: TImageList;
ImageList2: TImageList;
private
ImageList3: TImageList;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
ImageList3 := TImageList.Create(Self);
// all instances of TImageList run as usual
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
Unit2.MakeSuperImageList(ImageList2);
Unit2.MakeSuperImageList(ImageList3);
// from now on ONLY ImageList2 and ImageList3 are TSuperImageList
// ImageList1 is unchanged
end;
unit Unit2;
type
TSuperImageList = class(Controls.TImageList)
protected
procedure DoDraw(Index: Integer; Canvas: TCanvas; X, Y: Integer;
Style: Cardinal; Enabled: Boolean = True); override;
end;
procedure TSuperImageList.DoDraw(Index: Integer; Canvas: TCanvas; X, Y: Integer;
Style: Cardinal; Enabled: Boolean = True);
var
Icon: TIcon;
begin
Icon := TIcon.Create;
try
Self.GetIcon(Index, Icon);
Canvas.Draw(X, Y, Icon);
finally
Icon.Free;
end;
end;
procedure MakeSuperImageList(ImageList: TImageList);
begin
// TImageList -> TSuperImageList
end;
注意:为了清楚起见,我想更改一些实例,但不是全部,所以interposer class 不会这样做。
【问题讨论】:
-
您是否考虑过使用插入器类?
-
@David:这将在创建表单时更改表单上的所有实例。他想做的是改变一些个实例,但不是全部,以后再按需改变。
-
@DavidHeffernan,我会编辑 Q,这样会更清楚。
-
@kobik 我仍然认为插入器是正确的解决方案。您只需要以有区别的方式切换行为。查看我的最新更新。
-
只要你需要扩展(替换)的方法都是虚拟的,继承自 ImageList 的东西是不是不够好?