1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
| unit DnDDataGrid;
interface
uses
System.Windows.Forms, System.Drawing;
type
TDnDDataGrid = class(DataGrid)
private
{ Private Declarations }
dndMouseDownRow : integer;
dndMouseDownCol : integer;
txtSource : string;
procedure HandleMouseUp(sender : System.Object; e : System.Windows.Forms.MouseEventArgs);
procedure HandleMouseDown(sender : System.Object; e : System.Windows.Forms.MouseEventArgs);
procedure HandleMouseMove(sender : System.Object; e : System.Windows.Forms.MouseEventArgs);
procedure HandleDragOver(sender : System.Object; e : System.Windows.Forms.DragEventArgs);
procedure HandleDragEnter(sender : System.Object; e : System.Windows.Forms.DragEventArgs);
procedure HandleDragDrop(sender : System.Object; e : System.Windows.Forms.DragEventArgs);
public
constructor Create;
function getTexte() : string;
end;
implementation
constructor TDnDDataGrid.Create;
begin
inherited Create;
Include(Self.DragOver, Self.HandleDragOver);
Include(Self.DragDrop, Self.HandleDragDrop);
Include(Self.DragEnter, Self.HandleDragEnter);
Include(Self.MouseDown, Self.HandleMouseDown);
Include(Self.MouseMove, Self.HandleMouseMove);
Include(Self.MouseUp, Self.HandleMouseUp);
Self.dndMouseDownRow := -1;
Self.dndMouseDownCol := -1;
Self.AllowDrop := true;
end;
//Lorsque l'on lache la souris
function TDnDDataGrid.getTexte: string;
begin
Result := txtSource;
end;
procedure TDnDDataGrid.HandleDragDrop(sender: TObject;
e: System.Windows.Forms.DragEventArgs);
var
ht : System.Windows.Forms.DataGrid.HitTestInfo;
p : System.Drawing.Point;
begin
if(e.Data.GetDataPresent('Text'))then
begin
ht := Self.HitTest(Self.PointToClient(Point.Create(e.X,e.Y)));
if not ( (ht.Row = -1) or (ht.Column = -1) ) then
Self[ht.Row, ht.Column] := string(e.Data.GetData('Text'));
end;
end;
//Quand il entre dans la dataGrid
procedure TDnDDataGrid.HandleDragEnter(sender: TObject;
e: System.Windows.Forms.DragEventArgs);
begin
if(e.Data.GetDataPresent('Text')) then
e.Effect := DragDropEffects.Copy;
end;
procedure TDnDDataGrid.HandleDragOver(sender: TObject;
e: System.Windows.Forms.DragEventArgs);
var
ht : System.Windows.Forms.DataGrid.HitTestInfo;
begin
ht := Self.HitTest(Self.PointToClient(Point.Create(e.X,e.Y)));
if( ((ht.Row = Self.dndMouseDownRow) and (ht.Column = Self.dndMouseDownCol))
or (not e.Data.GetDataPresent('Text'))) then
//Si on se trouve dans la cellule source
e.Effect := DragDropEffects.None
else
//Si on se trouve dans une autre cellule
e.Effect := DragDropEffects.Copy;
end;
//Quand en lance le drag
procedure TDnDDataGrid.HandleMouseDown(sender: TObject;
e: System.Windows.Forms.MouseEventArgs);
var
ht : System.Windows.Forms.DataGrid.HitTestInfo;
begin
ht := Self.HitTest(Point.Create(e.X,e.Y));
Self.dndMouseDownRow := ht.Row;
Self.dndMouseDownCol := ht.Column;
Self.txtSource := 'aaa';
end;
//Quand on bouge
procedure TDnDDataGrid.HandleMouseMove(sender: TObject;
e: System.Windows.Forms.MouseEventArgs);
var
ht : System.Windows.Forms.DataGrid.HitTestInfo;
data : String;
begin
ht := Self.HitTest(Point.Create(e.X,e.Y));
if(e.Button = System.Windows.Forms.MouseButtons.Left) then
begin
if not( (dndMouseDownRow = -1) or (dndMouseDownCol = -1) or
( (ht.Row = dndMouseDownRow) and (ht.Column = dndMouseDownCol) ) ) then
begin
data := Self[Self.dndMouseDownRow, Self.dndMouseDownCol].ToString();
Self.DoDragDrop(data, DragDropEffects.Copy);
Self.dndMouseDownRow := -1;
Self.dndMouseDownCol := -1;
end;
end;
end;
procedure TDnDDataGrid.HandleMouseUp(sender: TObject;
e: System.Windows.Forms.MouseEventArgs);
begin
Self.dndMouseDownRow := -1;
Self.dndMouseDownCol := -1;
end;
end. |
Partager