如何实现从ListView拖入到TreeView

rt
---------------------------------------------------------------

参考:
ms-help://MS.VSCC.2003/MS.MSDNQTR.2003FEB.2052/cpref/html/frlrfSystemWindowsFormsControlClassDragDropTopic.htm
---------------------------------------------------------------

#region //导航叶开始拖动
private void listViewNavigator_ItemDrag(object sender, System.Windows.Forms.ItemDragEventArgs e)
{
//鼠标右键禁止拖动
if ( e.Button == MouseButtons.Right ) return ;
//无选择项禁止拖动
if ( listViewNavigator.SelectedItems.Count == 0 ) return ;
//开始拖放操作
this.listViewNavigator.DoDragDrop(Pub.NavigatorDragString, DragDropEffects.Copy ¦ DragDropEffects.Move ) ;
}
#endregion

#region //导航叶拖动项抵达导航树
private void treeViewNavigator_DragEnter(object sender, System.Windows.Forms.DragEventArgs e)
{
//判断是否目前拖动的数据是指定的字符串
string DragTagString = (string)e.Data.GetData("Temp".GetType()) ;

if ( DragTagString == Pub.NavigatorDragString )
e.Effect = DragDropEffects.Copy ;
else
e.Effect = DragDropEffects.None ;
}
#endregion

#region //导航叶拖动项在导航树中移动
private void treeViewNavigator_DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
//判断是否目前拖动的数据是指定的字符串
string DragTagString = (string)e.Data.GetData("Temp".GetType()) ;
if ( DragTagString != Pub.NavigatorDragString )
{
e.Effect = DragDropEffects.None ;
return ;
}

//导航树自动滚动
Point pt = this.treeViewNavigator.PointToClient(new Point(e.X, e.Y)) ;
int h = 10;
if ( pt.Y + h > this.treeViewNavigator.ClientSize.Height )
API.SendMessage(this.treeViewNavigator.Handle, API.WM_VSCROLL, API.SB_LINEDOWN, 0) ;
else if ( pt.Y < h )
API.SendMessage(this.treeViewNavigator.Handle, API.WM_VSCROLL, API.SB_LINEUP, 0) ;

if ( pt.X + h > this.treeViewNavigator.ClientSize.Width )
API.SendMessage(this.treeViewNavigator.Handle, API.WM_HSCROLL, API.SB_LINERIGHT, 0) ;
else if ( pt.X < h )
API.SendMessage(this.treeViewNavigator.Handle, API.WM_HSCROLL, API.SB_LINELEFT, 0) ;

//获取鼠标指针位置
Point Position = new Point(0, 0) ;
Position.X = e.X ;
Position.Y = e.Y ;
Position = treeViewNavigator.PointToClient(Position) ;

TreeNode DropNode = this.treeViewNavigator.GetNodeAt(Position) ;
if ( DropNode != null )
{
TreeViewColorReset(this.treeViewNavigator) ;

//延时自动展开当前节点
Pub.MarkNode = DropNode ;
this.timerExpandNode.Enabled = true ;

//标记当前节点颜色
DropNode.BackColor = Color.Navy ;
DropNode.ForeColor = Color.White ;

e.Effect = DragDropEffects.Copy ;
}
else
{
TreeViewColorReset(this.treeViewNavigator) ;
e.Effect = DragDropEffects.None ;
}
}
#endregion

#region //拖动项离开导航树
private void treeViewNavigator_DragLeave(object sender, System.EventArgs e)
{
//重置树节点颜色
TreeViewColorReset(this.treeViewNavigator) ;
}
#endregion

#region //导航叶拖动项放入导航树
private void treeViewNavigator_DragDrop(object sender, System.Windows.Forms.DragEventArgs e)
{
//获取鼠标指针位置上的树节点
Point Position = new Point(0, 0) ;
Position.X = e.X ;
Position.Y = e.Y ;
Position = treeViewNavigator.PointToClient(Position) ;
TreeNode DropNode = this.treeViewNavigator.GetNodeAt(Position) ;

//移动源与移动目标相同
if ( DropNode.Tag == this.treeViewNavigator.SelectedNode.Tag )
return ;

//确认移动
if ( DialogResult.Cancel == MessageBox.Show("是否真的将所选项移动到导航语音目录 [ " + DropNode.Text + " ] ?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) )
{
TreeViewColorReset(this.treeViewNavigator) ; //重置树节点颜色
return ;
}

//添加所选项到指定导航树节点
XmlDocument doc = new XmlDocument();
doc.Load(Pub.XMLNavigator);

//获取拖动目标树节点
string tagPath = "//Navigator[@ID="" + DropNode.Tag.ToString() + ""]" ;
XmlNode tagNode = doc.DocumentElement.SelectSingleNode(tagPath) ;

//循环处理选择项
for ( int i = 0; i < listViewNavigator.SelectedItems.Count; i++ )
{
//获取当前XML叶节点
string XPath = "//Navigator[@ID="" + listViewNavigator.SelectedItems[i].Tag.ToString() + ""]" ;
XmlNode curNode = doc.DocumentElement.SelectSingleNode(XPath) ;
//克隆当前XML叶节点
XmlNode cpyNode = curNode.CloneNode(false) ;
//删除当前XML叶节点
curNode.ParentNode.RemoveChild(curNode) ;
//添加克隆XML叶节点到新位置
tagNode.AppendChild(cpyNode) ;
}

doc.Save(Pub.XMLNavigator) ;

//删除ListView导航叶
while ( listViewNavigator.SelectedItems.Count != 0 )
this.listViewNavigator.SelectedItems[0].Remove() ;

//确定焦点
if ( this.listViewNavigator.Items.Count == 0 )
this.treeViewNavigator.Select() ;
else
{
listViewNavigator.Items[listViewNavigator.Items.Count - 1].Focused= true ;
listViewNavigator.Items[listViewNavigator.Items.Count - 1].Selected = true ;
listViewNavigator.Select() ;
}

//重置树节点颜色
TreeViewColorReset(this.treeViewNavigator) ;
}
#endregion

Published At
Categories with Web编程
Tagged with
comments powered by Disqus