我想在 delphi 中更改字符串网格中单元格的背景颜色(不是字体)。
只有一个单元格,而不是一行或一列。
我可以吗?
RRUZ:您的程序是正确的并且有效,但在我的程序中不起作用。
我的程序:
x 是一个全局整数数组
procedure TF_avalie_salon.StringGrid1DrawCell(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var S: string;
begin
S := StringGrid1.Cells[ACol, ARow];
StringGrid1.Canvas.FillRect(Rect);
SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
if (ARow<>0 )AND(acol<>0)AND(gridclick=true) then
begin
try
gridclick:=false;
x[acol+((strtoint(Edit_hafte.Text)-1)*7),arow]:=strtoint(StringGrid1.Cells[ACol, ARow]);
except
x[acol+((strtoint(Edit_hafte.Text)-1)*7),arow]:=0;
StringGrid1.Cells[acol,arow]:='0';
with TStringGrid(Sender) do
begin
Canvas.Brush.Color := clGreen;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
end;
end;
end;
end;
当我使用带有以下代码的 Canvas.Brush.Color 时,Canvas.Brush.Color 不起作用。如果我在下面的代码中处于非活动状态,我可以更改单元格的颜色。但我两者都需要。
S := StringGrid1.Cells[ACol, ARow];
StringGrid1.Canvas.FillRect(Rect);
SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
StringGrid1.Canvas.TextRect(Rect,Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);