Delphi自带风格管理器TStyleManager
今天发现Delphi自带的VCL历程中有风格管理器。研究了下代码,惊喜发现几行代码就可以实现这个功能。做个笔记记录一下
- 第一步 代码如下
-
unit StyleManager;
-
-
interface
-
-
uses
- Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
- Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons,
- Vcl.ButtonGroup, Vcl.CheckLst;
-
-
type
- TForm5 = class(TForm)
- cbxVclStyles: TComboBox;
- PreviousStyle: TButton;
- NextStyle: TButton;
- Panel1: TPanel;
- GroupBox1: TGroupBox;
- Edit1: TEdit;
- Button3: TButton;
- CheckBox1: TCheckBox;
- RadioButton1: TRadioButton;
- ComboBox1: TComboBox;
- ScrollBar1: TScrollBar;
- ScrollBar2: TScrollBar;
- ListBox1: TListBox;
- Shape1: TShape;
- StaticText1: TStaticText;
- CheckListBox1: TCheckListBox;
- ButtonGroup1: TButtonGroup;
- ColorBox1: TColorBox;
- SpeedButton1: TSpeedButton;
- Button1: TButton;
- Button2: TButton;
- Button4: TButton;
- Button5: TButton;
- RadioButton2: TRadioButton;
- procedure FormCreate(Sender: TObject);
- procedure cbxVclStylesChange(Sender: TObject);
- procedure PreviousStyleClick(Sender: TObject);
- procedure NextStyleClick(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
-
-
var
- Form5: TForm5;
- myList:TStringList ;
- number:integer;
-
-
implementation
-
-
uses
- Vcl.Themes;
-
-
-
procedure TForm5.cbxVclStylesChange(Sender: TObject);
-
begin
- TStyleManager.SetStyle(cbxVclStyles.Text);
- //以下方法可以用 如下代码替换。
- // number := myList.IndexOf(cbxVclStyles.Text)
-
- for number := 0 to myList.Count do
- begin
- if myList[number]=cbxVclStyles.Text then
- break;
- end;
-
-
end;
-
-
procedure TForm5.FormCreate(Sender: TObject);
-
var
- StyleName: string;
-
begin
- myList:=TStringList.Create;
- for StyleName in TStyleManager.StyleNames do
- begin
- cbxVclStyles.Items.Add(StyleName);
- myList.Add(StyleName);
- end;
- cbxVclStyles.ItemIndex := cbxVclStyles.Items.IndexOf(TStyleManager.ActiveStyle.Name);
-
end;
-
-
//下一个主题
-
procedure TForm5.NextStyleClick(Sender: TObject);
-
begin
- number:=number+1;
- if number<0 then
- begin
- number:=myList.Count-1;
- end;
-
- TStyleManager.SetStyle(myList[number]);
- cbxVclStyles.ItemIndex := cbxVclStyles.Items.IndexOf(TStyleManager.ActiveStyle.Name);
-
end;
-
//上一个主题
-
procedure TForm5.PreviousStyleClick(Sender: TObject);
-
begin
- number:=number-1;
- if number>=myList.Count then
- begin
- number:=0;
- end;
-
- TStyleManager.SetStyle(myList[number]);
- cbxVclStyles.ItemIndex := cbxVclStyles.Items.IndexOf(TStyleManager.ActiveStyle.Name);
-
end;
-
-
end.
-
- 第二步 更改工程设置
在工程Options的选项卡下面,把需要风格都给勾上
实际运行效果如下