Delphi自带风格管理器TStyleManager

Delphi自带风格管理器TStyleManager

今天发现Delphi自带的VCL历程中有风格管理器。研究了下代码,惊喜发现几行代码就可以实现这个功能。做个笔记记录一下

  • 第一步 代码如下
  1. unit StyleManager; 
  2.  
  3. interface 
  4.  
  5. uses 
  6. Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics, 
  7. Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons, 
  8. Vcl.ButtonGroup, Vcl.CheckLst; 
  9.  
  10. type 
  11. TForm5 = class(TForm) 
  12. cbxVclStyles: TComboBox; 
  13. PreviousStyle: TButton; 
  14. NextStyle: TButton; 
  15. Panel1: TPanel; 
  16. GroupBox1: TGroupBox; 
  17. Edit1: TEdit; 
  18. Button3: TButton; 
  19. CheckBox1: TCheckBox; 
  20. RadioButton1: TRadioButton; 
  21. ComboBox1: TComboBox; 
  22. ScrollBar1: TScrollBar; 
  23. ScrollBar2: TScrollBar; 
  24. ListBox1: TListBox; 
  25. Shape1: TShape; 
  26. StaticText1: TStaticText; 
  27. CheckListBox1: TCheckListBox; 
  28. ButtonGroup1: TButtonGroup; 
  29. ColorBox1: TColorBox; 
  30. SpeedButton1: TSpeedButton; 
  31. Button1: TButton; 
  32. Button2: TButton; 
  33. Button4: TButton; 
  34. Button5: TButton; 
  35. RadioButton2: TRadioButton; 
  36. procedure FormCreate(Sender: TObject); 
  37. procedure cbxVclStylesChange(Sender: TObject); 
  38. procedure PreviousStyleClick(Sender: TObject); 
  39. procedure NextStyleClick(Sender: TObject); 
  40. private 
  41. { Private declarations } 
  42. public 
  43. { Public declarations } 
  44. end; 
  45.  
  46. var 
  47. Form5: TForm5; 
  48. myList:TStringList ; 
  49. number:integer; 
  50.  
  51. implementation 
  52. {$R *.dfm} 
  53. uses 
  54. Vcl.Themes; 
  55.  
  56.  
  57. procedure TForm5.cbxVclStylesChange(Sender: TObject); 
  58. begin 
  59. TStyleManager.SetStyle(cbxVclStyles.Text); 
  60. //以下方法可以用 如下代码替换。 
  61. // number := myList.IndexOf(cbxVclStyles.Text) 
  62.  
  63. for number := 0 to myList.Count do 
  64. begin 
  65. if myList[number]=cbxVclStyles.Text then 
  66. break; 
  67. end; 
  68.  
  69. end; 
  70.  
  71. procedure TForm5.FormCreate(Sender: TObject); 
  72. var 
  73. StyleName: string; 
  74. begin 
  75. myList:=TStringList.Create; 
  76. for StyleName in TStyleManager.StyleNames do 
  77. begin 
  78. cbxVclStyles.Items.Add(StyleName); 
  79. myList.Add(StyleName); 
  80. end; 
  81. cbxVclStyles.ItemIndex := cbxVclStyles.Items.IndexOf(TStyleManager.ActiveStyle.Name); 
  82. end; 
  83.  
  84. //下一个主题 
  85. procedure TForm5.NextStyleClick(Sender: TObject); 
  86. begin 
  87. number:=number+1; 
  88. if number<0 then 
  89. begin 
  90. number:=myList.Count-1; 
  91. end; 
  92.  
  93. TStyleManager.SetStyle(myList[number]); 
  94. cbxVclStyles.ItemIndex := cbxVclStyles.Items.IndexOf(TStyleManager.ActiveStyle.Name); 
  95. end; 
  96. //上一个主题 
  97. procedure TForm5.PreviousStyleClick(Sender: TObject); 
  98. begin 
  99. number:=number-1; 
  100. if number>=myList.Count then 
  101. begin 
  102. number:=0; 
  103. end; 
  104.  
  105. TStyleManager.SetStyle(myList[number]); 
  106. cbxVclStyles.ItemIndex := cbxVclStyles.Items.IndexOf(TStyleManager.ActiveStyle.Name); 
  107. end; 
  108.  
  109. end. 
  110.  
  • 第二步 更改工程设置
    在工程Options的选项卡下面,把需要风格都给勾上
    enter description here

实际运行效果如下
enter description here

posted @ 2019-11-30 12:20  阿丽塔  阅读(786)  评论(0编辑  收藏  举报