原創(chuàng)|其它|編輯:郝浩|2012-10-11 16:32:41.000|閱讀 3272 次
概述:本人最近在做某個(gè)項(xiàng)目中需要用到動(dòng)態(tài)設(shè)置TreeList節(jié)點(diǎn)的技術(shù),這一篇主要記錄TreeList的拖拽功能。內(nèi)容主要分為ImageListBoxControl 至 TreeList和TreeList至LabelControl兩部分。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
本人最近在做某個(gè)項(xiàng)目中需要用到動(dòng)態(tài)設(shè)置TreeList節(jié)點(diǎn)的技術(shù),在這個(gè)過程中對(duì)于TreeList的各項(xiàng)功能有了初步的嘗試,準(zhǔn)備分幾篇內(nèi)容將其記錄下來。這一篇主要記錄TreeList的拖拽功能。
一、ImageListBoxControl 至 TreeList
在Visual Studio新建一個(gè)Form,并且將ImageListBoxControl和TreeList兩個(gè)控件分別拖到這個(gè)Form上,設(shè)置相關(guān)屬性.涉及到拖拽必然會(huì)有源控件和目標(biāo)控件,需要設(shè)置目標(biāo)控件的AllowDrop屬性為True,在此處就需要設(shè)置TreeList的AllowDrop屬性為True。
下面開始ImageListBoxControl的初始化操作。我們模仿邏輯運(yùn)算符,添加3個(gè)“與、或、非”的邏輯圖標(biāo)到ImageListBoxControl。
首先,需要為ImageListBoxControl新增三個(gè)事件相應(yīng)函數(shù),分別為MouseDown,MouseMove和GiveFeedback。這三個(gè)函數(shù)的含義為MouseDown和MouseMove表示鼠標(biāo)的按鍵和拖動(dòng)的事件,GiveFeedback表示在鼠標(biāo)拖動(dòng)時(shí),控件給予系統(tǒng)請(qǐng)求的響應(yīng)。實(shí)現(xiàn)代碼如下:
private void imageListBoxControl1_MouseDown(object sender, MouseEventArgs e) { //獲取當(dāng)前鼠標(biāo)選擇的項(xiàng)目Index int index = imageListBoxControl1.IndexFromPoint(new Point(e.X, e.Y)); if (index >= 0) { //根據(jù)index獲取選擇的Item newItem = imageListBoxControl1.Items[index]; } } private void imageListBoxControl1_MouseMove(object sender, MouseEventArgs e) { if (newItem == null || e.Button != MouseButtons.Left) return; //ImageListBoxControl拖拽響應(yīng)事件,LogicDragObject為自定義的邏輯操作Item類。 imageListBoxControl1.DoDragDrop(new LogicDragObject(newItem.ImageIndex,++groupNum), DragDropEffects.Copy); } private void imageListBoxControl1_GiveFeedback(object sender, GiveFeedbackEventArgs e) { e.UseDefaultCursors = false; }
其次,新增TreeList控件的DragDrop、GiveFeedback和DragOver三個(gè)響應(yīng)事件。
DragDrop的事件部分代碼如下:
TreeListHitInfo hi = treeList1.CalcHitInfo(treeList1.PointToClient(new Point(e.X, e.Y))); LogicDragObject dobj = GetDragObject(e.Data); //拖放邏輯操作符 TreeListNode node = hi.Node; if (hi.HitInfoType == HitInfoType.Empty || node != null) { node = treeList1.AppendNode(dobj.LogicDragData, node); node.StateImageIndex = dobj.ImageIndex; treeList1.MakeNodeVisible(node); }
拖拽的本質(zhì)就是由CalcHitInfo獲取當(dāng)前放置的節(jié)點(diǎn),由GetDragObject(e.Data)獲取到需要放置的Data,通過AppendNode添加Node,最后MakeNodeVisible使Node可見。
二、TreeList至LabelControl
現(xiàn)在我想實(shí)現(xiàn)以拖拽的方式刪除樹節(jié)點(diǎn)的操作。首先,放置一個(gè)LabelControl,并且將其設(shè)置為回收站的圖標(biāo)。和上面一樣,需要將LabelControl的AllowDrop屬性設(shè)置為True。然后需要實(shí)現(xiàn)DragDrop、DragEnter和DragLeave三個(gè)響應(yīng)事件。
DragDrop的部分實(shí)現(xiàn)代碼如下:
//獲取需要?jiǎng)h除的樹節(jié)點(diǎn),e.Data為獲取到的TreeList的拖拽節(jié)點(diǎn) TreeListNode node = GetDragNode(e.Data); if (node != null) { treeList1.DeleteNode(node); } SetDefaultLabel();
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:ltrelax的專欄-CSDN