delphi:如何更改字符串网格中单元格的颜色
我想改变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);
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.
Can I?
RRUZ : your procedure is correct and works but in my procedure doesn't work.
My procedure:
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;
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.我使用了这些代码,翻译成C++。具体有两处注释,那我就贴出代码吧。
在“StringGrid1”中,属性“DefaultDrawing”必须为 FALSE 才能发挥作用。
“Canvas”对象必须是完全限定的:即。 StringGrid1->Canvas->Font->Color =clBlack。
代码:
I used these codes, translated to C++. There are two specific notes, then I'll post the code.
In "StringGrid1", the property "DefaultDrawing" must be FALSE for this to work.
The "Canvas" object must be fully qualified: ie. StringGrid1->Canvas->Font->Color =clBlack.
CODE: