procedure TMainForm.FavItemClick(Sender: TObject);
var
i:Integer;
WorkDir:String;
begin
for i:=TMenuItem(Sender).Count-1 downto 3 do
TMenuItem(Sender).Delete(i);
WorkDir:=GetFavDir;
CreateFavMenu(WorkDir,FavItem);
end;
function TMainForm.GetFavDir: String;
//获得收藏夹的目录
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('Software/Microsoft/Windows/CurrentVersion/Explorer/Shell Folders', False) then
Result :=Reg.ReadString('Favorites');
finally
Reg.CloseKey;
Reg.Free;
End;
end;
procedure TMainForm.CreateFavMenu(Const WorkDir:String;Var MenuItem:TMenuItem);
var
SearchRec:TSearchRec;
FWorkDir:String;
SubMenuItem:TMenuItem;
begin
FWorkDir :=WorkDir;
if (FindFirst(FWorkDir +'/*.*', faDirectory,SearchRec)=0) then
begin
repeat
if (SearchRec.Attr and faDirectory = faDirectory) then
Begin
if (SearchRec.Name <>'.')and (SearchRec.Name <>'..') then
begin
SubMenuItem:=TMenuItem.Create(Application);
SubMenuItem.Caption :=SearchRec.Name;
SubMenuItem.ImageIndex:=21;
MenuItem.Add(SubMenuItem);
CreateFavMenu(FWorkDir+'/'+SearchRec.Name,SubMenuItem);
end;
end;
until FindNext(SearchRec) <> 0;
end;
FindClose(SearchRec);
if (FindFirst(FWorkDir +'/*.*', faDirectory,SearchRec)=0) then
begin
repeat
if (SearchRec.Attr and faDirectory <> faDirectory) then
begin
SubMenuItem:=TMenuItem.Create(MenuItem);
SubMenuItem.Caption :=Copy(SearchRec.Name,1,Length(SearchRec.Name)-4);
SubMenuItem.ImageIndex:=22;
if Length(SubMenuItem.Caption) >25 then
SubMenuItem.Caption :=Copy(SubMenuItem.Caption,1,25)+'...';
SubMenuItem.Hint :=GetUrlFileURL(FWorkDir,SearchRec.Name);
SubMenuItem.OnClick :=MenuItem_FaClick;
MenuItem.Add(SubMenuItem);
end;
until FindNext(SearchRec) <> 0;
End;
FindClose(SearchRec);
end;
这是我以前写的代码,你看一下。