怎么样使TStringGrid中的某列对齐(居中\居右或居左)?

解决方案 »

  1.   

    假设第2,4列右对齐,其他的左对齐
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      Area: TRect;
    begin
      with StringGrid1, StringGrid1.Canvas do begin
        FillRect(Rect);
        Area:= Rect;
        InflateRect(Area, -2, -2);
        if (ACol=1) or (ACol=3) then DrawText(Handle, PChar(Cells[ACol, ARow]),
          Length(Cells[ACol, ARow]), Area, DT_RIGHT)
        else TextRect(Rect, Rect.Left + 2, Rect.Top + 2, Cells[ACol, ARow]);
      end;
    end;
      

  2.   

    占 lzy6204(畲族的DF) 的光。
    全部居中:
    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      Area: TRect;
      i:integer;
    begin
      with StringGrid1, StringGrid1.Canvas do begin
        FillRect(Rect);
        Area:= Rect;
        InflateRect(Area, -2, -2);
        for i:=0 to stringgrid1.RowCount-1 do
        DrawText(Handle, PChar(Cells[ACol, ARow]),Length(Cells[ACol, ARow]), Area, DT_Center)
      end;
    end;