Search code examples
delphidelphi-xe7vcl-styles

How to check if a style file is already loaded?


I have a ListBox that lists all style files (vsf) in a folder. When the user clicks a file, I load that style:

 if TStyleManager.IsValidStyle(sSkinFile, StyleInfo) then
  begin
   TStyleManager.LoadFromFile(sSkinFile);
   TStyleManager.SetStyle(StyleInfo.Name);
  end

However, if the user clicks a style that was already loaded (was clicked previously), Delphi will rise and exception: "Style 'Golden Graphite' already registered".

Note: It looks like the system will not release the previous styles when a new style is loaded. I think that the memory consumption will be a bit higher if the user will start clicking all listed styles.

How do I check if a style was already loaded?


Solution

  • You can use the Style property of the TStyleManager, this property will return nil when a VCL Style is not loaded. Try this sample.

    uses
      Vcl.Styles,
      Vcl.Themes;
    
    function  VCLStyleLoaded(StyleName : string) : Boolean;
    begin
     Result := TStyleManager.Style[StyleName] <> nil;
    end;
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
     StyleInfo: TStyleInfo;
    begin
      if OpenDialog1.Execute then
      begin        
        if TStyleManager.IsValidStyle(OpenDialog1.FileName, StyleInfo)  and not VCLStyleLoaded(StyleInfo.Name) then
        begin
         TStyleManager.LoadFromFile(OpenDialog1.FileName);
         TStyleManager.SetStyle(StyleInfo.Name);
        end
      end;
    end;