给 TStringGrid 添加鼠标拖动功能 - 回复 "dxx" 的问题
2008-12-11 19:16
375 查看
问题来源: http://www.cnblogs.com/del/archive/2008/12/11/1091310.html#1398793
本例效果图(这个例子还有问题, 拖动和移动的距离单位不一样):
![](http://images.cnblogs.com/cnblogs_com/del/166320/o_812111.gif)
代码文件:
窗体文件:
本例效果图(这个例子还有问题, 拖动和移动的距离单位不一样):
![](http://images.cnblogs.com/cnblogs_com/del/166320/o_812111.gif)
代码文件:
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.
窗体文件:
object Form1: TForm1 Left = 0 Top = 0 Caption = 'Form1' ClientHeight = 187 ClientWidth = 310 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 = 8 Top = 8 Width = 281 Height = 153 TabOrder = 0 OnMouseDown = StringGrid1MouseDown OnMouseMove = StringGrid1MouseMove OnMouseUp = StringGrid1MouseUp end end
相关文章推荐
- 关于js拖拽功能,拖拽元素的position:fixed;left:0;right:0;样式引起左右拖动元素会出现落后鼠标移动距离的问题
- vue+mousemove实现鼠标拖动功能(拖动过快失效问题解决方法)
- 禁止用鼠标拖动窗口的大小 - 回复 "合肥的石头" 的问题
- FCKeditor编辑器添加图片上传功能及图片路径问题解决方法
- 关于在server2012添加IIS功能中出现WinRM无法启动的问题解决过程
- Sublime Text 3的注册码和添加到鼠标右键功能
- 模仿bootstrap做的 js tooltip (添加鼠标跟随功能)
- 删除或添加最大化、最小化按钮 - 回复 "Tommy the CAT" 的问题
- 原生JS表格行拖动排序,添加了回调功能
- 问题:UIButton 添加倒计时功能后会一闪一闪
- 可拖动窗口,附带鼠标控制渐变透明,开启关闭功能
- 鼠标按键失灵或单击变双击、拖动不灵等问题维修方法
- 【Unity快速实现小功能】快速添加一个鼠标点击物体的识别功能
- 为鼠标移动事件添加新的功能-在工具栏添加快捷按钮
- OpenLayers中开启SelectFeature控件后鼠标在面要素上不能拖动地图的问题
- 解决word中就出现了无法使用鼠标拖动和选取页面文字的问题
- 添加视频播放功能、鼠标点击人物移动
- JavaScript简单实现鼠标拖动选择功能
- c# 添加鼠标右键功能
- 图片放大时实现鼠标拖动查看完整图片功能