How to edit a control within a FireMonkey style outside of the style designer?

南楼画角 提交于 2019-12-24 10:03:54

问题


In this scenario, I created a default button on my FireMonkey HD application via RADStudio XE2. I then created a custom style for the button, named "Style1". This style is very much similar to the default button style, however, it has a TImage control next to the TText control.

In simple words, a button with an image next to the text.

Now, I'll apply an image to the TImage control for the button? Because if I apply an image to the TImage control VIA the style designer, the other controls who use the style will also get the same image.


回答1:


you can do it at runtime. at first you have to name your TImage style object, for ex. 'btnimg' after that you can find it by name using FindStyleResource:

procedure LoadImage(btn : TButton; imgFileName : string);
var img : TImage;
begin
    img := btn.FindStyleResource('btnimg') as TImage;

    if not assigned(img) then exit;

    img.bitmap.LoadFromFile(imgFileName); 
end;



回答2:


You might be interested in my TBitmapSpeedButton control, which has this functionality ready-rolled: http://monkeystyler.com/blog/entry/my-first-firemonkey-custom-control-tbitmapspeedbutton plus the update to load the image from a style resource: http://monkeystyler.com/blog/entry/tbitmapspeedbutton-loading-images-from-the-style



来源:https://stackoverflow.com/questions/9346867/how-to-edit-a-control-within-a-firemonkey-style-outside-of-the-style-designer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!