1、添加弹出菜单命名PopupMenu1
2、添加菜单项命名TestMI
3、添加按钮
和代码:
procedure TForm1.Button1Click(Sender: TObject);
var
MItems: array of TMenuItem;
SList: TStringList;
FileRec: TSearchrec;
i: integer;
begin
SList := TStringList.Create;
//3000+ files
if FindFirst('C:\Windows\System32\*', faNormal or faDirectory, FileRec) = 0
then
repeat
if (FileRec.Name = '.') or (FileRec.Name = '..') then
Continue;
SList.Add(FileRec.Name);
until FindNext(FileRec) <> 0;
FindClose(FileRec);
if SList.Count > 0 then
begin
SetLength(MItems, SList.Count);
for i := 0 to SList.Count - 1 do
begin
MItems[i] := TMenuItem.Create(TestMI);
MItems[i].Caption := SList[i];
end;
TestMI.Add(MItems);
end;
end;
当我点击按钮时,它是好的,但是当我弹出 PopupMenu1
并继续时 TestMI
,因为文件太多它没有响应。
有什么办法可以解决吗?
更新:
我必须使用 PopupMenu 来执行此操作。
我找了个程序,能快点,150ms
https://docs.google.com/open?id=0B1sDNMAzGE2oZWpTWlpWNHJGZzQ
它使用BarMenu 组件
但我无法在 Delphi 2009 中编译。错误:
库\栏菜单:
{$IFDEF MSWINDOWS}
{$IFNDEF DFS_COMPILER_5_UP}
{$MESSAGE FATAL 'You need Delphi 5 or higher in order to compile this unit.'}
{$ENDIF}
Windows, SysUtils, Classes, Graphics, Menus, Forms;
{$ENDIF}
更新到@Sertac Akyuz
在第一种情况下,您的解决方案很有用。非常感谢。我改变了那个案例代码:
unit Unit1;
interface
uses
Windows,
Messages,
SysUtils,
Variants,
Classes,
Graphics,
Controls,
Forms,
Dialogs,
StdCtrls,
Menus;
type
TForm1 = class(TForm)
Button1: TButton;
PopupMenu1: TPopupMenu;
TestMI: TMenuItem;
procedure Button1Click(Sender: TObject);
procedure CreMI(MI: TMenuItem);
procedure IMonClick(Sender: TObject);
procedure AddSubEmpItem(MI: TMenuItem);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.IMonClick(Sender: TObject);
begin
CreMI(TMenuItem(Sender));
end;
procedure TForm1.AddSubEmpItem(MI: TMenuItem);
var
EmpIM: TMenuItem;
begin
EmpIM := TMenuItem.Create(MI);
with EmpIM do
begin
Caption := '(Folder empty)';
Enabled := False;
Hint := '';
MI.Add(EmpIM);
end;
end;
procedure TForm1.CreMI(MI: TMenuItem);
var
MItems: array of TMenuItem;
SList: TStringList;
FileRec: TSearchrec;
i: integer;
begin
if (MI.Items[0].Caption = '(Folder empty)') and (MI.Count = 1) then
begin
SList := TStringList.Create;
if FindFirst(MI.Hint + '\*', faNormal or faDirectory, FileRec) = 0
then
repeat
if (FileRec.Name = '.') or (FileRec.Name = '..') then
Continue;
SList.Add(FileRec.Name);
until FindNext(FileRec) <> 0;
FindClose(FileRec);
if SList.Count > 0 then
begin
SetLength(MItems, SList.Count);
for i := 0 to SList.Count - 1 do
begin
MItems[i] := TMenuItem.Create(MI);
MItems[i].Caption := SList[i];
MItems[i].Hint := MI.Hint + SList[i] + PathDelim;
AddSubEmpItem(MItems[i]);
MItems[i].OnClick := IMonClick;
MItems[i].AutoHotkeys := maManual;
end;
MI.Add(MItems);
MI.AutoHotkeys := maManual;
end;
end;
//Button1.Caption := IntToStr(MI.Count);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
AddSubEmpItem(TestMI);
CreMI(TestMI);
end;
end.
放TestMI.Hint := C:\
单击按钮,当我继续前进时C:\ -> Windows -> System32
也没有响应。你能给点建议吗?