我想改变 Delphi 字符串网格中一个单元格的背景颜色(而不是字体),只想修改一个单元格,而非整行或列。是否可以实现?
RRUZ:您提供的程序是正确且可行的,但在我的程序中却无法生效。
我的程序如下:
x 是一个全局整数数组。
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);