给分吧。
procedure Tform1.SearchFile(aPath:String);//搜索并添加菜单
var
aRec: TSearchRec;
NewItem: TMenuItem;
begin
try
if FindFirst(aPath, faDirectory,aRec) = 0 then
begin
if (aRec.Attr > 0) and (aRec.Name <>'.') and (aRec.Name <>'..') and (aRec.Attr <> faDirectory) then
begin
NewItem := TMenuItem.Create(Self);
NewItem.Caption := aRec.Name;
NewItem.OnClick := DynamicMenuClick;//动态添加菜单的Click事件
PopupMenu1.Items.Add(NewItem);
end;
end;
while FindNext(aRec) = 0 do
if (aRec.Attr > 0) and (aRec.Name <>'.') and (aRec.Name <>'..') and (aRec.Attr <> faDirectory)then
begin
NewItem := TMenuItem.Create(Self);
NewItem.Caption := aRec.Name;
NewItem.OnClick := DynamicMenuClick;
PopupMenu1.Items.Add(NewItem);
end;
finally
FindClose(aRec);
end;
Application.ProcessMessages;
end;
procedure Tform1.DynamicMenuClick(sender :TObject);;//动态添加菜单的Click事件
begin
showmessage((Sender as TMenuItem).Caption );
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
SearchFile('c:/*.*');
end;