请问各位大侠,可否实现类似IE收藏夹那样根据一个文件夹内的文件和子文件夹来动态的添加菜单项》? ? 就是可以支持根据子文件夹出子菜单,根据文件出普通菜单项

解决方案 »

  1.   

    首先用这个函数,把指定的文件夹的内容列出来Function SearchFileS(SPath,Filter:String;InChildDir : Boolean ; var StrLS : TStringList ) : Boolean;
    var
      i: Integer;
      SearchRec: TSearchRec;
      FileName : String;
    begin
      Result := True;
      if copy(Spath,Length(Spath),1)<>'\' then SPath := SPath+'\';
      try
        i:=FindFirst(SPath+Filter,faAnyFile,SearchRec);
      except
        Result := False;
        exit;
      end;
      while i=0 do begin
        FileName := SearchRec.name;
        if Fileexists(SPath+FileName) then StrLS.Add(SPath+FileName)
        else if (FileName<>'.') and (FileName<>'..') then
           begin
             if InChildDir then  SearchFileS(SPath+FileName,Filter,InChildDir,StrLS)
             else StrLS.Add(SPath+FileName); 
           end
        else if FileName='..' then StrLS.Add(SPath);
        i := FindNext(SearchRec);
      end;
      try
        i:=FindFirst(SPath+'*.*',faDirectory,SearchRec);
      except
        Result := False;
        exit;
      end;
      while i=0 do begin
        FileName := SearchRec.name;
        if Fileexists(SPath+FileName) then 
        else if (FileName<>'.') and (FileName<>'..') then
           begin
             if InChildDir then  SearchFileS(SPath+FileName,Filter,InChildDir,StrLS)
             else StrLS.Add(SPath+FileName); 
           end;
    //    else if FileName='..' then StrLS.Add(SPath);
        i := FindNext(SearchRec);
      end;
      
    end;然后利用菜单的动态创建功能实现
    FileList : TStringList;
    MenuItem : TMenuItem;SearchFileAt('c:\','*.*',False,FileList);for i:=0 to FileList.Count-1 do begin
      MenuItem := TMenuItem.Create(Self);
      MenuItem.Caption := FileList.String[i];
      MainForm.Items.Add(Menu);
    end;
    时间关系,只写个大概,请仿照修改就行了。
      

  2.   

    楼上很清楚了,先查找文件,
    然后MenuItem := TMenuItem.Create(Self);
    动态增加菜单就OK了。
      

  3.   

    谢谢楼上大虾,可是有两个问题:
    1。上面代码可以添加菜单项目,但是怎么样实现点击菜单项后读取这个菜单项对应的文件内容(TXT文件)2。上面代码只可以添加文件,子文件夹无法添加,同时也无法在菜单项目中显示子级菜单还望各位高手赐教!
      

  4.   

    procedure TfrmMyIE.GetFavoriesFolder(var ParentMenu:TMenuItem ;
            Folder:string;MaxCount:integer);
    var
      i,l1,startindex:integer;
      ffd:WIN32_FIND_DATA;
      Buffer:array [0..MAX_PATH] of Char;
      Item,FirstURL,MenuItem:TMenuItem;
      FileName,Dest:String;
      hFind,Len:THANDLE;  tiaostr1,localext:string;
    begin
    startindex:=0;
    l1:=length(Folder);
    if(Folder[l1] <> '\')  then   Folder := Folder + '\';
    Dest:= Folder + '*.*';
    hFind:=FindFirstFile(Pchar(Dest), ffd);
        if(hFind>0) then
        begin//1
            repeat
                if(ffd.cFileName[0] <> '.') then
                begin//3
                    // 检查是否超出了最大的子项数目
                    FileName := Folder + ffd.cFileName;
                    // 读取文件夹
                    if(ffd.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY) then
                    begin//5
                        Item :=TMenuItem.create(ParentMenu);
                        Item.Caption := ffd.cFileName;
                        Item.ImageIndex := 0;
                        ParentMenu.Add(Item);
                        GetFavoriesFolder(Item,FileName,MaxCount);
                        end//5
                        else
                        begin//10
                       Len:=length(FileName);
                       localext:=Copy(FileName,Len + 1 - 4, 4);
                       if (localext='.url') or(localext='.URL') then
                        begin//11
                            GetPrivateProfileString('InternetShortcut','URL',
                                    nil,Buffer,MAX_PATH,Pchar(FileName));
                            Item :=TMenuItem.Create(ParentMenu);
                            localext:=ffd.cFileName;
                            if Length(localext) > 12 then
                              localext := Copy(localext, 1, 12) + '..';                        Item.Caption := localext;
                            Item.Hint := Buffer;
                            Item.ImageIndex :=1;
                            Item.OnClick := hint1Click;
                            ParentMenu.Add(Item);
                            end;//11
                         end;//10
                        sb1.SimpleText :=FileName;
                    end;//3
                    // 读取快捷方式
            until not (FindNextFile(hFind,ffd));
            Windows.FindClose(hFind);
        end;//1
        // 如果没有子条目,添加空白条目
       end;
    end;procedure Tform1.hint1Click(sender:tobject);
    begin
     shellexecute(...Pchar((sender as tmenuitem).hint),....);
    end;
      

  5.   

    修正:   if(ffd.dwFileAttributes=FILE_ATTRIBUTE_DIRECTORY) then改乘   if(ffd.dwFileAttributes  and FILE_ATTRIBUTE_DIRECTORY)>0 then