通过扩展组件的类来设计组件的样式
从这个问题 在引用/一个地方传递对象样式对象
我只是在想,如果我为我正在样式化的项目创建一个后代类会怎么样。
例如(请原谅糟糕的代码,不是在 ide 中,但你应该明白我的意思)
TStyledButton = class(TButton)
public
constructor Create; //This overrides the main TButton
end;
constructor TStyledButton.Create;
begin
inherited;
self.Color := clRed;
end;
然后在我的表单中,我只是将 Button1 作为 TStyledButton 代替。
这将删除表单 create 中用于处理设置样式/调用函数来设置样式的所有额外代码。
唯一的问题是,这将如何在设计视图中进行,我是否必须注册这个对象(组件?),以便它实际上像往常一样显示在设计视图中。
From this question Passing object in reference / one place to style objects
I was just thinking, what about if I created a descendant class for the item I am styling.
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;
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您了解 Delphi 包组件编写器时,您可以使用 IDE Expert 创建新组件在创建新的设计时包时自动将其添加到组件选项板:
首先使用 IDE 专家在组件/新组件中创建它:
出现提示时,选择安装到新包
提供包(文件)名称和描述
瞧!,您的调色板中已经有了新组件:
试试这个代码:
您会得到这个:
< img src="https://i.sstatic.net/usiBo.png" alt="我的按钮!">
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:
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:
Try this code:
You'll get this:
是的,您需要注册它,以便它显示在设计视图中。
这可能是一个好主意,因为您始终可以继续更改组件的行为。您需要更改组件样式,并且将来您可能需要其他东西。
所以,我会那么做。
编辑:
您可以通过创建一个应用程序来轻松更改所有
TButtons
为您自己的类型,该应用程序将搜索 DFM 和 PAS 寻找 TButtons 之类的组件,并将其更改为您自己的类型。或者您可以使用GExperts
替换组件功能。Yes, you will need to register it so it shows up in design view.
It may be a good idea since you can always continue to change your component behavior. You needed to change the component style and in the future you may need another thing.
So, I would do that.
EDIT:
You can easily change all
TButtons
for your own type by creating an APP that will search the DFM and PAS looking for components like TButtons and change it to your own. Or you can useGExperts
replace components function.