【问题标题】:In Firemonkey: Get a style object from another component在 Firemonkey 中:从另一个组件获取样式对象
【发布时间】:2022-01-07 13:48:33
【问题描述】:

我正在创建一个自定义组件,并且在该组件中我想使用 TGrid 中某些对象的某些颜色。我想尽可能多地使用样式颜色,以便我的应用程序具有一致的样式颜色。

例如,我需要 TGrid 中的 linefill 对象

基本上:我如何像普通的按钮点击一样找到 linefill 对象

【问题讨论】:

    标签: delphi firemonkey


    【解决方案1】:

    我可以这样解决:

    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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-08
      • 2018-11-23
      • 1970-01-01
      • 2021-11-09
      • 2021-04-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多