我想在Stirnggrid中显示不同颜色的字体,
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin   if StringGrid1.Cells [Acol,ARow]='3' then
     StringGrid1.Canvas.font.Color :=clred;
   if StringGrid1.Cells [Acol,ARow]='5' then
     StringGrid1.Canvas.font.Color :=clblue;
   if StringGrid1.Cells [Acol,ARow]='1' then
     StringGrid1.Canvas.font.Color :=clblack;
end;可是并没有效果,但我写成:procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
   if StringGrid1.Cells [Acol,ARow]='3' then
     StringGrid1.Canvas.brush.Color :=clred;
   if StringGrid1.Cells [Acol,ARow]='5' then
     StringGrid1.Canvas.brush.Color :=clblue;
   if StringGrid1.Cells [Acol,ARow]='1' then
     StringGrid1.Canvas.brush.Color :=clblack;    StringGrid1.Canvas.FillRect (Rect)
end;
这样又可以?还有就是要是写成:
procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
  Rect: TRect; State: TGridDrawState);
begin
   if StringGrid1.Cells [Acol,ARow]='3' then
     StringGrid1.Font.Color :=clred;
   if StringGrid1.Cells [Acol,ARow]='5' then
     StringGrid1.Font.Color :=clblue;
   if StringGrid1.Cells [Acol,ARow]='1' then
     StringGrid1.Font.Color :=clblack;end; 屏幕会不停的闪烁.这是为什么?

解决方案 »

  1.   

    第一种方法当然没有效果了,因为你只是给出了字体,没有把字体写出来呀,可以这样:
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
       StringGrid1.Canvas.FillRect (Rect);//清除以前的字体
       if StringGrid1.Cells [Acol,ARow]='3' then
         StringGrid1.Canvas.font.Color :=clred;
       if StringGrid1.Cells [Acol,ARow]='5' then
         StringGrid1.Canvas.font.Color :=clblue;
       if StringGrid1.Cells [Acol,ARow]='1' then
         StringGrid1.Canvas.font.Color :=clblack;
       DrawText(StringGrid1.Canvas.Handle,
                PChar(StringGrid1.Cells[ACol,ARow]),
                Length(StringGrid1.Cells[ACol,ARow]),
                DT_Left or DT_VCenter or DT_SingleLine);
      //把新的字体画出来!!
    end;//代码没有测试,你自己试吧,完善一下就可以了