1

我在 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;

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

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

提前致谢!

问候莉亚

4

2 回答 2

4

简单的方法:您可以在设计时通过取消选中 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 的属性、事件或方法...但是您仍然可以自定义样式。只需在样式编辑器中选择 stringgridstyle.background.header 并在对象检查器中编辑高度属性。

于 2015-07-29T18:32:59.840 回答
1

OnApplyStyleLookup:

var header:Theader;
begin
header:=THeader(TStringGrid(Sender).findStyleResource('Header'));
if Assigned(header) then
   header.height:=100;
end;
于 2017-03-03T20:54:28.433 回答