//TMainMenu带历史信息的菜单带历史信息的菜单(动态插入Insert)
//动态插入菜单项,首先要求创建TMenuItem菜单对象
所需组件
TMainMenu
TOpenDialog
//为TMainMenu创建
File
Open(name:Open1)
------------
Exit(name:Exit1)
//main code here;
uses ShellApi;
public
{ Public declarations }
procedure RunIt(Sender: TObject);
//实现添加历史菜单
procedure TForm1.RunIt(Sender: TObject);
begin
if Sender is TMenuItem then
ShowMessage(TMenuItem(Sender).Caption); //使用shellExecute执行选中的菜单
end;
procedure TForm1.open2Click(Sender: TObject);
var
Temp: TMenuItem;
event: TNotifyEvent;
begin
if not OpenDialog1.Execute then Exit;
Temp := TMenuItem.Create(Self);
Temp.Caption := OpenDialog1.FileName;
event := RunIt;
Temp.OnClick := event;
MainMenu1.Items.Items[0].Insert(2, Temp);
if MainMenu1.Items.Items[0].Count = 4 then
begin
Temp := TMenuItem.Create(Self);
Temp.Caption := '-';
MainMenu1.Items.Items[0].Insert(3,Temp);
end;
end;
//功能
//当用户单击历史菜单时运行选中的菜单项
//完成 -> 动态生成菜单项内容