星五博客

Delphi自带皮肤的使用

delphi xe2之后自带了皮肤,已经满足基本的界面美化需求,以下是调用方法:
引用Vcl.Themes单元

私有变量 vThemes: string;

窗口创建事件

var 
s: string;
  list: THashedStringList;

vThemes := vIni.ReadString('Configure', 'Skins', 'Luna');
  list := THashedStringList.Create;
  try
    list.Sorted := true;
    list.AddStrings(TStyleManager.StyleNames);
    for s in list do
      if (s <> 'Luna') and (s <> 'Windows') then
      begin
        vItem := TMenuItem.Create(nil);
        vItem.Caption := s;
        vItem.OnClick := V1Click;
        N12.Add(vItem);
      end;
  finally
    list.Free;
  end;
  for i := 0 to N12.Count - 1 do
    N12.Items[i].Checked := N12.Items[i].Caption = vThemes;
  TStyleManager.SetStyle(vThemes);

换肤菜单:
procedure TfrmMain.V1Click(Sender: TObject);
var
  i: integer;
begin
  vThemes := TMenuItem(Sender).Caption;
  for i := 0 to N12.Count - 1 do
    N12.Items[i].Checked := N12.Items[i].Caption = vThemes;
  TStyleManager.SetStyle(vThemes);
  vIni.WriteString('Configure', 'Skins', vThemes);
end;

加载文件方法
var
  dir, fileName, styleName: string;

  // 样式文件 *.vsf
  dir := GetEnvironmentVariable('Delphi') + '\Redist\styles\vcl';

  for fileName in TDirectory.GetFiles(dir, '*.vsf') do
    TStyleManager.LoadFromFile(fileName);

  for styleName in TStyleManager.StyleNames do
    ListBox1.Items.Add(styleName);

Delphi