procedure TForm3.MenuItemClick(Sender: TObject); var aFileName: String; iIndex: integer; begin aFileName := Folder+GetMenuFileName(TMenuItem(Sender)); //如果是文件则单击打开 if TMenuItem(Sender).Tag = 0 then ShellExecute(0, 'Open', PChar(aFileName), nil, nil, SW_SHOWNORMAL) else begin if TMenuItem(Sender).Count = 1 then begin ShowPMFileIcon(PopupMenu1, aFileName, TMenuItem(Sender)); iIndex := TotalFileCount(aFileName); if iIndex <> -1 then TMenuItem(Sender).Delete(0); end; end; end;
//统计某个文件夹下的文件个数 function TotalFileCount(aPath: String): Integer; var sr: TSearchRec; i: integer; aTempPath: String; begin i:= -1; aTempPath := IncludeTrailingBackslash(aPath)+'*.*'; //修正文件夹名称 if FindFirst(aTempPath, faAnyFile, sr)=0 then begin while FindNext(sr) = 0 do if sr.name[1]<>'.' then inc(i); FindClose(sr); end; Result := i; end;
//得到当前菜单项的完整文件名 function GetMenuFileName(aChild: TMenuItem): String; begin Result := aChild.Caption+'\'+Result; if Assigned(aChild.Parent) then Result := GetMenuFileName(aChild.Parent) else Result := Copy(Result, 2, Max_Path); end;
//如果当创建的菜单有子菜单时则移动鼠标则会触法下面的事件,反之则单击菜单项才触法下面的事件。 procedure TForm1.MenuItemClick(Sender: TObject); var aFileName: String; iIndex: integer; begin aFileName := Folder+GetMenuFileName(TMenuItem(Sender)); //如果是文件则单击打开 if TMenuItem(Sender).Tag = 0 then ShellExecute(0, 'Open', PChar(aFileName), nil, nil, SW_SHOWNORMAL) else begin if TMenuItem(Sender).Count = 1 then begin ShowPMFileIcon(PopupMenu1, aFileName, TMenuItem(Sender)); iIndex := TotalFileCount(aFileName); if iIndex <> -1 then TMenuItem(Sender).Delete(0); end; end; end;
//枚举某个文件夹下的所有文件及了文件夹到菜单上 procedure TForm1.ShowPMFileIcon(Pm: TPopupMenu; aPath: String; aParent: TMenuItem=nil; aFirst: Boolean = True); var sr: TSearchRec; i: integer; aMenuItem: TMenuItem; mnuEmpty: TmenuItem; aTempPath: String; {* 得到文件图标索引} function MenuImageIndex: integer; begin Result := GetFileIconIndex(aTempPath+sr.name); end; {* 添加空菜单项} procedure EmptyMenuItem; begin if (sr.attr and faDirectory)= faDirectory then begin aMenuItem.Tag := 1; //标记该菜单为文件夹 mnuEmpty := TMenuItem.Create(Pm); mnuEmpty.Caption := '(空)'; mnuEmpty.Enabled := False; aMenuItem.Add(mnuEmpty); end; end; begin aTempPath := IncludeTrailingBackslash(aPath); i := FindFirst(aTempPath+'*.*', faAnyFile, sr); while i=0 do begin if sr.Name[1] <> '.' then //如果文件名不为"."或".." begin aMenuItem := TMenuItem.Create(aParent); aMenuItem.Hint := aTempPath; aMenuItem.ImageIndex := MenuImageIndex; aMenuItem.Caption := sr.Name; aMenuItem.OnClick := MenuItemClick; if aParent = nil then Pm.Items.Add(aMenuItem) else aParent.Add(aMenuItem); {* 添加空菜单项} EmptyMenuItem; end; {* 查找下一个文件} i := FindNext(sr); end; FindClose(sr); end;
procedure TForm1.Button1Click(Sender: TObject); begin Folder := IncludeTrailingBackslash(Edit1.Text); ShowPopupMenu(Sender, PopupMenu1); end;
procedure TForm1.PopupMenu1Popup(Sender: TObject); begin if First then begin GetSysImageList(ImageList1); PopupMenu1.Items.Clear; ShowPMFileIcon(PopupMenu1, Folder); First := False; end; end;
procedure TForm1.Edit1Change(Sender: TObject); begin First := True; end;