Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do we detect and change styles at runtime? [duplicate]

It's cool that Delphi XE3 and below have styles for our applications. But I've noticed that we can mark as many styles as we want and them choose which of them to use as default.

It means that we could change styles at will, but how to do it in code? And how to let the user choose which style to use in our software?

like image 837
NaN Avatar asked Dec 15 '22 11:12

NaN


1 Answers

TStyleManager does what you need to accomplish this task. Use TStyleManager.StyleNames to get a list of the styles, and TStyleManager.TrySetStyle to change them at runtime.

To see how this works, start a new VCL Forms Application. Add all of the VCL styles you want to the project, and drop a TComboBox on a form. You'll need to add the implementation uses clause like I have below:

uses
  Vcl.Themes;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  TStyleManager.TrySetStyle(ComboBox1.Items[ComboBox1.ItemIndex]);
end;

procedure TForm1.FormShow(Sender: TObject);
var
  s: String;
begin
  ComboBox1.Items.BeginUpdate;
  try
    ComboBox1.Items.Clear;
    for s in TStyleManager.StyleNames do
       ComboBox1.Items.Add(s);
    ComboBox1.Sorted := True;
    // Select the style that's currently in use in the combobox
    ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(TStyleManager.ActiveStyle.Name);
  finally
    ComboBox1.Items.EndUpdate;
  end;
end;
like image 79
Ken White Avatar answered Dec 31 '22 12:12

Ken White