我有一个TStringGrid
,但是如果我点击并拖动它,它可以垂直和水平平移,我不希望用户能够做到这一点,我如何才能阻止这种情况发生?
发布于 2019-03-26 08:18:32
您可以使用OnTopLeftChanged
事件来捕捉任何类型的“滚动”发生的时间,并决定如何继续。如果您不希望用户在某些情况下超出范围,可以根据需要重新设置范围。这里有一个粗略的例子..。
uStringGridTestMain.dfm:
object frmStringGridTestMain: TfrmStringGridTestMain
Left = 0
Top = 0
Caption = 'String Grid Test'
ClientHeight = 416
ClientWidth = 738
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -11
Font.Name = 'Tahoma'
Font.Style = []
OldCreateOrder = False
OnCreate = FormCreate
PixelsPerInch = 96
TextHeight = 13
object StringGrid1: TStringGrid
Left = 72
Top = 32
Width = 513
Height = 329
Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine]
TabOrder = 0
OnTopLeftChanged = StringGrid1TopLeftChanged
ColWidths = (
64
64
64
64
64)
RowHeights = (
24
24
24
24
24)
end
end
uStringGridTestMain.pas:
unit uStringGridTestMain;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.Grids;
type
TfrmStringGridTestMain = class(TForm)
StringGrid1: TStringGrid;
procedure StringGrid1TopLeftChanged(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
frmStringGridTestMain: TfrmStringGridTestMain;
implementation
{$R *.dfm}
procedure TfrmStringGridTestMain.FormCreate(Sender: TObject);
begin
StringGrid1.Align:= alClient;
//Let's put a big scroll in both directions...
StringGrid1.RowCount:= 50;
StringGrid1.ColCount:= 50;
end;
procedure TfrmStringGridTestMain.StringGrid1TopLeftChanged(Sender: TObject);
begin
//You can change the "current" cell...
StringGrid1.Row:= 1;
StringGrid1.Col:= 1;
//Or you can change the scrolled cell on top-left...
StringGrid1.TopRow:= 1;
StringGrid1.LeftCol:= 1;
end;
end.
发布于 2020-08-18 16:06:13
要防止拖动时平移,可以将TStringGrid的TouchTracking属性设置为TBehaviorBoolean.False
https://stackoverflow.com/questions/55336484
复制相似问题