TStringGrid 添加鼠标拖动功能


本文整理自网络,侵删。

 代码文件:


--------------------------------------------------------------------------------

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Grids;

type
TForm1 = class(TForm)
StringGrid1: TStringGrid;
procedure FormCreate(Sender: TObject);
procedure StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

var
flag: Boolean;
x1,y1: Integer;

{初始化测试数据}
procedure TForm1.FormCreate(Sender: TObject);
var
i,j: Integer;
begin
StringGrid1.ColCount := 100;
StringGrid1.RowCount := 100;
StringGrid1.Align := alClient;
StringGrid1.Options := StringGrid1.Options - [goRangeSelect];
for i := 0 to StringGrid1.ColCount - 1 do
for j := 0 to StringGrid1.RowCount - 1 do
StringGrid1.Cells[i,j] := IntToStr(i*j);
end;

procedure TForm1.StringGrid1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
if not(ssCtrl in Shift) then Exit; {假如是按住 Ctrl 才能拖动}
flag := True;
x1 := X;
y1 := Y;
end;

procedure TForm1.StringGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
var
px,py: Integer;
begin
if not flag then Exit;
//if not(ssCtrl in Shift) then Exit;

px := GetScrollPos(StringGrid1.Handle, SB_HORZ);
py := GetScrollPos(StringGrid1.Handle, SB_VERT);
px := px - (X - x1);
py := py - (Y - y1);

StringGrid1.Perform(WM_HSCROLL, px shl 16 or SB_THUMBPOSITION, 0);
StringGrid1.Perform(WM_VSCROLL, py shl 16 or SB_THUMBPOSITION, 0);

x1 := X;
y1 := Y;
end;

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
flag := False;
end;

end.

相关阅读 >>

Delphi 之前解析串口数据

Delphi程序运行在64位机器连接odbc的问题

Delphi listview基本用法大全

干掉360保险箱vb/vc/Delphi 源码

Delphi timer定时器 实现定时执行

Delphi gb2312 编码转义url字符串

Delphi xe6 android 实现base64字符串的解析

Delphi 多函数形式的多线程同步-线程执行顺序

Delphi api 做的 serversocket 例子

Delphi xe 如何使用自带皮肤

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...