我正在創建一個自定義組件,并且在該組件中我想使用來自 TGrid 中某些物件的某些顏色。我想盡可能多地使用樣式顏色,以便我的應用程式具有一致的樣式著色。
例如,我需要來自TGrid的linefill 物件。
基本上:我如何從簡單的按鈕點擊中找到那個linefill 物件?
uj5u.com熱心網友回復:
我可以這樣解決:
procedure TForm46.Button2Click(Sender: TObject);
var
sb: TFmxObject;
begin
if not Assigned(BOGrid1.Scene.StyleBook) then
sb := TStyleManager.ActiveStyleForScene(BOGrid1.Scene)
else
sb := BOGrid1.Scene.StyleBook.Style;
var f := TBrush.Create(TBrushKind.Solid,TAlphaColorRec.Aqua);
if Assigned(stylebook) then
begin
var st := sb.FindStyleResource('gridstyle', false);
if Assigned(st) then
begin
var stobj := st.FindStyleResource('alternatingrowbackground');
if (Assigned(stobj) and (stobj is TBrushObject)) then
begin
f.Assign((stobj as TBrushObject).Brush);
button2.Text := intToStr(f.Color);
end;
end;
end;
end;
其中 BOGrid1 是組件。
有關使用一些幫助程式構建自己的組件時的更通用答案:
interface
TStylesHelper = class
class function GetStyleObject(const StyleBook: TFmxObject; const Path: Array of string ): TFmxObject;
class function GetStyleObjectBrush(const StyleBook: TFmxObject; const Path: Array of string): TBrush;
end;
implementation
{ TStylesHelper }
class function TStylesHelper.GetStyleObject(const StyleBook: TFmxObject; const Path: array of string): TFmxObject;
begin
if not Assigned(StyleBook) then
Exit(nil);
var fmxObject := StyleBook;
for var styleName in Path do
begin
fmxObject := fmxObject.FindStyleResource(styleName);
if fmxObject = nil then
Exit(nil);
end;
result := fmxObject;
end;
class function TStylesHelper.GetStyleObjectBrush(const StyleBook: TFmxObject; const Path: array of string): TBrush;
begin
result := nil;
var bo := GetStyleObject(StyleBook, Path) as TBrushObject;
if Assigned(bo) then
result := bo.Brush;
end;
然后在組件的 ApplyStyle 受保護方法中,您可以查找所有內容:
procedure TBOGrid.ApplyStyle;
var
stylebook: TFmxObject;
begin
inherited;
if not Assigned(Scene) then
Exit;
if not Assigned(Scene.StyleBook) then
stylebook := TStyleManager.ActiveStyleForScene(Scene)
else
stylebook := Scene.StyleBook.Style;
if not Assigned(styleBook) then
Exit();
var lineFillBrush := TStylesHelper.GetStyleObjectBrush(stylebook, ['gridstyle','linefill']);
if Assigned(lineFillBrush) then
GridCellColor := lineFillBrush.Color
else
GridCellColor := DefaultGridCellColor;
end;
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/373134.html
上一篇:Delphi:用陣列搜索字串
