通过扩展类来样式化组件 [英] Styling components by extending their class
问题描述
我只是在想,如果我为要样式化的项目创建后代类,该怎么办?
I was just thinking, what about if I created a descendant class for the item I am styling.
例如(不好意思的代码,不是ide,但是你应该明白我的意思)
eg (excuse the poor code, not in ide, but you should get what I mean)
TStyledButton = class(TButton)
public
constructor Create; //This overrides the main TButton
end;
constructor TStyledButton.Create;
begin
inherited;
self.Color := clRed;
end;
然后在我的表单中,我只是将Button1作为TStyledButton。
Then in my form I just have the Button1 as a TStyledButton instead.
这会删除表单创建中用于处理设置样式/调用函数以设置样式的所有多余代码。
This would remove all extra code in the form create to handle setting styles/calling function to set styles.
唯一的问题是,在设计视图中该如何记录下来,我将不得不注册该对象(组件?),以便它实际上像往常一样在设计视图中显示。
The only issue is, how would this go down in the design view, will I have to register this Object (component?) so it actually shows up as usual in design view.
推荐答案
虽然您了解 Delphi软件包组件编写者,您可以使用IDE专家创建一个新组件,同时创建新的设计时包,将其自动添加到组件面板中:
While you learn about Delphi packages component writers, you can use the IDE expert to create a new component automatically add it to the component palete while creating a new design time package:
首先使用IDE创建它组件/新组件专家:
Start by creating it using the IDE expert in Component/New component:
系统提示时,选择安装到新软件包
When prompted, select Install to new package
提供软件包(文件)的名称和说明
Provide the package (file) name and description
和瞧!,您的面板中就有新组件:
and voila!, you have your new component in your palette:
尝试以下代码:
TMyButton = class(TButton)
public
constructor Create(AOwner: TComponent); override;
end;
procedure Register;
implementation
uses Graphics;
{ TMyButton }
constructor TMyButton.Create(AOwner: TComponent);
begin
inherited;
Font.Style := [fsBold];
Caption := 'Click me!';
end;
您将获得以下内容:
这篇关于通过扩展类来样式化组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!