Delphi XE2使用VCL Styles (皮肤/skin)
在新版本中,你可以直接给你的程序换皮肤/skin(主题),Project-> Options -> Application -> Appearance,在这里选一个你想要的主题,
当然,你可以在你的程序里自己设定,为了使用VCL Styles,你需要使用TStyleManager类,Themes单元,如下几步:
注册Style
从文件加载(注册)VCL style,需要使用TStyleManager类中的LoadFromFile函数,
- procedure RegisterStyleFromDisk(const StyleFileName: string);
- begin
- try
- if TStyleManager.IsValidStyle(StyleFileName) then
- TStyleManager.LoadFromFile(StyleFileName);
- //beware in this line you are only loading and registering a VCL Style and not setting as the current style.
- else
- ShowMessage('the Style is not valid');
- end;
然后使用LoadFromResource或TryLoadFromResource从源加载style
- procedure RegisterStyleFromResource(const StyleResource: string);
- begin
- TStyleManager.LoadFromResource(HInstance, StyleResource);
- //beware in this line you are only loading and registering a VCL Style and not setting as the current style.
- end;
设置Style
设置已经加载好的Style,需要使用SetStyle(或TrySetStyle) procedure.
注意:SetStyle 有3个重载版本
- //class procedure SetStyle(const Name: string); overload;
- TStyleManager.SetStyle('StyleName');
- //class procedure SetStyle(Style: TCustomStyleServices); overload;
- TStyleManager.SetStyle(TStyleManager.Style['StyleName']);
- //class procedure SetStyle(Handle: TStyleServicesHandle); overload;
- TStyleManager.SetStyle(TStyleManager.LoadFromFile(StyleFileName))
使用上面代码写的Demo