WinForm下的TreeView拖拽操作

 

   //开始拖动操作事件 
         private void TreeView_ItemDrag(object sender, ItemDragEventArgs e) 
         { 
             TreeNode tn = e.Item as TreeNode; 
             if ((e.Button == MouseButtons.Left) && (tn != null) && (tn.Parent != null)) //根节点不允许拖放操作。 
             { 
                 this.treeView.DoDragDrop(tn, DragDropEffects.Copy | DragDropEffects.Move | DragDropEffects.Link); 
             } 
  
         } 
  
         //是否允许拖放操作继续检查事件 
         private void TreeView_QueryContinueDrag(object sender, QueryContinueDragEventArgs e) 
         { 
             //例如,当光标悬停在 TreeView 控件上时,展开该控件中的 TreeNode 
             //this.textBox1.Text = p.ToString(); 
         } 
  
         private void TreeView_DragEnter(object sender, DragEventArgs e) 
         { 
             //e.Effect = e.AllowedEffect; 
  
         } 
  
         private void TreeView_DragOver(object sender, DragEventArgs e) 
         { 
             //当光标悬停在 TreeView 控件上时,展开该控件中的 TreeNode 
             Point p = this.treeView.PointToClient(Control.MousePosition); 
             TreeNode tn = this.treeView.GetNodeAt(p); 
             if (tn != null) 
             { 
                 if (this.dragDropTreeNode != tn) //移动到新的节点 
                 { 
                     if (tn.Nodes.Count > 0 && tn.IsExpanded == false) 
                     { 
                         this.startTime = DateTime.Now;//设置新的起始时间 
                     } 
                 } 
                 else 
                 { 
                     if (tn.Nodes.Count > 0 && tn.IsExpanded == false && this.startTime != DateTime.MinValue) 
                     { 
                         TimeSpan ts = DateTime.Now - this.startTime; 
                         if (ts.TotalMilliseconds >= 1000) //一秒 
                         { 
                             tn.Expand(); 
                             this.startTime = DateTime.MinValue; 
                         } 
                     } 
                 } 
  
             } 
             //设置拖放标签Effect状态 
             if (tn != null )//&& (tn != this.treeView.SelectedNode)) //当控件移动到空白处时,设置不可用。 
             { 
                 if ((e.AllowedEffect & DragDropEffects.Move) != 0) 
                 { 
                     e.Effect = DragDropEffects.Move; 
                 } 
                 if (((e.AllowedEffect & DragDropEffects.Copy) != 0) && ((e.KeyState & 0x08) != 0))//Ctrl key 
                 { 
                     e.Effect = DragDropEffects.Copy; 
                 } 
                 if (((e.AllowedEffect & DragDropEffects.Link) != 0) && ((e.KeyState & 0x08) != 0) && ((e.KeyState & 0x04) != 0))//Ctrl key + Shift key 
                 { 
                     e.Effect = DragDropEffects.Link; 
                 } 
                 if (e.Data.GetDataPresent(typeof(TreeNode)))//拖动的是TreeNode 
                 { 
                      
                     TreeNode parND = tn;//判断是否拖到了子项 
                     bool isChildNode = false; 
                     while (parND.Parent != null ) 
                     { 
                         parND = parND.Parent; 
                         if(parND == this.treeView.SelectedNode) 
                         { 
                             isChildNode = true; 
                             break; 
                         } 
                     } 
                     if (isChildNode)  
                     { 
                         e.Effect = DragDropEffects.None; 
                     } 
                 } 
                 else if (e.Data.GetDataPresent(typeof(ListViewItem)))//拖动的是ListViewItem 
                 { 
                     if (tn.Parent == null)  
                     { 
                         e.Effect = DragDropEffects.None; 
                     } 
                 } 
             } 
             else 
             { 
                 e.Effect = DragDropEffects.None; 
             } 
  
             //设置拖放目标TreeNode的背景色 
             if (e.Effect == DragDropEffects.None) 
             { 
                 if (this.dragDropTreeNode != null) //取消被放置的节点高亮显示 
                 { 
                     this.dragDropTreeNode.BackColor = SystemColors.Window; 
                     this.dragDropTreeNode.ForeColor = SystemColors.WindowText; 
                     this.dragDropTreeNode = null; 
                 } 
             } 
             else 
             { 
                 if (tn != null) 
                 { 
                     if (this.dragDropTreeNode != null) 
                     { 
                         if (this.dragDropTreeNode != tn) 
                         { 
                             this.dragDropTreeNode.BackColor = SystemColors.Window;//取消上一个被放置的节点高亮显示 
                             this.dragDropTreeNode.ForeColor = SystemColors.WindowText; 
                             this.dragDropTreeNode = tn;//设置为新的节点 
                             this.dragDropTreeNode.BackColor = SystemColors.Highlight; 
                             this.dragDropTreeNode.ForeColor = SystemColors.HighlightText; 
                         } 
                     } 
                     else 
                     { 
                         this.dragDropTreeNode = tn;//设置为新的节点 
                         this.dragDropTreeNode.BackColor = SystemColors.Highlight; 
                         this.dragDropTreeNode.ForeColor = SystemColors.HighlightText; 
                     } 
                 } 
             } 
         } 

   //随着鼠标的移动显示不同的光标效果 
         private void TreeView_GiveFeedback(object sender, GiveFeedbackEventArgs e) 
         { 
             //e.UseDefaultCursors = true; 
              
             // Use custom cursors if the check box is checked. 
             //if (UseCustomCursorsCheck.Checked) 
             //{ 
             //int i = 0; 
             // Sets the custom cursor based upon the effect. 
             //e.UseDefaultCursors = false; 
             if ((e.Effect & DragDropEffects.Move) == DragDropEffects.Move) 
             { 
  
                 //Cursor d = new Cursor(""); 
                 //Cursor.Current = MyNormalCursor; 
             } 
             else 
             { 
                 //Cursor.Current = MyNoDropCursor; 
             } 
             //} 
  
         } 
  
         private void TreeView_DragDrop(object sender, DragEventArgs e) 
         { 
             if (this.dragDropTreeNode != null) 
             { 
                 if (e.Data.GetDataPresent(typeof(TreeNode))) 
                 { 
                      
                     TreeNode tn = (TreeNode)e.Data.GetData(typeof(TreeNode)); 
                     tn.Remove();//从原父节点移除被拖得节点 
                     this.dragDropTreeNode.Nodes.Add(tn);//添加被拖得节点到新节点下面 
                     Category category = (Category)tn.Tag; 
                     if (this.dragDropTreeNode.Parent == null) 
                     { 
                         category.ParentID = 0; 
                     } 
                     else 
                     { 
                         category.ParentID = ((Category)this.dragDropTreeNode.Tag).CategoryID;  
                     } 
                     //更新节点移动到数据库 
                     DocumentController.GetInstance().UpdateCategoryParent(category); 
                     if (this.dragDropTreeNode.IsExpanded == false) 
                     { 
                         this.dragDropTreeNode.Expand();//展开节点 
                     } 
  
                 } 
                 else if (e.Data.GetDataPresent(typeof(ListViewItem)))  
                 { 
                     if (this.dragDropTreeNode.Parent != null) 
                     { 
                         int categoryID = ((Category)this.dragDropTreeNode.Tag).CategoryID; 
                         ListViewItem listViewItem = (ListViewItem)e.Data.GetData(typeof(ListViewItem)); 
                         Item item = (Item)listViewItem.Tag; 
                         DocumentController.GetInstance().UpdateItemCategory(item.ItemID, categoryID); 
                         listViewItem.Remove(); 
                     }                     
                 } 
                 //取消被放置的节点高亮显示 
                 this.dragDropTreeNode.BackColor = SystemColors.Window; 
                 this.dragDropTreeNode.ForeColor = SystemColors.WindowText; 
                 this.dragDropTreeNode = null; 
             } 
         } 
  
         private void TreeView_DragLeave(object sender, EventArgs e) 
         { 
             if (this.dragDropTreeNode != null) //在按下{ESC},取消被放置的节点高亮显示 
             { 
                 this.dragDropTreeNode.BackColor = SystemColors.Window; 
                 this.dragDropTreeNode.ForeColor = SystemColors.WindowText; 
                 this.dragDropTreeNode = null; 
             } 
         } 
posted @ 2009-12-17 17:36  哈子竹  阅读(5345)  评论(0编辑  收藏  举报