要求:在Form中放两个TTreeView (tv_FileName, tv_URLName)控制,一个菜单并加一个菜单项mnup_Get。
您将下面的主要代码贴到这个菜单项的OnClick事件中就行了。至如结果怎么样,嘻嘻,你试过就知道的喔.....
如下是主要的代码
const
IMAGENUM_BOOKCLOSE = 0; //关闭的类型标志
IMAGENUM_BOOKOPEN = 1; //网络地址标志
IMAGENUM_URLPAGE = 2; //打开的类型标志
type
Tdlg_URLTree = class(TForm)
tv_FileName: TTreeView; //存放目录或文件名的控件
tv_URLName: TTreeView; //存放URL地址的控制
mnup_Get: TMenuItem; //菜单项
...
var
dlg_URLTree: Tdlg_URLTree;
implementation
uses IniFiles;
procedure Tdlg_URLTree.mnup_GetClick(Sender: TObject);
//取得网址的函数
procedure FF_GetDirFileName(strPath: String; //路径
tvFile, //存放目录或文件名的TTreeView控件
tvURL: TTreeView); //存放URL名的TTreeView控件
var
intRet: Integer;
DirInfo: TSearchRec;
strIniFile: TIniFile;
buffer: String;
tnNew: TTreeNode;
begin
intRet := FindFirst(strPath + '/*.*', FaAnyfile, DirInfo);
while intRet = 0 do
begin
if (DirInfo.Name <> '.') and //滤掉 .目录
(DirInfo.Name <> '..') and //滤掉 ..目录
(Pos('.lnk', DirInfo.Name) = 0) and //滤掉 *.lnk文件
(Pos('.ini', DirInfo.Name) = 0) then //滤掉 *.ini文件
begin
if (DirInfo.Attr and FaDirectory = FaDirectory) then
begin
tnNew := tvFile.Items.AddChild(tvFile.Selected, DirInfo.Name);
tnNew.Selected := True;
tnNew.ImageIndex := IMAGENUM_BOOKCLOSE; //设定分类目录节点的图标
tnNew.SelectedIndex := IMAGENUM_BOOKOPEN;
(tvURL.Items.AddChild(tvURL.Selected, DirInfo.Name)).Selected := True;
FF_GetDirFileName(strPath + '/' + DirInfo.Name, tvFile, tvURL);
end else
begin
buffer := DirInfo.Name;
Delete(buffer, Pos('.url', buffer), 4); //滤掉文件名中的 .url
//添加名称到 tvFile中
tnNew := tvFile.Items.AddChild(tvFile.Selected, buffer);
tnNew.ImageIndex := IMAGENUM_URLPAGE; //设定Web页节点的图标
tnNew.SelectedIndex := tnNew.ImageIndex;
//得到URL,并加入到tvURL树中
strIniFile := TIniFile.Create(strPath + '/' + DirInfo.Name);
buffer := strIniFile.ReadString('InternetShortcut','URL','');
strIniFile.Destroy;
tvURL.Items.AddChild(tvURL.Selected, buffer);
end;
end;
intRet := FindNext(DirInfo);
end;
//重置文件名树tvFile的焦点
if tvFile.Selected <> nil then
if tvFile.Selected.Parent <> nil then
(tvFile.Selected.Parent).Selected := True
else (tvFile.Selected).Selected := False;
//重置Web地址树tvURL的焦点
if tvURL.Selected <> nil then
if tvURL.Selected.Parent <> nil then
(tvURL.Selected.Parent).Selected := True
else (tvURL.Selected).Selected := False;
SysUtils.FindClose(DirInfo);
end;
var
strURLFileName: TFileName;
pWinDir
char; //windows目录
begin
GetMem(pWinDir, 256);
GetWindowsDirectory(pWinDir,128);
strURLFileName := pWinDir + '/Favorites';
FreeMem(pWinDir);
//取得收藏夹的内容
FF_GetDirFileName(strURLFileName, tv_FileName, tv_URLName);
end;