代码之家  ›  专栏  ›  技术社区  ›  Mahm00d Rupal K

Delphi:如何使tstringgrid中心的单元格文本对齐?

  •  11
  • Mahm00d Rupal K  · 技术社区  · 15 年前

    这似乎是显而易见的。我希望文本位于单元格的中心,但由于某种原因,在属性中找不到它。我该怎么做?

    2 回复  |  直到 7 年前
        1
  •  16
  •   Mohammed Nasman    15 年前

    在tstringgrid中没有将文本居中的属性,但在drawcell事件中可以这样做:

    procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
      Rect: TRect; State: TGridDrawState);
    var
      S: string;
      SavedAlign: word;
    begin
      if ACol = 1 then begin  // ACol is zero based
        S := StringGrid1.Cells[ACol, ARow]; // cell contents
        SavedAlign := SetTextAlign(StringGrid1.Canvas.Handle, TA_CENTER);
        StringGrid1.Canvas.TextRect(Rect,
          Rect.Left + (Rect.Right - Rect.Left) div 2, Rect.Top + 2, S);
        SetTextAlign(StringGrid1.Canvas.Handle, SavedAlign);
      end;
    end;
    

    我发布的代码 here

    更新:

    若要在单元格中书写时使文本居中,请将此代码添加到 GetEditText 事件:

    procedure TForm1.StringGrid1GetEditText(Sender: TObject; ACol, ARow: Integer;
      var Value: string);
    var
      S : String;
      I: Integer;
      IE : TInplaceEdit ;
    begin
      for I := 0 to StringGrid1.ControlCount - 1 do
        if StringGrid1.Controls[i].ClassName = 'TInplaceEdit' then
        begin
          IE := TInplaceEdit(StringGrid1.Controls[i]);
          ie.Alignment := taCenter
        end;
    end;
    
        2
  •  4
  •   Johan    7 年前

    这是一个更好的解决方案,其他人和他们的程序有一个错误的类型。 TStringGrid.SetCellsAlignment t环形网格.setCellsAlignment 这个 (-1 < Index) 比较是正确的,但是 then else 部件已交换…正确的版本(此版本)将显示,当索引大于-1时,它将覆盖存储的值,否则它将添加一个新条目,其他人将只执行oposit,从索引消息中提取列表,感谢检测到这样的情况。

    我还可以在另一个单独的单元中完成所有的工作,所以这里是(希望现在是正确的,感谢您检测到这样的错误类型):

    unit AlignedTStringGrid;
    
    interface
    
    uses Windows,SysUtils,Classes,Grids;
    
    type 
      TStringGrid=class(Grids.TStringGrid)
      private
        FCellsAlignment:TStringList;
        FColsDefaultAlignment:TStringList;
        function GetCellsAlignment(ACol,ARow:Integer):TAlignment;
        procedure SetCellsAlignment(ACol,ARow:Integer;const Alignment:TAlignment);
        function GetColsDefaultAlignment(ACol:Integer):TAlignment;
        procedure SetColsDefaultAlignment(ACol:Integer;const Alignment:TAlignment);
      protected
        procedure DrawCell(ACol,ARow:Longint;ARect:TRect;AState:TGridDrawState);override;
      public
        constructor Create(AOwner:TComponent);override;
        destructor Destroy;override;
        property CellsAlignment[ACol,ARow:Integer]:TAlignment read GetCellsAlignment write SetCellsAlignment;
        property ColsDefaultAlignment[ACol:Integer]:TAlignment read GetColsDefaultAlignment write SetColsDefaultAlignment;
      end;
    
    implementation
    
    constructor TStringGrid.Create(AOwner:TComponent);
    begin
      inherited Create(AOwner);
      FCellsAlignment:=TStringList.Create;
      FCellsAlignment.CaseSensitive:=True;
      FCellsAlignment.Sorted:=True;
      FCellsAlignment.Duplicates:=dupIgnore;
      FColsDefaultAlignment:=TStringList.Create;
      FColsDefaultAlignment.CaseSensitive:=True;
      FColsDefaultAlignment.Sorted:=True;
      FColsDefaultAlignment.Duplicates:=dupIgnore;
    end;
    
    destructor TStringGrid.Destroy;
    begin
      FCellsAlignment.Free;
      FColsDefaultAlignment.Free;
      inherited Destroy;
    end;
    
    procedure TStringGrid.SetCellsAlignment(ACol,ARow: Integer; const Alignment: TAlignment);
    var
      Index:Integer;
    begin
      if (-1 < Index) then begin
        FCellsAlignment.Objects[Index]:= TObject(Alignment);
      end else begin
        FCellsAlignment.AddObject(IntToStr(ACol) + '-' + IntToStr(ARow), TObject(Alignment));
      end;
    end;
    
    function TStringGrid.GetCellsAlignment(ACol,ARow: Integer): TAlignment;
    var
      Index:Integer;
    begin
      Index:= FCellsAlignment.IndexOf(IntToStr(ACol)+'-'+IntToStr(ARow));
      if (-1 < Index) then begin
        GetCellsAlignment:= TAlignment(FCellsAlignment.Objects[Index]);
      end else begin
        GetCellsAlignment:= ColsDefaultAlignment[ACol];
      end;
    end;
    
    procedure TStringGrid.SetColsDefaultAlignment(ACol: Integer; const Alignment: TAlignment);
    var
      Index:Integer;
    begin
      Index:= FColsDefaultAlignment.IndexOf(IntToStr(ACol));
      if (-1 < Index) then begin
        FColsDefaultAlignment.Objects[Index]:= TObject(Alignment);
      end else begin
        FColsDefaultAlignment.AddObject(IntToStr(ACol), TObject(Alignment));
      end;
    end;
    
    function TStringGrid.GetColsDefaultAlignment(ACol:Integer):TAlignment;
    var
      Index:Integer;
    begin
      Index:= FColsDefaultAlignment.IndexOf(IntToStr(ACol));
      if (-1 < Index) then begin
        GetColsDefaultAlignment:= TAlignment(FColsDefaultAlignment.Objects[Index]);
      end else begin
        GetColsDefaultAlignment:=taLeftJustify;
      end;
    end;
    
    procedure TStringGrid.DrawCell(ACol,ARow:Longint;ARect:TRect;AState:TGridDrawState);
    var
      Old_DefaultDrawing:Boolean;
    begin
      if DefaultDrawing then begin
        case CellsAlignment[ACol,ARow] of
          taLeftJustify: begin
            Canvas.TextRect(ARect,ARect.Left+2,ARect.Top+2,Cells[ACol,ARow]);
          end;
          taRightJustify: begin
            Canvas.TextRect(ARect,ARect.Right -2 -Canvas.TextWidth(Cells[ACol,ARow]), ARect.Top+2,Cells[ACol,ARow]);
          end;
          taCenter: begin
            Canvas.TextRect(ARect,(ARect.Left+ARect.Right-Canvas.TextWidth(Cells[ACol,ARow]))div 2,ARect.Top+2,Cells[ACol,ARow]);
          end;
        end;
      end;
      Old_DefaultDrawing:= DefaultDrawing;
      DefaultDrawing:=False;
      inherited DrawCell(ACol,ARow,ARect,AState);
      DefaultDrawing:= Old_DefaultDrawing;
    end;
    
    end.
    

    这是一个完整的单元,将其保存到一个名为 AlignedTStringGrid.pas .

    那么在任何形式上你都有 TStringGrid 添加 ,AlignedTStringGrid 在接口的末尾使用子句。

    注意:行也可以这样做,但目前我不知道如何混合(列和行),因为如何选择优先级,如果有人对此非常感兴趣,请告诉我。

    P.D.:同样的想法也可以在Tedit上实现,只需在stackoverflow.com上搜索 TEdit.CreateParams 或读帖子 How to set textalignment in TEdit control