看我写的例子,注意listview的dragmode改成dmAutomatic
(我没有连接数据库,你可以编程或使用控件来使用数据库,我演示的是drag&drop)
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ComCtrls;
type
TForm1 = class(TForm)
TreeView1: TTreeView;
ListView1: TListView;
procedure ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure ListView1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
procedure TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
var curTreeNode:TTreeNode;
procedure TForm1.ListView1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
LISTVIEW1.BeginDrag(TRUE);
end;
procedure TForm1.ListView1MouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
LISTVIEW1.EndDrag(TRUE);
end;
procedure TForm1.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer;
State: TDragState; var Accept: Boolean);
begin
if source is TLISTVIEW then accept:=true else Accept:=false;
end;
procedure TForm1.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer);
begin
curTreeNode:=treeview1.droptarget;
WITH LISTVIEW1 DO
BEGIN
treeview1.Items.AddChild(curTreeNode,listview1.selected.caption);
END;
end;
end.