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 ImageTypeTImageType read FImageType write SetImageType;
property ImageStyleLookupString 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;
if FImageType itBitmap then
if FImage <> nil then
:= nil;
    if (
FScene <> nil) and (FScene.GetStyleBook <> nil) and (FScene.GetStyleBook.Root <> nilthen
:= TControl(FScene.GetStyleBook.Root.FindStyleResource(FImageStyleLookup));
Obj nil then
if Application.DefaultStyles <> nil then
:= TControl(Application.DefaultStyles.FindStyleResource(FImageStyleLookup));
    if (
Obj <> nil) and (Obj is TImage) and (FImage <> nilthen

which shows how to find a style resource either from the current stylebook, or if that isn’t found, from the applications default style.

Download updated sources


Commenting is not available in this channel entry.