2007. november 15., csütörtök

Create a menu from a directory tree (Simple)


Problem/Question/Abstract:

I wanted to simulate the "favorites" of IE

Answer:

First of all, add this:

procedure CreateTreeMenus(Path: string; Root: TMenuItem);
var
  SR: TSearchRec;
  Result: Integer;
  Item: TMenuItem;
begin
  Path := IncludeTrailingBackSlash(Path);
  Result := FindFirst(Path + '*.*', faDirectory, SR);
  while (Result = 0) do
  begin
    if (((SR.Attr and faDirectory) <> 0) and (SR.Name <> '.') and (SR.Name <> '..'))
      then
    begin
      Item := TMenuItem.Create(Self);
      Item.Caption := SR.Name;
      Root.Add(Item);
      CreateTreeMenus(Path + SR.Name, Item);
    end;
    Result := FindNext(SR);
  end;
  SysUtils.FindClose(SR);
end;

Then call the function in this way:

CreateTreeMenus('C:\MyApp\MyFavs', mnuPreferiti);

Nincsenek megjegyzések:

Megjegyzés küldése