【问题标题】:How can I change the font size of the header of a Stringgrid in Delphi Firemonkey XE7?如何在 Delphi Firemonkey XE7 中更改 Stringgrid 标题的字体大小?
【发布时间】:2015-07-29 15:22:10
【问题描述】:

我在 Delphi Firemonkey XE7 中为一个应用程序创建了一个 Stringgrid,并用我的 MySQL 数据库中的数据填充它。 为了放大字体大小,我使用了以下代码:

procedure TFormSearchRecipient.sgRecipientDrawColumnCell(Sender: TObject;
  const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF;
  const Row: Integer; const Value: TValue; const State: TGridDrawStates);
var b : TRectF; border: integer;
begin
  //following leaves in the end a border so that the marked item can be seen
  b := bounds;
  border:= 2;
  b.Top := b.Top + border;
  b.Left := b.Left - border;
  b.Height := b.Height - 2 * border;
  b.Width := b.Width - 2 * border;

  //colors the background white so that the data cannot be seen anymore
  Canvas.Fill.Color := TAlphaColorRec.White;
  Canvas.FillRect(b, 0, 0, [], 1);
  //change the canvas text options
  Canvas.Fill.Color := TAlphaColorRec.Black;
  Canvas.Font.Size := 25;
  //write the content
  Canvas.FillText(Bounds, Value.AsString , False, 1, [] , TTextAlign.Leading);
end;

我希望你们中的一些人能理解这段代码的作用...... 这个picture 可能会有所帮助。

我现在的问题是:如何设置页眉以及如何放大页眉的字体大小,或者 - 如果不可能 - 我如何禁用、删除或隐藏页眉?

提前致谢!

你好,莉亚

【问题讨论】:

    标签: delphi header firemonkey delphi-xe7 stringgrid


    【解决方案1】:

    简单的方法:您可以在设计时通过取消选中 StringGrid.Options 中的选项 Header 来隐藏标题。

    或者,在运行时:StringGrid.Options:=StringGrid.Options - [TGridOption.Header]

    对于绘制标题文本,您可以使用 OnDrawColumnHeader 事件。例如:

    procedure THeaderFooterForm.sg1DrawColumnHeader(Sender: TObject;
      const Canvas: TCanvas; const Column: TColumn; const Bounds: TRectF);
    begin
      Canvas.Fill.Color := TAlphaColorRec.White;
      Canvas.FillRect(Bounds, 0, 0, [], 1);
      Canvas.Font.Size := 25;
      Canvas.Fill.Color := TAlphaColorRec.Black;
      Canvas.FillText(Bounds, Column.Header , False, 1, [] , TTextAlign.Leading);
    end;
    

    在设计时编辑列标题文本,右键单击 StringGrid 并选择“项目编辑器”。在 Object Inspector 中选择任意列并设置 Header 属性。

    或者,在运行时:sg1.Columns[zero_based_column_index].Header:='some text';

    最后一个问题 - 如何设置标题高度...我不知道,如何在运行时执行此操作。 TStringGrid 和 TCustomGrid 使用私有字段 FHeader,通过从列中复制值来更新 TCustomGrid.UpdateHeader 方法。没有从 FMX.Grid 单元外部访问 FHeader 的属性、事件或方法... 但是您仍然可以自定义样式。只需在 Style editor 中选择 stringgridstyle.background.header 并在 Object Inspector 中编辑 Height 属性。

    【讨论】:

      【解决方案2】:

      OnApplyStyleLookup:

      var header:Theader;
      begin
      header:=THeader(TStringGrid(Sender).findStyleResource('Header'));
      if Assigned(header) then
         header.height:=100;
      end;
      

      【讨论】:

      • 您好,欢迎来到 StackOverflow。请在您的答案中添加更多信息,因为仅代码的答案不适合 SO。请查看stackoverflow.com/help/how-to-answer 了解更多信息。
      • 好吧,这不是最全面的答案,但至少它可以很好地改变标题的高度。这正是我想要的。
      猜你喜欢
      • 2015-10-11
      • 2012-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      相关资源
      最近更新 更多