怎么让TStringGrid单元格的在单元格文字居中 ? 默认的是靠左上角对齐
谢谢

解决方案 »

  1.   

    procedure TFrmbzxx.StringGrid1DrawCell(Sender: TObject; ACol,
      ARow: Integer; Rect: TRect; State: TGridDrawState);
    var
      CellStr: String;
      Mode,CellLen: Integer;
    begin
      //①先清除CELL
      StringGrid1.Canvas.FillRect(Rect);
      CellStr := StringGrid1.Cells[ACol,ARow];
    //②根据文字長度自動調節列寛
      CellLen := StringGrid1.Canvas.TextWidth(CellStr)+3;
      if (CellLen>StringGrid1.ColWidths[ACol]) then
        StringGrid1.ColWidths[ACol] := CellLen;//④文字排版
      if ARow=0 then begin
        StringGrid1.Canvas.Font.Style :=[fsBold]; //可設定字体的顔色、大小
        Mode := DT_CENTER         //調整左右
      end
      else begin
        StringGrid1.Canvas.Font.Color := clWindowText;
        Mode := DT_LEFT;
        Rect.Left := Rect.Left+2; //微調(使字不圧左側的線)
      end;
      Rect.Top := Rect.Top+3;     //調整上下
      DrawText(StringGrid1.Canvas.Handle,PChar(CellStr),Length(CellStr),Rect,Mode);end;