带历史信息的菜单
实例说明
在有些软件中,菜单栏中可以记录已经打开过的文件信息,使用户操作简单、快捷。当用户要打开已打开过的文件时,不需要重复查找,只需选择菜单中打开过的文件,即可实现打开该文件的操作。运行本例,效果如图1所示。
图1 带历史信息的菜单
思路与技术
本例使用的是动态插入菜单的技术,利用菜单的动态插入技术,可以完成在菜单中记录打开过文档的功能,即当打开一个文件后,将这个文件的完整路径插入到菜单的相应位置。主要用到了TMainMenu组件的Insert()方法。在菜单的OnChange事件中,为菜单项指定相应的事件。动态插入菜单项的代码如下:
//------------------------------------------------------------------------------
procedure
TForm1.mni_OpenClick(Sender: TObject);
var
menuItem:TMenuItem;
begin
menuItem:=TMenuItem.Create(nil);
With
TOpenDialog.Create(nil) do
begin
if Execute then
begin
menuItem.Caption:=FileName;
MainMenu1.Items.Items[0].Items[2].Insert(0,menuItem);
end;
end;
end;
0
插入配置打开文件的路径菜单
1 单击这个菜单 返回这个菜单的路径
2
增加1个配置信息保存
3
读取配置信息
开发步骤
(1)新建一个工程,该工程会自动创建一个主窗体,默认命名为Form1。
(2)在窗体上添加TMainMenu组件和TOpenDialog组件。
(3)程序完整代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
mni_File: TMenuItem;
mni_Open: TMenuItem;
N3: TMenuItem;
mni_Close: TMenuItem;
N5: TMenuItem;
Button1: TButton;
mni_Recent: TMenuItem;
procedure mni_OpenClick(Sender: TObject);
procedure mni_CloseClick(Sender: TObject);
procedure MainMenu1Change(Sender: TObject; Source: TMenuItem;
Rebuild: Boolean);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure MenuItemExecute(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//------------------------------------------------------------------------------
procedure TForm1.mni_OpenClick(Sender: TObject);
var
menuItem:TMenuItem;
begin
menuItem:=TMenuItem.Create(nil);
With TOpenDialog.Create(nil) do
begin
if Execute then
begin
menuItem.Caption:=FileName;
MainMenu1.Items.Items[0].Items[2].Insert(0,menuItem);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TForm1.mni_CloseClick(Sender: TObject);
begin
Close;
end;
//------------------------------------------------------------------------------
procedure TForm1.MainMenu1Change(Sender: TObject; Source: TMenuItem;
Rebuild: Boolean);
var
i:integer;
begin
for i:=2 to mainmenu1.Items.Items[0].Count-3 do
begin
MainMenu1.Items.Items[0].Items[i].OnClick:=button1.OnClick;
end;
end;
//------------------------------------------------------------------------------
//返回当前单击的标题
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, StdCtrls;
type
TForm1 = class(TForm)
MainMenu1: TMainMenu;
mni_File: TMenuItem;
mni_Open: TMenuItem;
N3: TMenuItem;
mni_Close: TMenuItem;
N5: TMenuItem;
Button1: TButton;
mni_Recent: TMenuItem;
procedure mni_OpenClick(Sender: TObject);
procedure mni_CloseClick(Sender: TObject);
procedure MainMenu1Change(Sender: TObject; Source: TMenuItem;
Rebuild: Boolean);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure MenuItemExecute(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//------------------------------------------------------------------------------
procedure TForm1.mni_OpenClick(Sender: TObject);
var
menuItem:TMenuItem;
begin
menuItem:=TMenuItem.Create(nil);
With TOpenDialog.Create(nil) do
begin
if Execute then
begin
menuItem.Caption:=FileName;
MainMenu1.Items.Items[0].Items[2].Insert(0,menuItem);
end;
end;
end;
//------------------------------------------------------------------------------
procedure TForm1.mni_CloseClick(Sender: TObject);
begin
Close;
end;
//------------------------------------------------------------------------------
procedure TForm1.MainMenu1Change(Sender: TObject; Source: TMenuItem;
Rebuild: Boolean);
var
i:integer;
begin
for i:=2 to mainmenu1.Items.Items[0].Count-3 do
begin
MainMenu1.Items.Items[0].Items[i].OnClick:=button1.OnClick;
end;
end;
//------------------------------------------------------------------------------
//返回当前单击的标题
procedure TForm1.MenuItemExecute(Sender: TObject);
begin
ShowMessage(TMenuItem(Sender).Caption);
end;
//------------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
MenuItemExecute(Sender);
end;
//------------------------------------------------------------------------------
end.
begin
ShowMessage(TMenuItem(Sender).Caption);
end;
//------------------------------------------------------------------------------
procedure TForm1.Button1Click(Sender: TObject);
begin
MenuItemExecute(Sender);
end;
//------------------------------------------------------------------------------
end.