Step 3 - Add Style-Resources as RCDATA (Object Pascal)

From Appmethod Topics
Jump to: navigation, search

Go Up to Creating a FireMonkey Component (Object Pascal)

Each .style file needs a corresponding platform-specific (one-liner) .rc file, with root names that match the component unit.

For example, here are a Windows .rc file, a Mac .rc file, an iOS .rc file and an Android .rc file:

DialogButtonPanelStyle RCDATA ""
  • DialogButtons.mac.rc
DialogButtonPanelStyle RCDATA ""
  • DialogButtons.ios.rc
DialogButtonPanelStyle RCDATA ""
DialogButtonPanelStyle RCDATA ""

Add the Style-Resources as RCDATA

  1. Activate the DialogButtonsPackage.bpl project by double-clicking it in the Project Manager.
  2. For each .rc file that must be created:
    • Select File > New > Other > Other Files > Text File and in the New File dialog, select .rc Resource File.
    • Add the line described above, and save with the correct file name.
    The .rc files should show up in the project tree under the Contains node. (For ease of editing, you can also add the .style files to the project, but doing so is not necessary for the compile; the .style files are found automatically because they are in the same directory as the .rc files.)
  3. In DialogButtons.pas, in order to load these styles, declare an override to TStyledControl.GetStyleObject and implement it:
    function GetStyleObject: TControl; override;


  System.Types, FMX.Controls, FMX.Styles;

{$R *.mac.res}
{$R *.win.res}

function TDialogButtonPanel.GetStyleObject: TControl;
  Style = 'MyPanelButtonsStyle';
  if (FStyleLookup = '') then
    Result := TControl(TStyleManager.LoadFromResource(HInstance, Style,
  Result := inherited GetStyleObject;
The appropriate .res compiled from the .rc file is included with conditional platform directives. The name of the RCDATA item, the same in both files, is a constant in the function.

Note: The StyleName of the root (TRectangle) component in the .style file is coincidentally the same as the RCDATA name. The StyleName is required if the style is incorporated in a style book, and the StyleName self-documents the style, so it is good practice to include it. But when loaded directly, the StyleName of the root is not used and is superfluous.

The function loads the style only if the StyleLookup is blank. It uses TStyleManager to load the style, with the current module's handle—at design time, it is the package .bpl; at run time, it is the program's .exe or .dll—because attempting to access non-existent RCDATA raises an exception. Otherwise, the inherited behavior is used to find a named style or find the default style by class name.

  1. With the code in place, right-click the package project in the Project Manager and select Install. This compiles the package project and adds the component to the Tool Palette.



See Also