我想要一个所有行看起来都一样的网格在Delphi中,我怎样才能让用户调整没有固定行的TStringGrid列的大小?通常你只能调整固定的行,你不能使整个网格固定。
我正在使用 XE2。
TIA
标记
我想要一个所有行看起来都一样的网格在Delphi中,我怎样才能让用户调整没有固定行的TStringGrid列的大小?通常你只能调整固定的行,你不能使整个网格固定。
我正在使用 XE2。
TIA
标记
您可以覆盖 CalcSizingState。
设置
- 如果满足您的条件,则将状态设置为 gsRowSizing(在下面的示例中,检查是否在 MouseMove 中按下了 Alt 键)和
- 使用 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;
以下是我对bummi片段所做的一些调整(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.