How to apply a custom style to a custom Firemonkey component using Delphi Seattle

2.3k Views Asked by At

I have a custom Delphi component created for Firemonkey (fmx). I now need to apply my custom style to the component. The style is saved in a resource. Previously this was done in the GetStyleObject method by calling TStyleManager.LoadFromResource.

This method (LoadFromResource) does not exist anymore in Delphi 10 Seattle for the Firemonkey framework.

My code in XE7 was working through the LoadFromResource:

function TFMXPic.GetStyleObject: TFmxObject;
var
style : string;
begin
  if (StyleLookup = '') then
  begin
    style := GetClassStyleName;
    Result := TControl(TStyleManager.LoadFromResource(HInstance,
    style, RT_RCDATA));
    Exit;
  end;
  Result := inherited GetStyleObject;
end;

How do I achieve this in Delphi 10 Seattle?

2

There are 2 best solutions below

5
John Kouraklis On

first, I don't think it is correct to check the StyleLookup. This property tells the component to look for this specific style name in the stylebook.

Then, you try to load a style file at component level. FMX does work like this. You have a style book, which loads the style file and then each component in a form uses this book to locate the style name as defined by the stylelookup value.

Out of my head, this course of actions should do the job:

  1. Add the style file in the resources of your project as you have already done. Say you have a style called "mycomponent" for your component

  2. Add a stylebook in the form

  3. in the OnCreate even of the form, load the resource file to a TResourceStream and then load the last to the stylebook using TStyleBook.LoadFromStream

  4. Now you can access the style by setting the StyleLookup='mycomponent' property of your component

Hope this helps.

0
Christo On

I found a solution. Thank you guys at TMS software. The TStyleStreaming class should be used instead of TStyleManager class. I modified my code as follow (all is now working)

function TMyComponent.GetStyleObject: TFmxObject;
var
style : string;
begin
  if (StyleLookup = '') then
  begin
    style := GetClassStyleName;
    Result := TControl(TStyleStreaming.LoadFromResource(HInstance,
    style, RT_RCDATA));
    Exit;
  end;
  Result := inherited GetStyleObject;
end;