关于MDI

shilijuan 2000-06-22 05:02:00
对于多文档应用程序,通常在文件菜单项下列出最近打开的几个文件名。请问用Delphi怎样实现?
...全文
139 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
yeah 2000-07-14
  • 打赏
  • 举报
回复
private
recentList:TStrings;
INI:Tinifile;

{完成部份}
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;

终于写完了,用了我不少电话费,不知值不值50分*_*
SuperMMX 2000-07-13
  • 打赏
  • 举报
回复
ini 比较方便, registry 也行.
菜单动态创建.
Wingsun 2000-06-22
  • 打赏
  • 举报
回复
将曾经打开过的文件记录在注册表或某一个配置文件中,在程序启动时调入,动态创建菜单即可。
费墨von 2000-06-22
  • 打赏
  • 举报
回复
可以考虑把近把开过的文件名连路径方到一个INI文件中并加上编号,动态生成文件菜单项下列出最近打开的几个文件名菜单名用事先定好的编号。ONclick事件可以根据不同的编号号码来找要打开的文件。虽然有点麻烦但我试过是可行的。

5,379

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧