Step 3 - Add Style-Resources as RCDATA (C++)

From RAD Studio
Jump to: navigation, search

Go Up to Creating a FireMonkey Component (C++)

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 and a Mac .rc file:

  • DialogButtons.win.rc
DialogButtonPanelStyle RCDATA "DialogButtonPanel_win.style"
  • DialogButtons.mac.rc
DialogButtonPanelStyle RCDATA "DialogButtonPanel_mac.style"

Add the Style-Resources as RCDATA

  1. Activate the DialogButtonsPackage.bpl project by double-clicking it in the Projects Window.
  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.h, in order to load these styles, declare an override to TStyledControl.GetStyleObject and implement it:
//DialogButtons.h
#include<System.Types.hpp>
#include<FMX.Styles.hpp>
...
protected:
     Fmx::Types::TFmxObject* __fastcall GetStyleObject(void);


//DialogButtons.cpp
...
Fmx::Types::TFmxObject* __fastcall TDialogButtonPanel::GetStyleObject(void)
{
 const UnicodeString Style = "DialogButtonPanelStyle";
	if (StyleLookup == "") {
	return TStyleManager::LoadFromResource((unsigned int)HInstance, Style,
	  (wchar_t*) RT_RCDATA);
	}
	return TStyledControl::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.

Previous

Next

See Also