unit SearchTool;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ImgList, StdCtrls, ComCtrls, FileCtrl, StrUtils;
type
TSearchDlg = class(TForm)
DriveComboBox: TDriveComboBox;
FileListBox: TFileListBox;
DirTreeView: TTreeView;
FileList: TListBox;
BtnSerach: TButton;
FilterType: TComboBox;
ImageList: TImageList;
procedure FromOnCreate(Sender: TObject);
procedure DirTreeViewExpanding(Sender: TObject; Node: TTreeNode;
var AllowExpansion: Boolean);
procedure BtnSerachClick(Sender: TObject);
procedure FindFile(Dir: String);
private
{ Private declarations }
public
{ Public declarations }
end;
var
SearchDlg: TSearchDlg;
FileExt: String;
implementation
{$R *.dfm}
//窗口创建时候同时生成文件目录树
procedure TSearchDlg.FromOnCreate(Sender: TObject);
var
FirstNode,DirNode: TTreeNode;
ItemCount,index: Integer;
strItem: String;
begin
ItemCount:= DriveComboBox.Items.Count;
FirstNode:= DirTreeView.Items.GetFirstNode;
for index:= 0 to ItemCount - 1 do
begin
StrItem:= DriveComboBox.Items[index];
StrItem:= copy(strItem, 1, pos(':', strItem));
DirNode:= DirTreeView.Items.AddChild(FirstNode, strItem);
DirNode.HasChildren:= true;
DirNode.ImageIndex:= 0;
DirNode.SelectedIndex:= 1;
end;
end;
//目录树展开事件
procedure TSearchDlg.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;
end;
//通过FileListBox控件来获取子目录
FileListBox.Clear;
FileListBox.Directory:= strPath;
ItemCount:= FileListBox.Items.Count;
for index:= 0 to ItemCount -1 do
begin
Itemstr:= FileListBox.Items[index];
Itemstr:= copy(ItemStr, 2, pos(']', ItemStr)-2);
if(Itemstr <> '.') and (Itemstr <> '..') and (Itemstr <> '') then
begin
DirNode:= DirTreeView.Items.AddChild(Node, Itemstr);
DirNode.HasChildren:= true;
DirNode.ImageIndex:= 0;
DirNode.SelectedIndex:= 1;
icount:= icount + 1;
end;
end;
if icount = 0 then
Node.HasChildren:= false;
end;
//扫描按纽点击事件
procedure TSearchDlg.BtnSerachClick(Sender: TObject);
var
strPath: String;
level,find: Integer;
i,MaxWidth: Integer;
DirNode: TTreeNode;
begin
DirNode:= DirTreeView.Selected;
if(DirNode = nil) then
exit;
level:= DirNode.Level;
strPath:= DirNode.Text + '\';
while level <> 0 do
begin
strPath:= DirNode.Parent.Text + '\' + strPath;
DirNode:= DirNode.Parent;
level:= level -1;
end;
FileList.Clear;
FileExt:= FilterType.Text; //获取后缀名
if(FileExt = '所有文件(*.*)') or (FileExt = '') then
FileExt:= '.*';
//查找该目录下相应后缀名的所有文件
FindFile(strPath);
//横向滚动条的添加
MaxWidth:= 0;
for i:= 0 to FileList.Items.Count - 1 do
if MaxWidth < FileList.Canvas.TextWidth(FileList.Items.Strings[i]) then
MaxWidth := FileList.Canvas.TextWidth(FileList.Items.Strings[i]);
SendMessage(FileList.Handle, LB_SETHORIZONTALEXTENT, MaxWidth + 50, 0);
//文件目录树设置焦点
DirTreeView.SetFocus;
end;
//采用递归方式进行子目录文件查找
procedure TSearchDlg.FindFile(Dir: String);
var
SearchRec: TSearchRec;
find: Integer;
begin
//查找目录所有文件
find:= FindFirst(Dir + '*.*', faAnyFile, SearchRec);
while find = 0 do
begin
if(SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
if(Searchrec.Attr <> faDirectory) then
begin
//过滤后缀名文件,采用字符串处理函数SameText和截取函数RightStr
if(FileExt = '.*') then
FileList.Items.Add(SearchRec.Name)
else if SameText(RightStr(SearchRec.Name, Length(FileExt)), FileExt) then
FileList.Items.Add(SearchRec.Name)
end
else
//递归查找子目录的文件
FindFile(Dir + SearchRec.Name + '\');
find:= FindNext(SearchRec);
end;
//释放资源
FindClose(SearchRec);
end;
end.