procedure TfrmMain.NewMenuItem;
var
SQLText:string;
rst:_Recordset;
tmpMenuName:string;
tmpMenuItem:TMenuItem;
i:integer;
begin
Popupmenu3.Items.Clear;
for i:=0 to 5 do
begin
tmpMenuItem:=TMenuItem.Create(nil);
tmpMenuName:='MenuItem'+inttostr(i);
tmpMenuItem.Caption :=tmpMenuName;
tmpMenuItem.OnClick :=ViewClick;
Popupmenu3.Items.Add(tmpMenuItem);
end;
end;
procedure TfrmMain.ViewClick(Sender: TObject);
var
tmp:string;
begin
if SystemInited=False then Exit;
tmp:=(Sender as TMenuItem).Caption;
showmessage(tmp);
end;
{点击事件,触发打开子窗体}
procedure TfrmMainWindow.MainMenuClick(Sender: TObject);
var
Item:TMenuItem;
begin
Item:=Sender as TMenuItem;
OpenForm(Item);
end;
procedure TfrmMainWindow.CreateMenu;
var
Item:array[0..100] of TMenuItem;
i:Integer;
begin
for i:=MainMenu.Items.Count-3 downto 1 do
MainMenu.Items.Destroy;
for i:=0 to 99 do
Item:=TMenuItem.Create(Self);
if ModuleList<>nil then ModuleList.Destroy;//
ModuleList:=TStringList.Create;
ClientDataSet1.Close;
ClientDataSet1.CommandText:='Select Round(Sort/10000,0) as Sort1,'+
'Round(Sort/100,0)-Round(Sort/10000,0)*100 as Sort2,'+
'Sort-Round(Sort/100,0)*100 as Sort3,'+
'ModuleName,ModuleCaption,Sign From '+
'ModuleList order by Sort';
ClientDataSet1.Open;
for i:=0 to ClientDataSet1.RecordCount-1 do
begin
Item.Name:=ClientDataSet1.Fields[3].AsString;
Item.Caption:=ClientDataSet1.Fields[4].AsString;
if ClientDataSet1.Fields[5].AsInteger=1 then
begin
Item.OnClick:=MainMenuClick;
{获取模块列表}
ModuleList.Add(Trim('frm'+ClientDataSet1.Fields[3].AsString)+
'='+Trim(ClientDataSet1.Fields[4].AsString));
end;//end of if
if (ClientDataSet1.Fields[1].AsInteger=0) and
(ClientDataSet1.Fields[2].AsInteger=0) then
MainMenu.Items.Insert(MainMenu.Items.Count-2,Item)
else if ClientDataSet1.Fields[2].AsInteger=0 then
MainMenu.Items[MainMenu.Items.Count-3].Add(Item)
else MainMenu.Items[MainMenu.Items.Count-3].Items[MainMenu.Items[MainMenu.Items.Count-3].Count-1].Add(Item);
ClientDataSet1.Next;
end;//end of While not
end;
这是我编写的,里面当然不只是简单的创建菜单了。
不过你的功能就包含在里面了。自己研究研究,应该可以明白的。