可否顺便告诉我如何改变某个Cell的文本颜色!

解决方案 »

  1.   

    把StringGrid的DefaultDrawing设为False,然后在OnDrawCell中写
    DrawText(YouStringGrid.Canvas.Handle,PChar(YorStringGrid.Cells[ACol,ARow]),Length(YouStringGrid.Cells[ACol,ARow]),Rect,DT_WORDBREAK or DT_CENTER);改变颜色可用YouStringGrid.Canvas.Brush.Color := clRed;
      

  2.   

    1。设置对齐(这是我从伴水帮主那里学到的代码):
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      vText: PChar;
    begin
      with TStringGrid(Sender) do begin
        vText := PChar(Cells[ACol, ARow]);
        Canvas.FillRect(Rect);
        DrawText(Canvas.Handle, vText, StrLen(vText), Rect,
          DT_CENTER or DT_VCENTER or DT_SINGLELINE);    if gdFocused in State then begin //同意camel_luo//我来加一条
          Rect.Left := Rect.Left + 1;
          Rect.Top := Rect.Top + 1;
          Rect.Right := Rect.Right - 1;
          Rect.Bottom := Rect.Bottom - 1;
          Canvas.DrawFocusRect(Rect);
        end;
      end;
    end;
    具体参考如下贴子
    http://www.csdn.net/expert/topic/825/825872.xml?temp=.6189844
    2。改变某个单元格的颜色(活在水中的鱼的贴子):
    procedure Tfrmgz.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
        C1,  C2:  TColor;
    begin 
         if  gdSelected    in  State  then 
        begin  
            with  TDBGrid(Sender).Canvas  do  
            begin 
            C1  :=  Brush.Color; 
            C2  :=  Font.Color; 
            Brush.Color  := clinfobk;//红底
            Font.Color  :=  Clgreen;//黄字 
            TDBGrid(Sender).DefaultDrawColumnCell(Rect,  DataCol,  Column,  State); 
            Brush.Color  :=  C1; 
            Font.Color  :=    C2  ; 
            end; 
        end; 
    end;详见:
    http://www.csdn.net/expert/topic/825/825872.xml?temp=.6189844
      

  3.   

    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids;type
      TForm1 = class(TForm)
        StringGrid1: TStringGrid;
        procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
          Rect: TRect; State: TGridDrawState);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;var
      Form1: TForm1;implementation{$R *.dfm}procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    begin
        if (acol=1) and (arow=2) then
        begin
            StringGrid1.Canvas.Font.Color:=clred;
            StringGrid1.Canvas.Brush.Color:=clyellow;
            StringGrid1.Canvas.TextRect(Rect,Rect.Left,Rect.Top,StringGrid1.Cells[Acol,arow]);
        end;
    end;procedure TForm1.FormCreate(Sender: TObject);
    var
        i,j : integer;
    begin
        StringGrid1.RowCount:=5;
        StringGrid1.ColCount:=5;
        for i:=0 to 4 do
            for j:= 0 to 4 do
                StringGrid1.Cells[i,j]:=inttostr(i*j);
    end;end.
      

  4.   

    sgCol: TStringGridprocedure TForm1.sgColDrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
      S: string;
      Size: TSize;
    begin
      with sgCol.Canvas do
      begin
        Pen.Color := clBlack;
        if (ACol = 0) and (ARow = 0) then //画一个对角线和一些字
        begin
          MoveTo(Rect.Top, Rect.Left);
          LineTo(Rect.Right, Rect.Bottom);
          TextOut(((Rect.Right - Rect.Left) div 2) + 11, Rect.Top, '表列');
          TextOut(Rect.Left + 1, (Rect.Bottom - Rect.Top) div 2, '名称');
        end
        else begin  //只有三行,第一行空,二是“全称”,三是“简称“,
          //标题的文字中间对齐
          if (ACol >= 1) and (ARow = 0) then
            S := Format('第%d列', [ACol]);
          if ACol = 0 then
            case ARow of
              1: S := '全称';
              2: S := '简称';
            end;
          //如果你想第个单元格中间对齐, 
          //S := Cell[ARow, ACol];      Size := TextExtent(S);
          TextOut(Rect.Left + ((Rect.Right - Rect.Left - Size.CX) div 2),
            Rect.Top + ((Rect.Bottom - Rect.Top - Size.CY) div 2), S);
        end;
      end;
    end;
      

  5.   

    // 记录表格的 OnDrawCell 事件过程
    procedure TfrmShowRecord.strgridRecordDrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
       strValue: String;
       intMargin, intHeight, intWidth: Integer;
    begin
       with Sender as TStringGrid do
       begin
          // 画背景
          if (ARow > 0) and (ACol > 0) then
          begin
             if ARow mod 2 = 0 then
                Canvas.Brush.Color := $00F4FFFE
             else
                Canvas.Brush.Color := clWhite;
             Canvas.FillRect(Rect);
          end;
          // 计算显示在矩形框中的左上角位置
          strValue := Cells[ACol, ARow];
          Canvas.Font := Font;
          intWidth := Canvas.TextWidth(strValue);
          if (ARow = 0) or (ACol = 0) then
          begin
             intMargin := (Rect.Right - Rect.Left - intWidth) div 2;
             if intMargin < 0 then
                intMargin := 0;
          end
          else
          begin
             intMargin := 2;
             // 根据列的对齐方式设置显示模式
             if FarrColAlignments <> Nil then
                case FarrColAlignments[ACol - 1] of
                   taLeftJustify:
                      intMargin := 2;
                   taRightJustify:
                      intMargin := Rect.Right - Rect.Left - intWidth - 2;
                   taCenter:
                      intMargin := (Rect.Right - Rect.Left - intWidth) div 2;
                end;
          end;
          intHeight := (Rect.Bottom - Rect.Top - Canvas.TextHeight(strValue)) div 2;
          // 在矩形框中写值
          Canvas.TextRect(Rect, Rect.Left + intMargin, Rect.Top + intHeight, strValue);
       end;
    end;