Mega Code Archive

 
Categories / Delphi / Files
 

How to create a menu from a directory tree (easy to use)

Title: How to create a menu from a directory tree (easy to use). Question: I wanted to simulate the "favorites" of IE, and that's the result: 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 ); That's all folks!