在string中如何让每行显示不同颜色

解决方案 »

  1.   

    如果是一般的文本框,只需要改变.font.color就可以了!
      

  2.   

    stringgrid 可以在 onDrawCell事件中自己画颜色
      

  3.   

    是StringGrid,单行显示如题头的灰色,双行白色
      

  4.   

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      SGrdCount: TStringGrid;
      CellText: String;
      aTextLeft, aTextTop, aTextWidth, aTextHeight: Integer;
    begin
      SGrdCount := (Sender as TStringGrid);
      CellText := SGrdCount.Cells[ACol, ARow];
      SGrdCount.Canvas.Font := SGrdCount.Font;
       //单行显示灰色
      if(ARow Mod 2 = 0) then
      begin
        SGrdCount.Canvas.Brush.Color := SGrdCount.FixedColor;
        SGrdCount.Canvas.FillRect(Rect);
        Frame3D(SGrdCount.Canvas, Rect, clBtnHighlight, clBtnShadow, 1);
      end;
      //白色是默认色双行无需设置
    end;
      

  5.   

    数字也要画上去
    procedure TForm1.SGrdCountDrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      SGrdCount: TStringGrid;
      CellText: String;
      aTextLeft, aTextTop, aTextWidth, aTextHeight: Integer;
    begin
      SGrdCount := (Sender as TStringGrid);
      CellText := SGrdCount.Cells[ACol, ARow];
      SGrdCount.Canvas.Font := SGrdCount.Font;
       //µ¥ÐÐÏÔʾ»ÒÉ«
      if(ARow Mod 2 = 0) then
      begin
        SGrdCount.Canvas.Brush.Color := SGrdCount.FixedColor;
        SGrdCount.Canvas.FillRect(Rect);
        Frame3D(SGrdCount.Canvas, Rect, clBtnHighlight, clBtnShadow, 1);
      end;
      //设置文字显示位置
       aTextWidth := SGrdCount.Canvas.TextWidth(CellText);
       aTextHeight := SGrdCount.Canvas.TextHeight(CellText);
       aTextTop := Rect.Top + (Rect.Bottom - Rect.Top - aTextHeight) div 2;
       aTextLeft := Rect.Left + 2;  SGrdCount.Canvas.TextRect(Rect, aTextLeft, aTextTop, CellText) ;
    end;