procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if ARow>0 then begin
if ARow mod 2 = 0 then
StringGrid1.Canvas.Brush.Color := $00DBDBDB
else
StringGrid1.Canvas.Brush.Color := clWhite;
StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,stringgrid1.cell[acol,arow]);
end;
end;
procedure TfrmProviderStatus.StrGridMainDrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
tempStr :string;
StrHeight :integer;
s:string;
r:TRect;
begin
tempStr:=trim(StrGridMain.Cells[ACol,ARow]);
if (ACol=1) and (tempStr<>'')then
begin
with StrGridMain.Canvas do
begin
StrHeight:=TextHeight(tempStr);
Brush.Color:=RGB(100,128,192);
FillRect(Rect);
Pen.Color:=RGB(192,192,192);
Pen.Width:=1;
Rectangle(Rect);
Font.Color:=RGB(0,0,200);
Font.Style:=[fsBold,fsUnderline]; //字体加粗,加下划线
TextOut(Rect.Left+2,Rect.Top+(Rect.Bottom-Rect.Top-StrHeight) div 2,tempStr);
TextOut(Rect.Left+2,Rect.Top+(Rect.Bottom-Rect.Top-StrHeight) div 2,tempStr);
end;
end;
//--单元格居中
with StrGridMain do
begin
Canvas.FillRect(Rect);
s:=Cells[ACol,ARow];
r:=Rect;
DrawText(Canvas.Handle,PChar(s),Length(s),r,DT_CENTER or DT_SINGLELINE or DT_VCENTER);
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
if ARow>0 then begin
if ARow mod 2 = 0 then
StringGrid1.Canvas.Brush.Color := $00DBDBDB
else
StringGrid1.Canvas.Brush.Color := clWhite;
StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,'');
end;
end;
(注:此為隔行变色、供参考)
参照下面代码试一下行不:
procedure TfrmEhlib.dgStoreDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
if Odd(aqStore.RecNo + 1) then {隔行变色}
begin
dgStore.Canvas.Brush.Color := clCream;
dgStore.Canvas.Font.Color := clNavy;
end;
if aqStore.FieldByName('scount').asinteger > 80 then {附合条件行变色}
dgStore.Canvas.brush.Color := clLime;
if Column.FieldName = 'onshow' then {附合条件单元格变色}
if Column.Field.AsBoolean = true then
dgStore.Canvas.brush.Color := $00FFBB95;
if State = [gdSelected, gdFocused] then {当前所选单元格变色}
begin
dgStore.Canvas.Brush.Color := clSkyBlue;
dgStore.Canvas.Font.Color := clBlack;
end;