TBitmapSpeedButton: Loading Images from the Style
Recently recently blogged about creating a TBitmapSpeedButton - a button which could show a bitmap image. When I came to use it I realised an ideological flaw. I was using the old VCL style model of loading a bitmap at design time to use in the application.
That works fine for VCL, but in FireMonkey it’s good practice to offload the visuals to the style. What would be ideal is if, in the form designer, I can specify a style resource for the image to be used. Now I, or my visual designer, can choose an image to use without needing access to my source code. If the look of the software needs updating I can just update the style file with new images and the app will look different without having to edit the source code (or at least the FMX file, which as far as I’m concerned is the same thing.
So, I’ve updated my component with two new properties:
property ImageType: TImageType read FImageType write SetImageType;
property ImageStyleLookup: String read FImageStyleLookup write SetImageStyleLookup;
TImageType is either itBitmap or itStyleLookup and tells the component where to get the bitmap data from.
The only interesting bit of new code is the new UpdateImage method:
procedure TBitmapSpeedButton.UpdateImage;
var Obj: TFMXObject;
begin
if FImageType = itBitmap then
if FImage <> nil then
FImage.Bitmap.Assign(FBitmap)
else
else //itResource
begin
Obj := nil;
if (FScene <> nil) and (FScene.GetStyleBook <> nil) and (FScene.GetStyleBook.Root <> nil) then
Obj := TControl(FScene.GetStyleBook.Root.FindStyleResource(FImageStyleLookup));
if Obj = nil then
if Application.DefaultStyles <> nil then
Obj := TControl(Application.DefaultStyles.FindStyleResource(FImageStyleLookup));
if (Obj <> nil) and (Obj is TImage) and (FImage <> nil) then
FImage.Bitmap.Assign(TImage(Obj).Bitmap);
end;
end;
which shows how to find a style resource either from the current stylebook, or if that isn’t found, from the applications default style.
RSS feed
