Reputation: 65
I've developed an inherited TCheckBox that needs some style fixes.
I've achieved this in the past with other firemonkey controls by overriding the "GetStyleObject" method and doing the necessary fixes "on the fly", over the original style object.
My intention with this "cleaner" approach is to allow the user to load any default firemonkey style, and still be able to code my own version of some styled controls.
Ex.:
type
TMyCheckBox = class(TCheckBox)
protected
function GetStyleObject: TFmxObject; override;
end;
function TMyCheckBox.GetStyleObject: TFmxObject;
begin
Result := inherited;
{do the required changes over the returned object}
end;
However, with this code, strangely "TMyCheckBox.GetStyleObject" never gets called, but with other controls like custom "TreeViewItem" it does...
Any thoughts?
Thank you all in advance.
Upvotes: 1
Views: 228
Reputation: 109
OK I had the same problem and here is how i solved them:
Procedure AppendStyle;
function GetClassStyleName: String; virtual;
See the code below for explanation: Here is the code of my component:
Procedure TPlayerButton.AppendStyle;
var
StyleObject : TFmxObject;
BinStream : TMemoryStream;
begin
BinStream := TMemoryStream.Create;
Try
StyleObject := GetStyleObject;
Try
BinStream.WriteComponent(StyleObject);
BinStream.Position := 0;
FStyle := TStyleStreaming.LoadFromStream(BinStream)
Finally
StyleObject.Free;
End;
Finally
BinStream.Free
End;
End;
function TPlayerButton.GetClassStyleName: String;
begin
Result := GetClassName + 'style';
Delete(Result, 1, 1);
end;
` 4. In the component constructor add as last lines:
constructor TPlayerButton.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
...
FStyle := nil;
AppendStyle;
StyleLookup := 'PlayerButtonStyle';
End;
Remember to free FStyle in the destructor;
if Assigned(FStyle) Then FreeAndNil(FStyle);
Everywhere replace TPlayerButton with the class name of your component.
VERY IMPORTANT: Remember when place a first component on a form to right click on it and select Edit Defalut Style
When Delphi style editor appear just close them. Now your style definition is added to the stylebook. You dont need to do this for next components placed ot the Form
Hope this will help you.
Upvotes: 1