StringGrid 实例5 本例功能:字体修改为居中,红色,20号

实例5
本例功能:
在FireMonkey中StringGrid对于字体的调整由于没有font属性和onDrawCell事件使得变得有些麻烦,后来费了一些功夫才找到方法。但由于XE3中某些属性还是没有,(比如说:StGrid.RealignContent;)于是下载了XE4才得以修改成功。
1
、字体修改为居中,红色,20号
运行效果图:




unit Unit22;

 

interface

 

uses

 System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,

 System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs,

 FMX.StdCtrls, FMX.Grid, FMX.Layouts, FMX.Header, Data.DB, Datasnap.DBClient;

 

type

 TForm22 = class(TForm)

    StringGrid1: TStringGrid;

    Header1: THeader;

    StyleBook1: TStyleBook;

    StringColumn1: TStringColumn;

    StringColumn2: TStringColumn;

    StringColumn3: TStringColumn;

    procedure FormCreate(Sender: TObject);

    procedure ChangeGridFont;

    procedure GridCellStyle_alRight(Sender: TObject);

 private

    { Private declarations }

 public

    { Public declarations }

 end;

 

var

 Form22: TForm22;

 

implementation

 

{$R *.fmx}

procedure AlignHeaders(StGrid: TStringGrid);

var Header: THeader;

    I: Integer;

begin

 Header := THeader(StGrid.FindStyleResource('header'));

 if Assigned(Header) then

 begin

    for I := 0 to Header.Count - 1 do

    begin

      Header.Items[I].TextAlign := TTextAlign(0);

      Header.Items[I].StyledSettings:=[];   //如果没有这句,则字体大小,颜色不会改变

      with Header.Items[I].Font do

      begin

         Size := 22;

         Style := [TFontStyle.fsBold];

      end;

    end;

    Header.Height := 48;

 end;

 StGrid.RealignContent;

end;

 

 

procedure TForm22.FormCreate(Sender: TObject);

var i,j:integer;

begin

      AlignHeaders(StringGrid1);

        for i := 0 to StringGrid1.RowCount-1 do

            for j := 0 to StringGrid1.ColumnCount-1 do

                    StringGrid1.Cells[j,i]:=Format('%d,%d', [i,j]);;

                    ChangeGridFont;

 //StringGrid1.Columns[j].Width:=200;

 //StringGrid1.Columns[1].Width:=190;

//StringGrid1.Columns[2].Width:=80;

end;

 

procedure TForm22.GridCellStyle_alRight(Sender: TObject);

var

    TC: TTextCell;

begin

    AlignHeaders(StringGrid1);

    if ( Sender <> Nil ) and ( Sender is TTextCell ) then

    begin

      TC := TTextCell( Sender );

      TC.StyledSettings:=[];    //如果没有这句,则字体大小,颜色不会改变

      TC.FontColor := TAlphaColors.Red;

      TC.Font.Family := '宋体';

      TC.Font.Style := [ TFontStyle.fsBold, TFontStyle.fsItalic ];

      TC.Font.Size := 20;

      TC.TextAlign := TTextAlign.taLeading;

    end;

end;

 

procedure TForm22.ChangeGridFont;

var

       i: Integer;

       Col: Integer;

// Row: Integer;

 CellCtrl: TStyledControl;

begin

 for Col := 0 to StringGrid1.ColumnCount - 1 do

 begin

    for i := 0 to StringGrid1.Columns[Col].ChildrenCount - 1 do

    begin

          CellCtrl := TTextCell(StringGrid1.Columns[Col].Children[i]);

           if ( CellCtrl <> Nil ) and ( CellCtrl is TTextCell ) then

             TTextCell(CellCtrl).OnApplyStyleLookup := GridCellStyle_alRight;

    end;

 end;

end;

end.

你可能感兴趣的:(String)