如何在Delphi Firemonkey XE7中更改Stringgrid标题的字体大小?

时间:2015-07-29 15:22:10

标签: delphi header firemonkey delphi-xe7 stringgrid

我在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可能会有所帮助。

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

提前致谢!

问候Lea

2 个答案:

答案 0 :(得分:3)

简单方法:您可以通过在设计时取消选中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,然后在Object Inspector中编辑Height属性。

答案 1 :(得分:-1)

OnApplyStyleLookup:

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