delphi中stringGrid不同行显示不同颜色字体出现闪烁。
在DrawCell()中的代码是这样的,实现双数行白色字体,单数行黑色字体
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do
begin
if (ARow mod 2=0)and ( (ACol=0)or(ACol=1) )then
begin
font.Color:=clWhite;
Canvas.textout(Rect.left+2,Rect.top+6,Cells[ACol,ARow]);
end
else if (ARow mod 2=1)and ( (ACol=0)or(ACol=1) )then
begin
font.Color:=clSkyBlue;
Canvas.textout(Rect.left+2,Rect.top+6,Cells[ACol,ARow]);
end;
end;
但是出现的效果是第一列的单数行不停闪烁,应该是字体在白色和黑色之间切换导致的。但是我明明(ARow mod 2=0)条件限制的呀,有谁知道为什么闪烁不?赐教!急啊!
[解决办法]
canvas.font.Color:=clWhite;
[解决办法]
procedure TForm1.FormCreate(Sender: TObject);
var
i:integer;
begin
for i:=0 to 4 do
begin
StringGrid1.Cells[0,i]:='AAA';
StringGrid1.Cells[1,i]:='BBB';
end;
end;
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
Rect: TRect; State: TGridDrawState);
begin
with StringGrid1 do
begin
if (ARow mod 2=0)and ( (ACol=0)or(ACol=1) )then
begin
Canvas.Brush.Color:=clGreen;
Canvas.FillRect(Rect);
Canvas.font.Color:=clWhite;
Canvas.textout(Rect.left+2,Rect.top+6,Cells[ACol,ARow]);
end
else if (ARow mod 2=1)and ( (ACol=0)or(ACol=1) )then
begin
Canvas.Brush.Color:=clYellow;
Canvas.FillRect(Rect);
Canvas.font.Color:=clblack;
Canvas.textout(Rect.left+2,Rect.top+6,Cells[ACol,ARow]);
end;
end;
end;
[解决办法]
因为你的2个if之外的情况,没有定义字体颜色,所以只能取最近上次的字体颜色
应该最后还有一个:else font.color:=clblack;