delphi:如何更改字符串网格中单元格的颜色
我想在 delphi 中更改字符串网格中单元格的背景颜色(不是字体).
I want to change background color ( not font ) of a cell in string grid in delphi .
只有一个单元格,而不是一行或一列.
Just one cell not a row or a column.
我可以吗?
RRUZ:你的程序是正确的并且有效,但在我的程序中不起作用.
RRUZ : your procedure is correct and works but in my procedure doesn't work.
我的程序:
x 是一个全局整数数组
x is a global array of integer
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 不起作用.如果我在下面的代码中处于非活动状态,我可以更改单元格颜色.但我两者都需要.
When I use Canvas.Brush.Color with below code , Canvas.Brush.Color doesn't work. If I inactive below code I can change the cells color. But I need both.
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);
Rafael 链接包含您需要的所有内容,使用 OnDrawCell
事件是绘制 StrignGrid 单元格的方法.检查这个仅绘制特定单元格背景的示例.
The Rafael link contains all which you need, using the OnDrawCell
event is the way to paint the cells of a StrignGrid. check this sample which paint only the background of an specific cell.
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
begin
if (ACol = 3) and (ARow = 2) then
with TStringGrid(Sender) do
begin
//paint the background Green
Canvas.Brush.Color := clGreen;
Canvas.FillRect(Rect);
Canvas.TextOut(Rect.Left+2,Rect.Top+2,Cells[ACol, ARow]);
end;
end;