学用 TStringGrid [9] - OnDrawCell
学用 TStringGrid [9] - OnDrawCell
涂孟超 发表于3年前
学用 TStringGrid [9] - OnDrawCell
  • 发表于 3年前
  • 阅读 4
  • 收藏 0
  • 点赞 0
  • 评论 0
本例效果图:


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ExtCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure FormCreate(Sender: TObject);
    procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  TextFormat: TTextFormat;

procedure TForm1.FormCreate(Sender: TObject);
var
  i,j: Integer;
begin
  with StringGrid1 do
    for i := 0 to ColCount - 1 do
      for j := 0 to RowCount - 1 do
        Cells[i,j] := Format('%d%d',[i,j]);

  StringGrid1.Font.Style := [fsBold];
  StringGrid1.Font.Size := 10;
end;

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
var
  str: string;
begin
  Randomize;
  StringGrid1.Canvas.Brush.Color := Random($FFFFFF);
  StringGrid1.Canvas.FillRect(Rect);
  str := StringGrid1.Cells[ACol,ARow];
  StringGrid1.Canvas.TextRect(Rect, str, TextFormat);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  Randomize;
  StringGrid1.Font.Color := Random($FFFFFF);
  TButton(Sender).Caption := '字体颜色';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  TextFormat := [tfSingleLine, tfCenter, tfVerticalCenter];
  StringGrid1.Refresh;
  TButton(Sender).Caption := '居中对齐';
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  TextFormat := [tfSingleLine, tfRight, tfVerticalCenter];
  StringGrid1.Refresh;
  TButton(Sender).Caption := '右对齐';
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  TextFormat := [tfSingleLine, tfLeft, tfVerticalCenter];
  StringGrid1.Refresh;
  TButton(Sender).Caption := '左对齐';
end;

end.

 
 
 
 
 

 

 

  
共有 人打赏支持
粉丝 13
博文 2004
码字总数 14107
×
涂孟超
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: