{ first create the separator }
NewItem := TMenuItem.Create(Self);
NewItem.Caption := '-';
{ add the new item to the Windows menu }
Windows.Add(NewItem);
{ now create and add a menu item for each form }
for I := 0 to Screen.FormCount-1 do
begin
NewItem := TMenuItem.Create(Self);
NewItem.Caption := Screen.Forms[I].Name;
Windows.Add(NewItem); end;
NewItem := TMenuItem.Create(Self);
NewItem.Caption := '-';
{ add the new item to the Windows menu }
Windows.Add(NewItem);
{ now create and add a menu item for each form }
for I := 0 to Screen.FormCount-1 do
begin
NewItem := TMenuItem.Create(Self);
NewItem.Caption := Screen.Forms[I].Name;
Windows.Add(NewItem); end;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
PopupMenu1: TPopupMenu;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var MenuItem:TMenuItem;
begin
MenuItem:=TMenuItem.Create(nil);
MenuItem.Caption:='HElllo';
MenuItem.OnClick:=Button1Click;
PopupMenu1.Items.Add(MenuItem);
end;
end.
动态创建一个菜单项,并将它的onclick事件设为buttonclick
创建完后,插入popupmenu
TPopupMenu.Items.Add(TMenuItem)
这个方法,的参数也可以是个菜单项的数组,所以
你也可动态创建几个菜单项,组成个数组,插入!
你要加各种事件处理,过程还可以根据菜单项不同,而不同处理
type
TForm1 = class(TForm)
Button1: TButton;
PopupMenu1: TPopupMenu;
procedure Button1Click(Sender: TObject);
procedure Click(Sender:TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
var MenuItem:TMenuItem;
begin
MenuItem:=TMenuItem.Create(nil);
MenuItem.Caption:='HElllo';
MenuItem.OnClick:=Form1.Click;
PopupMenu1.Items.Add(MenuItem);
end;
procedure TForm1.Click(Sender: TObject);
begin
if (Sender as TMenuItem).Caption='你的条件' then
begin
handleMsg...
end;
end;
var F: TSearchRec
begin
if FindFirst(Path,faAnyFile,F)= 0 then
begin
repeat
if (F.Name<>'.') and(F.Name<>'..')
and (F.Attr and faDirectory)<>faDirectory then
begin
MenuItem:=TMenuItem.Create(nil);
MenuItem.Caption:=F.Name;
...
PopupMenu1.Items.Add(MenuItem);
end;
until FindNext(F) <> 0;
FindClose(F);
end