我在stringGrid 的 drawcell事件里有如下代码,大概意思是每个四行交替变换颜色,
由于格子比较多,当stringGrid里的滚动条发生变化时,那些格子闪烁得很厉害,请问该如何解决呀.
代码如下:
var
i,j: integer;
rect: TRect;
begin
with stringGrid do
begin
for i:= 1 to RowCount do
for j:=1 to ColCount do
begin
case ch of
True: begin
Canvas.Brush.Color :=clSkyBlue;// ClBlue;
if (i mod 4 = 0) and (j=ColCount) then
if (i div 4) mod 2 =0 then
ch := False;
end;
False: begin
Canvas.Brush.Color :=clWindow;
if (i mod 4 = 0) and (j= ColCount) then
if (i div 4) mod 2 =1 then
begin
ch := True;
end ;
end;
end;
rect:= CellRect(j,i);
Canvas.FillRect(rect);
Canvas.font.color:=ClBlack;
Canvas.TextOut(rect.left , rect.top, Cells[j, i]);
end;
end;
end;
由于格子比较多,当stringGrid里的滚动条发生变化时,那些格子闪烁得很厉害,请问该如何解决呀.
代码如下:
var
i,j: integer;
rect: TRect;
begin
with stringGrid do
begin
for i:= 1 to RowCount do
for j:=1 to ColCount do
begin
case ch of
True: begin
Canvas.Brush.Color :=clSkyBlue;// ClBlue;
if (i mod 4 = 0) and (j=ColCount) then
if (i div 4) mod 2 =0 then
ch := False;
end;
False: begin
Canvas.Brush.Color :=clWindow;
if (i mod 4 = 0) and (j= ColCount) then
if (i div 4) mod 2 =1 then
begin
ch := True;
end ;
end;
end;
rect:= CellRect(j,i);
Canvas.FillRect(rect);
Canvas.font.color:=ClBlack;
Canvas.TextOut(rect.left , rect.top, Cells[j, i]);
end;
end;
end;
你想想你的代码是不是很失败!
把循环去掉,直接用参数Arow,Acol 这两个参数分别表示当前绘制的行和列,
说说你代码的问题,
1、Rect在事件中已经存在这样的参数,你怎么还在上面定义的呢
2、循环去掉
3、绘制单元格这样写吧 把这句改一下:
Canvas.TextOut(rect.left , rect.top, Cells[j, i]);
改为:
Canvas.TextRect(Rect,Rect.left,Rect.top,Cells[Acol,Arow]);
因为我这个stringGrid列数有120行,30列,但最多能显示20行12列,其他行列要要移动滚动条才能看到.但是这个Drawcell里面的ACOL这个能数最大值也是最多能显示的列即12列,用你的方法是不会有闪烁的现象,但是当移动单元或者滚动条时,那些颜色就显示得不正常了.所以我才想出了这个笨方法.可否帮忙解决一下.
但是当移动单元或者滚动条时,那些颜色就显示得不正常了,你在stringGrid的TopLeftChanged方法中加入一下代码:
stringGrid.Enabled = false;
stringGrid.Refresh;
stringGrid.Enabled = true;
stringGrid.SetFocus;
with self.StringGrid1 do
begin
if ((Arow div 4) mod 2)=0 then
canvas.Brush.Color:=clSkyBlue
else
canvas.Brush.Color:=clred;// ClBlue; Canvas.font.color:=Clblue;
Canvas.TextRect(Rect,Rect.left,Rect.top,Cells[Acol,Arow]);
end;
看了你的代码,恍然大悟啊!!!
不愧是五星级的.