其实只提供了 13 个 vsf 样式文件, 还有默认的 Windows 样式, 共 14 种.
在空白窗体上添加 ListBox1 等控件, 测试代码:
uses IOUtils, Vcl.Styles, vcl.Themes; procedure TForm1.FormCreate(Sender: TObject); var dir, fileName, styleName: string; begin //VCL 的样式文件 *.vsf 在 X:\Program Files\Embarcadero\RAD Studio\9.0\Redist\styles\vcl\ dir := GetEnvironmentVariable('Delphi') + '\Redist\styles\vcl'; {载入所有 *.vsf 文件} for fileName in TDirectory.GetFiles(dir, '*.vsf') do TStyleManager.LoadFromFile(fileName); {将样式名称导入列表} for styleName in TStyleManager.StyleNames do ListBox1.Items.Add(styleName); end; {修改样式} procedure TForm1.ListBox1Click(Sender: TObject); begin TStyleManager.SetStyle(ListBox1.Items[ListBox1.ItemIndex]); end;
效果图:

--------------
2012-06-08 我增加--
在窗体的onshow 事件中自动获取软件目录下的样式加到listbox中,这里很容易引起一个问题就是TStyleManager不允许已经加载过的重复加载,否则就会出错,经过反复测试,我终于测试出一种完美方法,

---------

procedure TfrmStyle.FormShow(Sender: TObject); var dir, fileName, styleName: string; I: Integer; top_exist: Boolean; begin //VCL 的样式文件 *.vsf 在 X:\Program Files\Embarcadero\RAD Studio\9.0\Redist\styles\vcl\ //dir := GetEnvironmentVariable('Delphi') + '\Redist\styles\vcl'; {载入所有 *.vsf 文件} { 帮助*'fileName = D:\ShopDaxiaProject\shopdaxia_soft\Style\IcebergClassico.vsf' } for fileName in TDirectory.GetFiles(ExtractFilePath(ParamStr(0))+'Style', '*.vsf') do begin //默认top_exist为false top_exist := False; for I := 0 to Length(TStyleManager.StyleNames)-1 do begin if StringReplace(TStyleManager.StyleNames[I],' ','',[rfReplaceAll]) = StringReplace(ExtractFileName(fileName),'.vsf','',[rfReplaceAll]) then begin //说明已经存在了,跳出 top_exist := True; Break; end; end; //如果不存在就加入进来 if not top_exist then begin TStyleManager.LoadFromFile(fileName); end; end; {将样式名称导入列表} for styleName in TStyleManager.StyleNames do begin ListBox1.Items.Add(styleName); end; end;
listbox 点击事件:
procedure TfrmStyle.ListBox1Click(Sender: TObject); var MyFastIni: TFastIni; begin TStyleManager.SetStyle(ListBox1.Items[ListBox1.ItemIndex]); {存入ini文件中} MyFastIni := TFastIni.Create; try MyFastIni.IniWriteString('setup','Style','StyleFile',ListBox1.Items[ListBox1.ItemIndex]); finally MyFastIni.Free; end; end;
主程序启动时的oncreate中的代码为(也记录下):
{获取样式} MyFastIni := TFastIni.Create; try if (MyFastIni.IniReadString('setup','Style','StyleFile') <> '') and (MyFastIni.IniReadString('setup','Style','StyleFile') <> 'Windows') then begin //因为样式的标题和文件名有区别,标题有空格,所以不要忘记stringreplace TStyleManager.LoadFromFile(ExtractFilePath(ParamStr(0))+'Style\'+StringReplace(MyFastIni.IniReadString('setup','Style','StyleFile'), ' ', '', [rfReplaceAll])+'.vsf'); TStyleManager.SetStyle(MyFastIni.IniReadString('setup','Style','StyleFile')); end; finally MyFastIni.Free; end;
花了一个晚上 终于搞定,如果没有之前的知识,估计很难搞定。。知识看来是慢慢积累的。
本文来自博客园,作者:del88,转载请注明原文链接:https://www.cnblogs.com/del88/archive/2012/06/07/2540984.html
浙公网安备 33010602011771号