天天看點

WinForm下的TreeView拖拽操作

http://www.cnblogs.com/zfqbt/archive/2009/12/17/1626582.html

//開始拖動操作事件

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

;

}

}