下面的程序用于treeview的拖放控制,应该能满足你的需要.
var
AnItem: TTreeNode;
AttachMode: TNodeAttachMode;
HT: THitTests;
{ 获取鼠标点击区域的信息
THitTest =(htAbove, //客户区域的上部
htBelow, //客户区域的下部
htNowhere, //非客户区域
htOnItem, //点击了项目的Item or Text or bitmap
htOnButton, //点击了项目的Button
htOnIcon, //点击了项目的Icon
htOnLabel, //点击了项目的Label
htOnRight, //点击了项目的Right
htOnIndent, //点击了客户区域的无效部位
htOnStateIcon, //点击了与项目关联的Icon or Bitmap
htToLeft, //项目的Left
htToRight); //项目的Right
}
begin
if TreeView1.Selected = nil then Exit;
{ 获取当前点击处的信息}
HT := TreeView1.GetHitTestInfoAt(X, Y);
{ 取当前获得焦点的Node }
AnItem := TreeView1.GetNodeAt(X, Y);
if (HT - [htOnItem, htOnIcon, htNowhere, htOnIndent] <> HT) then
begin
if (htOnItem in HT) or (htOnIcon in HT) then
begin
AttachMode := naAddChild; //当作子Node Mode
caption :='naAddChild';
end else if htNowhere in HT then
begin
AttachMode := naAdd; //新建Node Mode
Caption :='naAdd';
end else if htOnIndent in HT then
begin
AttachMode := naInsert; //插入Mode,即移动位置。
Caption :='naInsert';
end;
Treeview1.Selected.MoveTo(AnItem, AttachMode); //移动Selected Node to 获得焦点的位置
// if treeview1.Selected.Level>anitem.Level then
// treeview1.Selected.MoveTo(AnItem, AttachMode)
// else showmessage('not');
end;
end;