我想将TToolBar子类化为另一个名为MyTToolBar的类,以便我可以覆盖一个方法。我是Delphi的新手,但经过两个小时尝试各种方法后,我无法使用MyTToolBar代替TToolBar。我不能成为第一个想要在可视组件类上覆盖方法的人。
我来自Xcode背景,其中可视化组件的子类化很容易。您创建父类的子类(例如,MySubclass)(例如,MySuperClass),然后只需在Xcode的Interface Builder视图中分配子类。子类被自动识别和使用。
为什么我在Delphi RAD Studio XE3中似乎无法做到这一点?
将TToolBar添加到TForm后,似乎无法更改类。我尝试了Object Inspector以及.PAS源代码文件。如果我更改.PAS文件中的类,我会收到一条错误消息,指出工具栏“应该是Vcl.ComCtrls.TToolBar类型,但是声明为MyTToolbar。更正声明?”这看起来很傻......
哦,我还使用了新的组件向导选择:文件 - >新建 - >其他 - > Delphi项目 - > Delphi文件 - >组件。我选择MyTToolBar的祖先作为TToolBar并告诉它在'Samples'调色板页面中注册。但是,它不会出现在“样本”页面中。
与XCode方法最接近的是使用Delphi中的“内插器”类。基本上,您不会更改IDE为标准TToolBar
用法创建的代码。您改为声明一个派生自标准TToolBar
组件的新类,但也命名为TToolBar
,并且在声明标准TToolBar
之后使编译器可以看到它。无论编辑器最后一次看到哪个TToolBar
类,都是实际的类类型,只要流式传输TForm
DFM,它就会被实例化。
您可以通过以下两种不同方式之一在标准TToolBar
类之后使编译器看到您的自定义TToolBar
类:
TToolBar
类相同的单元中声明自定义TForm
类:
unit MyForm;
interface
uses
..., Vcl.ComCtrls, ...;
type
TToolBar = class(Vcl.ComCtrls.TToolBar)
// override what you need...
end;
TMyForm = class(TForm)
ToolBar1: TToolBar; // <-- do not change this!
...
end;
implementation
// implement custom TToolBar as needed...
// normal TForm implementation code as needed ...
end.
TToolBar
类,然后在添加TForm
单元后将其添加到uses
单元的ComCtrls
子句中:
unit MyToolBar;
interface
uses
..., Vcl.ComCtrls;
type
TToolBar = class(Vcl.ComCtrls.TToolBar)
// override what you need...
end;
implementation
// implement custom TToolBar as needed...
end.
。
unit MyForm;
interface
uses
..., Vcl.ComCtrls, ..., MyToolBar;
type
TMyForm = class(TForm)
ToolBar1: TToolBar; // <- do not change this!
...
end;
implementation
// normal TForm implementation code as needed ...
end.
此方法仅适用于每个项目。如果您想在多个项目中使用自定义TToolBar
类,那么最好将它安装到IDE中,如@KenWhite描述的那样,并使用它而不是标准的TToolBar
。回去命名为TMyToolBar
(或其他),不要再将其命名为TToolBar
,因为它不会被用作插入器。确保在其项目选项中将包标记为“运行时和设计时”(创建单独的仅运行时和设计时包在本讨论的范围之外)。 TMyToolBar
将在设计时提供给你像任何其他组件一样放在你的TForm
上。如果不是,那么您没有正确设置它。
要更改现有表单上的组件,它实际上必须是IDE可以在设计时创建实例的组件。这意味着IDE必须首先了解它。
执行此操作的方法是创建自己的后代组件,并实际将其安装到IDE中的设计时包中。然后,您可以将其放在表单而不是标准版本上,或者通过一些工作将其替换为现有表单。 (你必须首先创建你的版本并安装它。)
从IDE菜单中的File->New->Package (Delphi)
开始。像任何其他项目一样保存包(例如,MyComponents.dpk
)。
现在使用File->New->Other->Delphi Files
,双击右侧窗格中的Component
。将启动New Component
向导,您可以在其中选择要从中下降的现有组件(或设计新组件)。
按照向导的步骤操作,您将得到组件的基本shell:
unit MyToolBar1;
interface
uses
System.SysUtils, System.Classes, Vcl.Controls, Vcl.ToolWin, Vcl.ComCtrls;
type
TMyToolBar = class(TToolBar)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
published
{ Published declarations }
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('Samples', [TMyToolBar]);
end;
end.
在新的后代中实现所需的任何功能,然后保存文件。
右键单击Project Manager
中的包(默认情况下为IDE中的右上方窗口),然后从上下文菜单中选择Install
。这将编译和构建包,并自动将其安装在IDE中。 (我展示的示例将根据Samples
调用中指示的内容将新组件放在调色板中的RegisterComponents
页面上。)
完成上述操作后,您可以更改现有表单(首先备份.pas和.dfm文件!)。我将使用您提到的TToolBar
,并且我已在下面的说明中发布了替换样品。
手动将源代码编辑器中的类名从TToolBar
更改为TMyToolBar
。
右键单击表单,然后从上下文菜单中选择View as Text
。
找到TToolBar
,并将其从TToolBar
更改为TMyToolBar
。
再次右键单击,然后从上下文菜单中选择View as Form
。如果您已正确完成这些步骤,则单击工具栏应在对象检查器中显示TMyToolBar
。如果你没有看到它(或者你收到错误消息),你做错了什么;您可以通过右键单击代码编辑器顶部的选项卡并选择Close tab
来关闭选项卡,并在提示保存更改时回答“否”,然后根据需要从备份副本中恢复,我告诉您先制作。
为您创建一个单元:
Unit YourComponent;
interface
uses
....
Type
TYourNewClass=Class(ExistingClass)
private
...
protected
...
public
...
published
end;
procedure Register;
implementation
.....
procedure Register;
begin
RegisterComponents('YourPalette', [TYourNewClass]);
end;
创建一个新的包(或打开一个自己的现有包)并在你的Package.bpl上添加你选择install的单元。