DbGrid中可以是通过属性设置为每一列的文本居中,居左,居右,请问StringGrid如何做?

解决方案 »

  1.   

    var
      CX, CY:Integer;
      Str:string;
    begin
      Str := StringGridInfo.cells[acol, arow];
      CX := (Rect.Right - Rect.Left - Self.StringGridInfo.Canvas.TextWidth(Str)) div 2;
      CY := (Rect.Bottom - Rect.Top - Self.StringGridInfo.Canvas.TextHeight(Str)) div 2;
      StringGridInfo.Canvas.TextOut(rect.left + CX, rect.top + CY, Str);
    end;
      

  2.   

    在OnDrawCell事件下面绘制,先填充整个Cell,再写字。原来的字就没了
      

  3.   

    一个例子
    StringGrid组件Cells内容对齐
    ============================
    在StringGrid的DrawCell事件中添加类似的代码就可以了:VAR
     vCol, vRow : LongInt;
    begin
     vCol := ACol; vRow := ARow;
     WITH Sender AS TStringGrid, Canvas DO
       IF vCol = 2 THEN BEGIN ///对于第2列设置为右对齐
         SetTextAlign(Handle, TA_RIGHT);
         FillRect(Rect);
         TextRect(Rect, Rect.RIGHT-2, Rect.Top+2, Cells[vCol, vRow]);
       END;
    end;