1

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?

Dalija Prasnikar
  • 27,212
  • 44
  • 82
  • 159
Christo
  • 411
  • 7
  • 27

2 Answers2

1

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.

John Kouraklis
  • 686
  • 4
  • 12
  • Thank you. Unfortunately TStylebook does not have a property for LoadFromresource. Also, I used to add the style to my custom component at design time. I was hoping to still accomplish this. – Christo Apr 08 '16 at 14:41
  • Christo, sorry I meant TStylebook.LoadFromStream. I corrected it in the post. – John Kouraklis Apr 08 '16 at 14:48
  • Thank you for this, I got it working. I now only need to get it working on Design Time and not only on Run Time. Any suggestions? – Christo Apr 11 '16 at 07:46
  • If you want it in design time why are you trying to load it in run time? You can just load the style file directly to the style manager – John Kouraklis Apr 11 '16 at 15:15
  • Hi John. If you look at my example code in the question, you will see that I did do it through the style manager. The issue is that Delphi Seattle does not have the function in the TStyleManager class. However, as stated in my answer below, they moved it TStyleStreaming. – Christo Apr 12 '16 at 07:06
0

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;
Christo
  • 411
  • 7
  • 27