【问题标题】:Can Delphi let user resize TStringGrid column without fixed rows?Delphi 可以让用户在没有固定行的情况下调整 TStringGrid 列的大小吗?
【发布时间】:2023-03-21 02:28:01
【问题描述】:

我想要一个所有行看起来都一样的网格 在 Delphi 中,如何让用户在没有固定行的情况下调整 TStringGrid 列的大小?一般只能调整固定的行,不能固定整个网格。

我正在使用 XE2。

TIA

标记

【问题讨论】:

  • 列,行...请您详细说明用户应该和不应该做什么?也许是一些带有箭头的 PNG 截图突出显示了 chanegs ?有点看不懂……
  • docwiki.embarcadero.com/Libraries/XE5/en/… 这表明任何列和行都可以调整大小,你设置对了吗?
  • 这里提到的问题是,Delphi (AFIAK) 只会在标题行(固定行)上方时为列设置鼠标“调整大小”光标。
  • 是的,Matt Allwood 已经确定了这个问题。我希望所有单元格看起来都一样,但是您只能在至少一个固定的情况下调整列的大小(FixedRows := 1),并且您不能显示所有行都固定的网格。我在做 grid.FixedRows := grid.RowCount ,它不会那样做。
  • 我所做的是添加一种标题行。我现在有一个固定的行和一个固定的列。这有一个意想不到的结果,我可以调整所有行的宽度,除了第 0 行。

标签: delphi column-width tstringgrid


【解决方案1】:

以下是我对bummi sn-p 所做的一些调整(Alt 键用于 ColSizing,Ctrl 用于 RowSizing):

unit UConfigPanel;

interface

uses
  System.Classes, System.SysUtils, Vcl.Grids, Vcl.Controls;

type
  TCustomStringGrid = Class(Vcl.Grids.TStringGrid)
  protected
    procedure CalcSizingState(X, Y: Integer; var State: TGridState; var Index: Longint;
      var SizingPos, SizingOfs: Integer; var FixedInfo: TGridDrawInfo); override;
    procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
    procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
  private
    FColSizing: Boolean;
    FRowSizing: Boolean;
    FCol: Integer;
    FRow: Integer;
  public
    property ColSizing: Boolean read FColSizing;
    property RowSizing: Boolean read FRowSizing;
  end;      

{ TCustomStringGrid }

procedure TCustomStringGrid.CalcSizingState(X, Y: Integer; var State: TGridState;
  var Index: Longint; var SizingPos, SizingOfs: Integer;
  var FixedInfo: TGridDrawInfo);
begin
  inherited;
  if FColSizing then
  begin
    State := gsColSizing;
    if (FCol > -1) then
      Index := FCol;
  end
  else
  if FRowSizing then
  begin
    State := gsRowSizing;
    if (FRow > -1) then
      Index := FRow;
  end
end;

procedure TCustomStringGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X,
  Y: Integer);
begin
  inherited;
  MouseToCell(X, Y, FCol, FRow);
end;

procedure TCustomStringGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
begin
  inherited;
  FColSizing := ssAlt in Shift;
  FRowSizing := ssCtrl in Shift;
end;

end.

【讨论】:

    【解决方案2】:

    您可以覆盖 CalcSizingState。

    设置
    - 如果满足您的条件(在下面的示例中检查是否在 MouseMove 中按下 Alt 键)和
    ,则向 gsRowSizing 声明 - 使用 MouseToCell 从 MouseDown 到计算索引的索引。

    可能需要进行一些微调。

    type
    
      TStringGrid = Class(Grids.TStringGrid)
      private
        FIsSizing: Boolean;
        FIndex: Integer;
        procedure CalcSizingState(X, Y: Integer; var State: TGridState; var Index: Longint; var SizingPos, SizingOfs: Integer; var FixedInfo: TGridDrawInfo); override;
        procedure MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer); override;
        procedure MouseMove(Shift: TShiftState; X, Y: Integer); override;
      private
    
      End;
    
      TForm3 = class(TForm)
        StringGrid1: TStringGrid;
      private
        { Private-Deklarationen }
      public
        { Public-Deklarationen }
      end;
    
    var
      Form3: TForm3;
    
    implementation
    
    {$R *.dfm}
    { TStringGrid }
    
    procedure TStringGrid.CalcSizingState(X, Y: Integer; var State: TGridState; var Index, SizingPos, SizingOfs: Integer; var FixedInfo: TGridDrawInfo);
    begin
      inherited;
      if FIsSizing then
        State := gsRowSizing;
      if (FIndex > -1) then
      begin
        Index := FIndex;
      end;
    end;
    
    procedure TStringGrid.MouseDown(Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    var
      Col: Integer;
    begin
      inherited;
      MouseToCell(X, Y, Col, FIndex);
    end;
    
    procedure TStringGrid.MouseMove(Shift: TShiftState; X, Y: Integer);
    begin
      inherited;
      FIsSizing := ssAlt in Shift;
    end;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-12
      • 2021-04-23
      • 2020-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多