Delphi XE2 VCL风格教程 - 如何在运行时更改样式 [英] Delphi XE2 VCL styles tutorial - how to change the style at runtime

查看:196
本文介绍了Delphi XE2 VCL风格教程 - 如何在运行时更改样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案

有没有一个很好的VCL样式教程,我们看到如何动态(运行时)加载/更改样式? p>我正在添加一个答案,因为本地信息通常只适用于链接。



这是您开始之前需要了解的关键事实:


  1. 许多VCL控件都有颜色属性,但是当样式打开时,这些属性将被忽略,默认的常用控件将被Delphi自己绘制,而不是使用Windows附带的XP或Windows 2000风格。


  2. 不知何故,深深的在您的应用程序中,VCL风格放置钩子,接管绘制您的控件。它可以处理的一切,将使用常规控件顶部的皮肤绘制。许多人称之为剥皮vcl,在VCL风格之前,您可能已经找到了第三方皮肤系统。现在它是内置的。


  3. 任何不挂钩的东西,仍然会得到普通的样式。所以大多数第三方的控制,VCL的一些位将不会主题。不要期待完美的即时结果。此外,您有时会看到由于皮肤造成的瞬间闪烁或毛刺,这是预期的。在运行时加载样式,结果的最终质量是任何人的猜测。您不一定会保证在运行时加载的样式将包含您可能希望包含的所有内容。你也不能保证一个您静态地包含在您的应用程序中,但至少您静态包含的可以由您的质量检查团队(可能是您)进行验证。


这是开始最简单的步骤:真的只有#2到#4是必不可少的。


  1. 单击文件 - >新建 - > VCL窗体项目。


  2. 右键单击项目管理器窗格中的项目选项,单击属性。导航到应用程序 - >外观


  3. 单击自定义样式将其打开。 (Amakrits是我的列表中的第一个,所以我点击)。


  4. 点击默认样式组合框,将其更改为默认设置。



  5. 运行您的应用程序。




现在,高级内容:在运行时更改您的样式:



我使用此按钮单击并创建formcreate来执行此操作: p>

fdefaultStyleName:String; 添加到表单的私人部分。



确保 Vcl.Themes 在您的uses子句中。

  procedure TForm1.Button1Click(Sender:TObject); 
begin
如果分配(TStyleManager.ActiveStyle)和(TStyleManager.ActiveStyle.Name<>'Windows')然后开始
TStyleManager.TrySetStyle('Windows');
end else begin
TStyleManager.TrySetStyle(fdefaultStyleName); //任何在项目设置中。
结束

end;

procedure TForm1.FormCreate(Sender:TObject);
begin
if Assigned(TStyleManager.ActiveStyle)then
fdefaultStyleName:= TStyleManager.ActiveStyle.Name;

end;


Is there a good VCL Styles tutorial where we see how to dynamically (in run time) load/change the style ?

解决方案

I'm adding an answer because local information is often preferred to just links.

Here's the key facts you need to know before you start:

  1. Many VCL controls have color properties, but those properties are going to get ignored when styles are on, and the default "common controls" like Button are going to get drawn by Delphi itself, instead of using the XP or Windows 2000 style that "comes with windows".

  2. Somehow, deep within your application, VCL styles puts hooks in that take over painting your controls. Everything that it can handle, will be drawn using a "skin" on top of the regular controls. Many people call this "skinning the vcl", and prior to VCL styles, you might have found a third party skin system. Now it's built in.

  3. Anything that is not hooked, will still get the regular style. So most third party controls, and some bits of the VCL will not be themed. Don't expect perfect instant results. Also, you might sometimes see some momentary flicker or glitches as a result of skinning, that's to be expected. Add loading of styles at runtime, and the end-quality of your result is anybody's guess. You can't necessarily guarantee that the style which is loaded at runtime, will contain everything you might want it to contain. Nor can you guarantee that with one you statically include in your app, but at least the ones you statically include could be verified by your QA team (which might be you).

And here's the simplest steps to get started: Really only step #2 through #4 are essential.

  1. Click File -> New -> VCL Forms project.

  2. Right click on the project options in the Project manager pane, and click properties. Navigate to Application -> Appearance

  3. Click on a custom style to turn it on. (Amakrits is the first in my list, so I'll click that).

  4. Click on the Default Style combobox and change it to something other than default.

  5. Put something on your form so it's not empty. (A button, a listbox, etc).

  6. Run your app.

Now, advanced stuff: Change your style at runtime:

I use this button click and formcreate to do that:

Add fdefaultStyleName:String; to private section of your form.

Make sure Vcl.Themes is in your uses clause.

procedure TForm1.Button1Click(Sender: TObject);
begin
 if Assigned(TStyleManager.ActiveStyle) and (TStyleManager.ActiveStyle.Name<>'Windows') then begin
   TStyleManager.TrySetStyle('Windows');
 end else begin
   TStyleManager.TrySetStyle(fdefaultStyleName); // whatever was in the project settings.
 end;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
if Assigned(TStyleManager.ActiveStyle) then
  fdefaultStyleName := TStyleManager.ActiveStyle.Name;

end;

这篇关于Delphi XE2 VCL风格教程 - 如何在运行时更改样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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