unit Main;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ExtCtrls, StdCtrls, ImgList;
type
TScrollDirection = (sdUp, sdDown, sdNone);
TForm1 = class(TForm)
TreeView1: TTreeView;
ImageList1: TImageList;
ListView1: TListView;
ListView2: TListView;
Timer1: TTimer;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure Timer1Timer(Sender: TObject);
procedure ListView1EndDrag(Sender, Target: TObject; X, Y: Integer);
procedure TreeView1EndDrag(Sender, Target: TObject; X, Y: Integer);
procedure Button1Click(Sender: TObject);
private
TargetWin: TWinControl;
ScrollDirection: TScrollDirection;
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var
//the maximum distance from the top or bottom of
//the WinControls in which a drag scroll will occur...
SCROLLMARGIN: integer = 18;
//---------------------------------------------------------------------
procedure TForm1.FormCreate(Sender: TObject);
var
rect: TRect;
begin
Treeview1.fullexpand;
//Get the height of a listview item for
//a better value for SCROLLMARGIN...
rect := Listview1.items[0].DisplayRect(drBounds);
SCROLLMARGIN := rect.bottom-rect.Top;
end;
//---------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
close;
end;
//---------------------------------------------------------------------
// This demonstrates dragging of nodes within a treeview...
//---------------------------------------------------------------------
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := Source = Sender;
if not Accept then exit;
//See if scrolling is needed...
with TTreeView(Sender) do
begin
if (Y < SCROLLMARGIN) then
ScrollDirection := sdUp
else if (Y > ClientHeight - SCROLLMARGIN) then
ScrollDirection := sdDown
else
ScrollDirection := sdNone;
if ScrollDirection = sdNone then
Timer1.enabled := false
else
begin
TargetWin := TWinControl(Sender);
Timer1.enabled := true;
end;
end;
end;
//---------------------------------------------------------------------
procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
with TTreeView(Sender) do
begin
if (Sender <> Source) or (DropTarget = nil) or
(DropTarget = Selected) then exit;
Selected.MoveTo(DropTarget,naAddChildFirst);
end;
end;
//---------------------------------------------------------------------
procedure TForm1.TreeView1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
Timer1.enabled := false;
end;
//---------------------------------------------------------------------
// This demonstrates dragging of nodes between listviews...
//---------------------------------------------------------------------
procedure TForm1.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if (Source is TListView) {and (Source <> Sender)} then
Accept := true else
Accept := false;
if not Accept then exit;
//OK, now see if scrolling is needed...
with TListView(Sender) do
begin
if (Y < SCROLLMARGIN*2) then //nb: allow for the listview header
ScrollDirection := sdUp
else if (Y > ClientHeight - SCROLLMARGIN) then
ScrollDirection := sdDown
else
ScrollDirection := sdNone;
if ScrollDirection = sdNone then
Timer1.enabled := false
else
begin
TargetWin := TWinControl(Sender);
Timer1.enabled := true;
end;
end;
end;
//---------------------------------------------------------------------
procedure TForm1.ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
var
ListItem: TListItem;
begin
Timer1.enabled := false;
if not (Source is TListView) or (Source = Sender) then exit;
with TListView(Source).Selected do
begin
ListItem := TListView(Sender).items.add;
ListItem.Caption := Caption;
ListItem.ImageIndex := ImageIndex;
Delete; //deletes source item after adding target item
end;
end;
//---------------------------------------------------------------------
procedure TForm1.ListView1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
Timer1.enabled := false;
end;
//---------------------------------------------------------------------
// Timer used with all WinControls for scrolling...
//---------------------------------------------------------------------
procedure TForm1.Timer1Timer(Sender: TObject);
begin
//Note: Timer1.interval = 100 but this can changed
//to speed up or slow down the scroll rate.
ImageList1.HideDragImage;
with TargetWin do
if ScrollDirection = sdUp then
sendmessage(handle,WM_VSCROLL,SB_LINEUP,0) else
sendmessage(handle,WM_VSCROLL,SB_LINEDOWN,0);
ImageList1.ShowDragImage;
end;
end.
unit draglistview;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls, ExtCtrls, StdCtrls, ImgList;
type
TScrollDirection = (sdUp, sdDown, sdNone);
TFrm_drag_listview = class(TForm)
TreeView1: TTreeView;
ImageList1: TImageList;
ListView1: TListView;
ListView2: TListView;
Timer1: TTimer;
Button1: TButton;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure FormCreate(Sender: TObject);
procedure ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
procedure Timer1Timer(Sender: TObject);
procedure ListView1EndDrag(Sender, Target: TObject; X, Y: Integer);
procedure TreeView1EndDrag(Sender, Target: TObject; X, Y: Integer);
procedure Button1Click(Sender: TObject);
private
TargetWin: TWinControl;
ScrollDirection: TScrollDirection;
public
{ Public declarations }
end;
var
Frm_drag_listview: TFrm_drag_listview;
implementation
{$R *.DFM}
var
//the maximum distance from the top or bottom of
//the WinControls in which a drag scroll will occur...
SCROLLMARGIN: integer = 18;
//---------------------------------------------------------------------
procedure TFrm_drag_listview.FormCreate(Sender: TObject);
var
rect: TRect;
begin
Treeview1.fullexpand;
//Get the height of a listview item for
//a better value for SCROLLMARGIN...
rect := Listview1.items[0].DisplayRect(drBounds);
SCROLLMARGIN := rect.bottom-rect.Top;
end;
//---------------------------------------------------------------------
procedure TFrm_drag_listview.Button1Click(Sender: TObject);
begin
close;
end;
//---------------------------------------------------------------------
// This demonstrates dragging of nodes within a treeview...
//---------------------------------------------------------------------
procedure TFrm_drag_listview.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
Accept := Source = Sender;
if not Accept then exit;
//See if scrolling is needed...
with TTreeView(Sender) do
begin
if (Y < SCROLLMARGIN) then
ScrollDirection := sdUp
else if (Y > ClientHeight - SCROLLMARGIN) then
ScrollDirection := sdDown
else
ScrollDirection := sdNone;
if ScrollDirection = sdNone then
Timer1.enabled := false
else
begin
TargetWin := TWinControl(Sender);
Timer1.enabled := true;
end;
end;
end;
//---------------------------------------------------------------------
procedure TFrm_drag_listview.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
with TTreeView(Sender) do
begin
if (Sender <> Source) or (DropTarget = nil) or
(DropTarget = Selected) then exit;
Selected.MoveTo(DropTarget,naAddChildFirst);
end;
end;
//---------------------------------------------------------------------
procedure TFrm_drag_listview.TreeView1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
Timer1.enabled := false;
end;
//---------------------------------------------------------------------
// This demonstrates dragging of nodes between listviews...
//---------------------------------------------------------------------
procedure TFrm_drag_listview.ListView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if (Source is TListView) {and (Source <> Sender)} then
Accept := true else
Accept := false;
if not Accept then exit;
//OK, now see if scrolling is needed...
with TListView(Sender) do
begin
if (Y < SCROLLMARGIN*2) then //nb: allow for the listview header
ScrollDirection := sdUp
else if (Y > ClientHeight - SCROLLMARGIN) then
ScrollDirection := sdDown
else
ScrollDirection := sdNone;
if ScrollDirection = sdNone then
Timer1.enabled := false
else
begin
TargetWin := TWinControl(Sender);
Timer1.enabled := true;
end;
end;
end;
//---------------------------------------------------------------------
procedure TFrm_drag_listview.ListView2DragDrop(Sender, Source: TObject; X, Y: Integer);
var
ListItem: TListItem;
begin
Timer1.enabled := false;
if not (Source is TListView) or (Source = Sender) then exit;
with TListView(Source).Selected do
begin
ListItem := TListView(Sender).items.add;
ListItem.Caption := Caption;
ListItem.ImageIndex := ImageIndex;
Delete; //deletes source item after adding target item
end;
end;
//---------------------------------------------------------------------
procedure TFrm_drag_listview.ListView1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
Timer1.enabled := false;
end;
//---------------------------------------------------------------------
// Timer used with all WinControls for scrolling...
//---------------------------------------------------------------------
procedure TFrm_drag_listview.Timer1Timer(Sender: TObject);
begin
//Note: Timer1.interval = 100 but this can changed
//to speed up or slow down the scroll rate.
ImageList1.HideDragImage;
with TargetWin do
if ScrollDirection = sdUp then
sendmessage(handle,WM_VSCROLL,SB_LINEUP,0) else
sendmessage(handle,WM_VSCROLL,SB_LINEDOWN,0);
ImageList1.ShowDragImage;
end;
end.