Delphi让菜单具有显示打开历史的功能 记住以前打开的记录
- 时间:2020-04-18
- 概述:历史记录 记忆菜单
Delphi让菜单具有显示打开历史的功能 记住最近打开的文档记录,这个功能对大家来说,已经不算陌生了,在大家常用的Word文档中,就有此功能,当然本代码只是一个简单的实现,用RecentList来更新最近文件列表菜单,从注册表中读取最近文件列表信息,每次打开一个文件后,向注册表中写入最近文件列表信息,下面来看代码吧:
unit UntOpenMenu; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Menus,registry; type TFrmMain = class(TForm) MainMenu1: TMainMenu; F1: TMenuItem; open: TMenuItem; RecentMenu: TMenuItem; OpenDialog1: TOpenDialog; filename0: TMenuItem; filename1: TMenuItem; filename2: TMenuItem; filename3: TMenuItem; filename4: TMenuItem; exit1: TMenuItem; procedure FormCreate(Sender:TOBject); procedure FormDestroy(sender:TObject); procedure openClick(Sender: TObject); procedure filename0Click(Sender: TObject); procedure exit1Click(Sender: TObject); private procedure UpdateRCList(const FileName: String); procedure UpdateRCMenu; procedure OpenFile(FileName:String); procedure WriteRegistry; procedure ReadRegistry; public { Public declarations } private recentList:TStrings; end; var FrmMain: TFrmMain; implementation {$R *.DFM} {完成部份} //更新临时存储最近文件列表的RecentList procedure TFrmMain.UpdateRCList(const FileName: String); var Position:integer; begin Position:=RecentList.IndexOf(FileName); {TStrings的IndexOf(s:string)方法返回s在TStrings对象中第一次出现的位置} if Position>=0 then RecentList.Delete(Position); //如果该文件名已在列表中存在,则将它从原来的位置移到最上面 RecentList.Insert(0,FileName); if RecentList.Count>5 then RecentList.Delete(5); end; //用RecentList来更新最近文件列表菜单 procedure TFrmMain.UpdateRCMenu; Var I:integer; begin For I:=0 to RecentList.Count - 1 do begin RecentMenu.items[I].Visible:=true; RecentMenu.items[I].Caption:=Format('&%d ',[I])+RecentList[i]; //使最近文件列表菜单项具有加速键 end; end; //具体打开文件操作, procedure TFrmMain.OpenFile(FileName:String); begin UpdateRCList(filename); UpdateRCMenu; showmessage(filename); end; {应用程序启动时读注册表} procedure TFrmMain.FormCreate(Sender:TOBject); begin ReadRegistry; end; //从注册表中读取最近文件列表信息 procedure TFrmMain.ReadRegistry; var I:integer; MyReg:TRegistry; begin RecentList:=TStringList.Create; MyReg:=TRegistry.create; try MyReg.RootKey := HKEY_CURRENT_USER; if MyReg.OpenKey('\Software\可视化油藏动态分析\最近文件列表',false) then begin RecentMenu.Visible:=true; MyReg.GetValueNames(RecentList); for I:=0 to RecentList.Count - 1 do RecentList[i]:=MyReg.ReadString(RecentList[i]); UpdateRCMenu; end; finally MyReg.CloseKey; MyReg.Free; inherited; end; end; //向注册表中写入最近文件列表信息 procedure TFrmMain.WriteRegistry; Var I:Integer; MyReg:TRegistry; begin MyReg:=TRegistry.create; try MyReg.RootKey := HKEY_CURRENT_USER; if MyReg.OpenKey('\Software\可视化油藏动态分析\最近文件列表',true) then begin For I:=0 to RecentList.Count - 1 do MyReg.WriteString('File'+Inttostr(I),RecentList[i]); end; finally MyReg.CloseKey; MyReg.Free; inherited; end; end; //应用程序关闭时,写注册表 procedure TFrmMain.formdestroy(sender:TObject); begin WriteRegistry; recentlist.free; end; //从打开文件对话框打开文件 procedure TFrmMain.openClick(Sender: TObject); begin if OpenDialog1.Execute then begin OpenFile(OpenDialog1.FileName); RecentMenu.Visible:=true; end; end; //从最近文件列表菜单项打开文件 procedure TFrmMain.filename0Click(Sender: TObject); var i:integer; begin i:=(Sender as TMenuItem).Tag; OpenFile(RecentList[i]); //调用打开文件过程 end; procedure TFrmMain.exit1Click(Sender: TObject); begin close; end; end.
相关声明:
- 若“Delphi让菜单具有显示打开历史的功能 记住以前打开的记录”有损您的权益,请告之我们删除内容。
部分文章来源于网络,版权归原作者所有。