TreeView 拖放事件參考寫法 |
|
sryang
尊榮會員 發表:39 回覆:762 積分:920 註冊:2002-06-27 發送簡訊給我 |
拖放時功能:
<textarea cols="80" rows="130" class="delphi" name="code"> procedure TfmERingNG.TreeView1DragOver(Sender, Source: TObject; X, Y: Integer; State: TDragState; var Accept: Boolean); var AnItem: TTreeNode; i, j, iBegin, iEnd, iLevel: Integer; Pt: TPoint; StartTime, EndTime: TDateTime; begin if TreeView1.Selected = nil then Exit; iBegin := TreeView1.Selected.AbsoluteIndex; iLevel := TreeView1.Selected.Level; i := iBegin; while i < TreeView1.Items.Count - 1 do begin i := i 1; if TreeView1.Items[i].Level <= iLevel then Break; end; if i <= TreeView1.Items.Count - 1 then iEnd := i - 1 else iEnd := i; AnItem := TreeView1.GetNodeAt(X, Y); Accept := (Source = TreeView1) and (AnItem <> nil) and not ((AnItem.AbsoluteIndex >= iBegin) and (AnItem.AbsoluteIndex <= iEnd)); if AnItem = nil then Exit; if not AnItem.Expanded and AnItem.HasChildren then begin if (State = dsDragMove) and AnItem.DropTarget then begin StartTime := Now; GetCursorPos(Pt); Pt := ScreenToClient(Pt); TreeView1.RePaint; while AnItem = TreeView1.GetNodeAt(Pt.x, Pt.y) do begin GetCursorPos(Pt); Pt := ScreenToClient(Pt); if Now - StartTime > 0.000001 then Break; end; EndTime := Now; if EndTime - StartTime > 0.000001 then begin AnItem.Expand(False); TreeView1.RePaint; end; end; end; while (Y > TreeView1.Height - GetSystemMetrics(SM_CYHSCROLL) - 5) and (Y <= TreeView1.Height) do begin GetCursorPos(Pt); Pt := ScreenToClient(Pt); Y := Pt.y; i := TreeView1.TopItem.AbsoluteIndex; j := 1; while (i j <= TreeView1.Items.Count) and (TreeView1.Items[i j].Parent <> nil) and (not TreeView1.Items[i j].Parent.Expanded) do j := j 1; TreeView1.TopItem := TreeView1.Items[i j]; TreeView1.RePaint; if i = TreeView1.TopItem.AbsoluteIndex then Break; Sleep(50); end; while (Y < 20) and (Y >= 0) do begin GetCursorPos(Pt); Pt := ScreenToClient(Pt); Y := Pt.y; i := TreeView1.TopItem.AbsoluteIndex; j := 1; while (i - j >= 0) and (TreeView1.Items[i - j].Parent <> nil) and (not TreeView1.Items[i - j].Parent.Expanded) do j := j 1; if i > 0 then begin TreeView1.TopItem := TreeView1.Items[i - j]; TreeView1.RePaint; end; if i = 0 then Break; Sleep(50); end; end; procedure TfmERingNG.TreeView1DragDrop(Sender, Source: TObject; X, Y: Integer); var AnItem: TTreeNode; AttachMode: TNodeAttachMode; HT: THitTests; begin if TreeView1.Selected = nil then Exit; HT := TreeView1.GetHitTestInfoAt(X, Y); AnItem := TreeView1.GetNodeAt(X, Y); if AnItem = nil then Exit; if (HT - [htOnItem, htOnRight, htOnIcon, htNowhere, htOnIndent] <> HT) then begin AttachMode := naInsert; if (htOnItem in HT) or (htOnRight in HT) or (htOnIcon in HT) then AttachMode := naAddChild else if htNowhere in HT then AttachMode := naAdd else if htOnIndent in HT then AttachMode := naInsert; TreeView1.Selected.MoveTo(AnItem, AttachMode); end; end; </textarea>
------
歡迎參訪 "腦殘賤貓的備忘錄" http://maolaoda.blogspot.com/ |
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
|
it1506
初階會員 發表:33 回覆:89 積分:49 註冊:2011-02-16 發送簡訊給我 |
|
P.D.
版主 發表:603 回覆:4038 積分:3874 註冊:2006-10-31 發送簡訊給我 |
|
sryang
尊榮會員 發表:39 回覆:762 積分:920 註冊:2002-06-27 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |