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 {$R *.dfm} 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的选项卡下面,把需要风格都给勾上
实际运行效果如下
代码实现单风格方法:
引入
Vcl.Themes,
Vcl.Styles,
设置主题类型
TStyleManager.TrySetStyle('Turquoise Gray');
或则在窗体代码中实现
unit Unit14; interface uses Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.Themes, Vcl.Styles; type TForm14 = class(TForm) btn1: TButton; btn2: TButton; lst1: TListBox; OpenDialog1: TOpenDialog; procedure FormCreate(Sender: TObject); procedure btn1Click(Sender: TObject); private { Private declarations } procedure StylesListRefresh(); public { Public declarations } end; var Form14: TForm14; implementation {$R *.dfm} { TForm14 } procedure TForm14.btn1Click(Sender: TObject); begin TStyleManager.SetStyle(lst1.Items[lst1.ItemIndex]); end; procedure TForm14.FormCreate(Sender: TObject); begin Self.StylesListRefresh; end; procedure TForm14.StylesListRefresh; var stylename: string; begin Self.lst1.Clear; // retrieve all the styles linked in the executable for stylename in TStyleManager.StyleNames do begin lst1.Items.Add(stylename); end; end; end.
Delphi自带风格管理器TStyleManager https://www.gzza.com/1661.html
本网站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。原创内容除特殊说明外,转载本站文章请注明出处。
如有侵权、不妥之处,联系删除。 Email:master@gzza.com
本网站资源来自互联网收集,仅供用于学习和交流,请勿用于商业用途。原创内容除特殊说明外,转载本站文章请注明出处。
如有侵权、不妥之处,联系删除。 Email:master@gzza.com
THE END