Step 3 - Add Style-Resources as RCDATA (Delphi)

From RAD Studio
Jump to: navigation, search

Go Up to Creating a FireMonkey Component (Delphi)


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:

  • DialogButtons.win.rc
DialogButtonPanelStyle RCDATA "DialogButtonPanel_win.style"
  • DialogButtons.mac.rc
DialogButtonPanelStyle RCDATA "DialogButtonPanel_mac.style"
  • DialogButtons.ios.rc
DialogButtonPanelStyle RCDATA "DialogButtonPanel_ios.style"
  • DialogButtons.android.rc
DialogButtonPanelStyle RCDATA "DialogButtonPanel_android.style"

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:
protected
    function GetStyleObject: TControl; override;

implementation

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

{$IFDEF MACOS}
{$R *.mac.res}
{$ENDIF}
{$IFDEF MSWINDOWS}
{$R *.win.res}
{$ENDIF}

function TDialogButtonPanel.GetStyleObject: TControl;
const
  Style = 'MyPanelButtonsStyle';
begin
  if (FStyleLookup = '') then
  begin
    Result := TControl(TStyleManager.LoadFromResource(HInstance, Style,
      RT_RCDATA));
    Exit;
  end;
  Result := inherited GetStyleObject;
end;
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.

Previous

Next

See Also