有两个treeview控件和一个ListView控件,暂且记tvA、tvB和lvC吧,从tvA中拖动文件夹到tvB中,然后将拖动文件夹中的所有文件显示在lvC中
tvA显示某路径下的所有文件,tvB是某项目分组,从tvA中拖动文件夹到tvB中时将拖动文件夹中的所有文件(非文件夹)显示到lvC中;tvA和tvB之前的拖动可以实现,关键是一旦拖动到tvB中后如何将拖动文件夹中所有的文件获取到。
希望各位大侠指点下 ^^
tvA显示某路径下的所有文件,tvB是某项目分组,从tvA中拖动文件夹到tvB中时将拖动文件夹中的所有文件(非文件夹)显示到lvC中;tvA和tvB之前的拖动可以实现,关键是一旦拖动到tvB中后如何将拖动文件夹中所有的文件获取到。
希望各位大侠指点下 ^^
1.拖拽功能
2.根据文件夹遍历所有文件拖拽功能
将treeview的dragmode设置为dmAutomatic
在
procedure TForm.TreeViewDragOver事件中谁便写点什么。procedure TForm.TreeViewDragDrop(Sender, Source: TObject; X,
Y: Integer);
var
VL_Node, VL_tmpNode: TTreeNode;
VL_I: integer;
begin
if (TreeView.Selected <> nil) and (TreeView.Selected.level > 0) then
begin
VL_Node := TreeView.GetNodeAt(X, Y);
if VL_Node <> nil then
begin
if TreeView.Selected.Parent = VL_Node then Exit;
if TreeView.Selected = VL_Node then Exit;
if TreeView.Selected.Level < VL_Node.Level then
begin
VL_tmpNode := VL_Node;
for VL_I := 0 to VL_Node.Level - TreeView.Selected.Level - 1 do
begin
VL_tmpNode := VL_tmpNode.Parent;
end;
if TreeView.Selected = VL_tmpNode then Exit;
end; if Application.MessageBox('是否确认要移动该节点?', '确认信息', mb_YESNO or mb_ICONQUESTION) <> IDYES then Exit;
TreeView.Selected.MoveTo(VL_Node, naADDChild);
end;
end;
end;遍历功能function ListDirs(Path: string; List: TStringList): Integer;
var
FindData: TWin32FindData;
FindHandle: THandle;
FileName: string;
AddToList: Boolean;
begin
Result := 0;
AddToList := Assigned(List); if Path[Length(Path)] <> '\' then
Path := Path + '\'; Path := Path + '*.*'; FindHandle := Windows.FindFirstFile(PChar(Path), FindData);
while FindHandle <> INVALID_HANDLE_VALUE do
begin
FileName := StrPas(FindData.cFileName);
if (FileName <> '.') and (FileName <> '..') and
((FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0) then
begin
Inc(Result);
if AddToList then
List.Add(FileName);
end; if not Windows.FindNextFile(FindHandle, FindData) then
FindHandle := INVALID_HANDLE_VALUE;
end;
Windows.FindClose(FindHandle);
end;使用:
var
s: TStringList;
begin
s := TStringList.Create;
ListDirs('c:\windows\', s);
ListBox1.Items.AddStrings(s);
s.Free;
end;
procedure FileSearch(PathName:string);
var
F : TSearchRec;
Found : Boolean;
begin
ChDir(PathName);
Found := (FindFirst('*.*', faAnyFile, F) = 0);
while Found do
begin
if (F.Name = '.') or (F.Name = '..') then
begin
Found := (FindNext(F) = 0);
Continue;
end; if (F.Attr and faDirectory)>0 then
begin
Application.ProcessMessages;
FileSearch(F.Name);
end;
//插入你的代码,F.Name就是文件名,GetCurrentDir可以得到当前目录
Found := (FindNext(F) = 0);
end;
FindClose(F);
ChDir('..\');
end;
稍加修改后得到了想要的答案,^^
function TFileTree.GetDirectories(List: TRzListView; Directory: string;
AStr: TStringList): Integer;
var
FindData: TWin32FindData;
FindHandle: THandle;
FileName: string;
AddToList: Boolean;
Item: TListItem;
DirTemp: string;
begin
Result := List.Items.Count;
AddToList := Assigned(AStr); if Directory[Length(Directory)] <> '\' then
Directory := Directory + '\'; Directory := Directory + '*.*'; FindHandle := Windows.FindFirstFile(PChar(Directory), FindData);
while FindHandle <> INVALID_HANDLE_VALUE do
begin
FileName := StrPas(FindData.cFileName);
if (FileName <> '.') and (FileName <> '..') and
((FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) <> 0) then
begin
DirTemp := Copy(Directory, 0, Length(Directory) - Length('*.*'));
GetDirectories(List, DirTemp + '\' + FileName, AStr);
end
else if (FileName <> '.') and (FileName <> '..') and
((FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY) = 0) then
begin
Inc(Result);
if AddToList then
begin
Item := List.Items.Add;
Item.Caption := IntToStr(Result);
Item.SubItems.Add(FileName);
end;
end; if not Windows.FindNextFile(FindHandle, FindData) then
FindHandle := INVALID_HANDLE_VALUE;
end;
Windows.FindClose(FindHandle);
end;
这样就可以将文件夹中的所有文件都遍历到了,包括文件夹中的子文件夹中的文件也可以获得。