delphi stringgrid 某单元格字体颜色

2025-03-04 13:00:21
推荐回答(1个)
回答1:

完整的程序源码:
您拷贝就可以运行:

unit Unit1;

interface

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

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do
if Cells[ACol,ARow] = '1233' then
begin
Canvas.Font.Color := clred; //字体颜色为红的
Canvas.Brush.color:=clMoneyGreen; //背景为 美元绿色
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol,ARow]);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
with StringGrid1 do
begin
RowCount :=5;//设置5行
ColCount :=5;//设置5列
Cells[1,2]:='1233';
Cells[2,2]:='1233';
Cells[2,4]:='1233';
end;
end;

end.
-----------------------------------------------------
关键是DrawCell事件,在这个事件中可以设字体名、大小、颜色、字体装饰、cell背景画布等
---------------------------------------------------