Delphi 實現TreeView結點拖拽的實例(轉)


Delphi 實現TreeView結點拖拽的實例(轉)
2010-09-16 21:03

Delphi 實現TreeView結點拖拽的實例

2010-06-04 11:15
轉載自 BD楓楓
最終編輯 BD楓楓

下面的程序片段演示了如何實現拖拽treeview構件結點的例子

{鼠標按下時執行的語句}
procedure TForm1.Treeview1MouseDown(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin

{判斷左鍵按下並且鼠標點在一個結點上開始實現拖拽}
if ( Button = mbLeft ) and
( htOnItem in Treeview1.GetHitTestInfoAt( X, Y ) ) then
begin
Treeview1.BeginDrag( False );
end;
end;

{鼠標拖動執行語句}
procedure TForm1.Treeview1DragOver( Sender, Source: TObject;
X, Y: Integer; State: TDragState; var Accept: Boolean);
var
Node : TTreeNode;
begin
if Source = Treeview1 then
begin
Node := Treeview1.GetNodeAt( X, Y ); {取當前結點}
if Node <> nil then {當前結點不為空才能實現拖拽,accept:=true}

Accept := true;
end;
end;

{鼠標釋放時執行的語句}
procedure TForm1.Treeview1DragDrop( Sender, Source: TObject;
X, Y : Integer );
var
TempNode : TTreeNode;
AttachMode : TNodeAttachMode;
begin
if Treeview1.Selected = nil then
Exit;

AttachMode := naAddChild; {設置結點移動模式,設移動結點為子結點}


{ 注意在這里存在一個bug,當移動結點時,如果目標結點沒有子結點,}
{ 則加入的新的子結點會失敗,所以先在當前目標結點的下面 }
{ 加入一個臨時子結點,移動完畢后,再將臨時結點刪除 }

Treeview1.Items.BeginUpdate;
try
TempNode := Treeview1.Items.AddChild( Treeview1.DropTarget,
'Temp' );
try
{ 移動選中的結點到目標結點 }
Treeview1.Selected.MoveTo( Treeview1.DropTarget, AttachMode );
finally
TempNode.Free; { 不要忘了釋放臨時結點 }
end;
finally
Treeview1.Items.EndUpdate;
end;
end;

詳情地址:

DELPHI中利用TreeView控件建立目錄樹

2010-06-09 15:54
關於TreeView的使用,還可以參看:聯合使用TreeView 組件
   TreeView是一個顯示樹型結構的控件,通過它能夠方便地管理和顯示具有層次結構的信息,是Windows應用程序的基本控件之一。DELPHI雖然具有比較強大的文件管理功能,提供了多個用於文件管理的標准控件,如DriveComboBox、DirectoryListBox、FileListBox等,通過設置它們的屬性,使其建立起聯系,甚至不用編寫一行程序,我們就可以實現在不同的目錄之間進行切換,然而這樣的目錄切換只適用於進行文件的查找定位,而不能方便地進行目錄的瀏覽,例如我們要從c:\windows目錄轉到c:\program files目錄,就必須返回到根目錄才能進行切換,而不能象Windows資源管理器那樣任意地在不同的目錄之間進行瀏覽與切換。

   要實現在不同目錄之間任意切換和瀏覽,還是需要使用TreeView控件,以下程序就利用DELPHI的TreeView控件來建立目錄樹。

   在該程序中采用的各部件以及界面設計如下圖所示:


   各部件的主要屬性設置如下:
部件 屬性 屬性值
form name caption form1
‘目錄瀏覽’
drivecommbobox name
visible
drivecommbobox1
false
filelistbox name
visible
filetype
filelistbox1
false
fddirectory
imagelist name imagelist1
treeview name
images


   該程序利用DriveCommboBox控件來獲得系統具有的驅動器,並以此作為目錄樹的最上層,利用FileListBox控件,通過設置其Filetype屬性為fdDirectory,可以獲得所需的子目錄,在TreeView控件的OnExpanding事件中將得到的子目錄加到該控件的某一節點下。

   整個程序的源代碼如下:

   unit main;

   interface

   uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,StdCtrls, FileCtrl, ComCtrls, ImgList;

   type

   TForm1 = class(TForm)
   DirTreeView: TTreeView;
   FileListBox1: TFileListBox;
   DriveComboBox1: TDriveComboBox;
   ImageList1: TImageList;
   procedure FormCreate(Sender: TObject);
   procedure DirTreeViewExpanding(Sender: TObject; Node: TTreeNode;var AllowExpansion: Boolean);
   private
   { Private declarations }
   public
   { Public declarations }
   end;

   var
   Form1: TForm1;

   implementation
   {$R *.DFM}

   procedure TForm1.FormCreate(Sender: TObject);
   var
   FirstNode,DirNode : TTreeNode;
   ItemCount,Index:integer;
   Itemstr:string;
   begin
   ItemCount:= DriveComboBox1.Items.Count; //所有驅動器的個數
   FirstNode := DirTreeView.Items.GetFirstNode;
   for index := 0 to ItemCount -1 do
   begin
   ItemStr:= DriveComboBox1.Items[index];
   ItemStr:= copy(ItemStr,1,pos(:,ItemStr)) ; //獲得驅動器的名稱(比如C/D)
   DirNode := DirTreeView.Items.AddChild(FirstNode, ItemStr );
   DirNode.HasChildren := true;
  DirNode.ImageIndex := 0;
   DirNode.SelectedIndex := 1;
   end;
   end;

//響應擴展事件
   procedure TForm1.DirTreeViewExpanding(Sender: TObject; Node: TTreeNode;Var AllowExpansion: Boolean);
   var
   DirNode : TTreeNode;
   ItemCount,Index,level,icount:integer;
   Itemstr,strPath:string;
   begin
   if node.Count = 0 then
   begin
   icount:=0;
   level:=node.Level ;
   dirnode:=node;
   strPath:=node.Text+\ ;
   while level 0 do
   begin
   strPath:=dirnode.Parent.Text+\+strpath;
   dirnode:=dirnode.parent;
   level :=level -1;
   end;

   FileListBox1.Clear ;
   FileListBox1.Directory := strpath;
   ItemCount:= FileListBox1.Items.Count;

  for index:=0 to ItemCount -1 do
   begin
   itemstr:=filelistbox1.items[index];
   itemstr:= copy(ItemStr,2,pos(],ItemStr)-2) ;
   if (itemstr〈〉.) and (itemstr 〈〉 ..) then
   begin
   DirNode := DirTreeView.Items.AddChild(Node,itemstr );
   DirNode.HasChildren :=true;
   DirNode.ImageIndex := 0;
   DirNode.SelectedIndex := 1;
   icount:=icount+1;
   end;

   if icount = 0 then
Node.HasChildren := false;
  end;
   end;
   end;
   end.



   程序的運行效果如圖所示:我們可以展開目錄樹中的任何一個節點,並且可以在任意節點之間切換,就象我們在Windows資源管理器中所作的那樣,而不需要逐級回退之后才能進行切換。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM