{完成部份}
procedure TForm1.AddRecentMenu(const FileName: String);
begin
if RecentList.IndexOf(FileName)<0 then
begin
RecentList.Insert(0,FileName);
if RecentList.Count>9 then
RecentList.Delete(9);
ReIndexRCMenu; //重建recentMenu,这样保证刚打开的文件排在第一位
mnSeparator.Visible:=True;
end;
end;
procedure TForm1.ReIndexrcMenu;
Var I:integer;
mn:TMenuItem;
begin
{清除原菜单 F1为“文件”菜单,mnSeparator为分隔条,mnTop为RecentMenu的
上一个菜单条 假设将RecentMenu放到菜单最下面}
For I:=F1.Count downto mnSeparator.MenuIndex+1 do
F1.Delete(I);
For I:=0 to RecentList.Count - 1 do
begin
Mn:=TMenuItem.Create(Self);
With MN do
begin
Caption:=Format('&%d ',[I+1])+ExtractfileName(RecentList[i]);
Tag:=I;
Hint:=Format('打开“%S”。',[RecentList[i]]);
OnClick:=RecentMenuClick;
F1.Insert(mnTop.MenuIndex,MN);
end;
end;
end;
procedure TForm1.RecentMenuClick(Sender: TObject);
begin
With Sender as TMenuItem do
OpenFile(RecentList[Tag]); //调用打开文件过程
end;
{启动时将文件列表读入到StringList中}
procedure TForm1.FormCreate(Sender:TOBject);
begin
RecentList:=TStringList.Create;
INI:=Tinifile.Create(ExtractfilePath(Application.EXEName)+'Myini.ini');
With INI do
begin
ReadSection('Recent Files',RecentList);
if RecentList.Count>0 then
begin
mnSeparator.Visible:=True;
if RecentList.Count>9 then //限制不超过9个菜单条
For I:=RecentList.Count - 1 downto 9 do
RecentList.Delete(I);
for I:=0 to RecentList.Count - 1 do
RecentList[i]:=ReadString('Recent Files',RecentList[i],'');
ReIndexRcMenu;
end;
end;
end;
{File->New}
procedure TForm1.NewFile(Sender:TObject);
begin
With OpenDialog1 do
begin
if Not Execute then exit;
OpenFile(FileName);
Addrecentmenu(FileName);
end;
end;
{保存RecentMenu}
procedure TForm1.SaveRecentItem;
Var I:Integer;
begin
With INI do
begin
if RecentList.Count<>0 then
begin
EraseSection('Recent Files');
For I:=0 to RecentList.Count - 1 do
WriteString('Recent Files','File'+Inttostr(I),RecentList[i]);
end;
Updatefile;
end;
end;
procedure Tform1.formclose(Sender:TObject);
begin
saverecentItem;
end;
procedure Tform1.formdestroy(sender:TObject);
begin
ini.free;
recentlist.free;
end;