请问如何在改变stringgrid中的文字颜色,要每个CElls都不一样的,因为我要做一个股票软件,里面涨跌都要用不同的颜色显示!我用stringgrid做得接收!

解决方案 »

  1.   

    在OnDrawwCell中,
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      with StringGrid1.Canvas do begin
        // 根据各种条件设置颜色
        if (ACol = 1) and (ARow = 2) then
          Font.Color := clBlue
        else
        if (ACol = 2) and (ARow = 1) then
          Font.Color := clRed;
        TextOut(Rect.Left+2, Rect.Top+2, StringGrid1.Cells[ACol, ARow]);
      end;
    end;
      

  2.   

    調試過了,沒問題:
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
      if gdFixed in state then  Exit;
      If StringGrid1.Cells[ACol,ARow]='A' then
          stringgrid1.Canvas.Font.Color:=clBlue
      else
          stringgrid1.Canvas.Font.Color:=clRed;
      stringgrid1.Canvas.Font.Height:=16;
      stringgrid1.Canvas.Font.Style:=[fsBold];
      stringgrid1.canvas.textout(rect.left+3,rect.top+6,StringGrid1.Cells[ACol,ARow]);
    end;