更新经过一些试验,似乎将设置保存到DOF实际上比将其保存到DSK文件更可靠。
将另一个TEdit添加到窗体并创建LoadDOFSettings和SaveDOFSettings
类似于现有的LoadSettings和SaveSettings,并在收到时调用它们
DesktopLoad和DesktoSave通知。无需通过Timer1事件调用SaveDOFSettings,因为DOF似乎没有发生重命名。
原始答案
我建议在阅读此答案之前,先做一个文件|在IDE中关闭全部,
创建一个新程序包,将下面的单元添加到其中,然后将其安装在IDE中。
该软件包的目的是双重的,首先是说明如何在DSK文件中保存自定义设置,其次是让您了解有关项目的哪些事件信息
您可以通过ToolsAPI单元中的服务从IDE中获取文件。
安装软件包后,请密切注意其表单,该表单会在您打开,处理和关闭项目时在上方的备忘录中显示文件通知。有
需要注意的几件事:
-
打开项目时,收到的最后一条通知是有关其DSK文件已打开的消息。
-
并非每种文件类型都是通知的主题。特别是,您不会收到任何专门针对DOF文件的通知,因此,如果您要写入该文件并稍后再读取该文件,则必须做出有关何时安全(或不安全)的假设,这就是可能是为什么您遇到了您要问的问题。
-
在项目上执行“全部关闭”时,收到通知的最后文件更改是正在写入的DSK。要注意的是,它最初是写入到具有相同名称但扩展名为。$$$的文件中。此后不久,但您无法确切地说,这个。$$$文件被重命名为.DSK。
由下面的代码创建的表单具有一个编辑框,DSK文件的edMyValue' which can be used to set a value in a section of the DSK file called
MySettings and which is reloaded the next time the project is opened. The writing of the
MySettings`部分由TTimer触发,延迟2秒以使IDE时间按照我的描述编写并重命名DSK文件。显然,这为比赛条件提供了机会。
您可能想参考
http://www.gexperts.org/open-tools-api-faq/#dsk
(GExperts是IDE加载项工具,自Delphi成立以来就一直存在)
本文的部分讨论当前项目的.DSK文件。像DOF一样,它是INI文件格式,具有类似
的部分
[Closed Files]
[Modules]
[EditWindow0]
[View0]
您会看到它说
检查ofnProjectDesktopLoad和ofnProjectDesktopSave NotifyCode值。当您看到其中之一时,可以使用TIniFile等类从FileName参数指示的文件中保存/加载值。
由于重命名业务,也许比文章建议的要难一些。
玩得开心!
unit IDEEventsu;
interface
uses
Windows,Messages,SysUtils,Variants,Classes,Graphics,Controls,Forms,Dialogs,StdCtrls,ExtCtrls,ToolsAPI,Grids,IniFiles;
type
TFileEventsForm = class(TForm)
Panel1: TPanel;
Memo1: TMemo;
edMyValue: TEdit;
btnClear: TButton;
Timer1: TTimer;
Memo2: TMemo;
procedure btnClearClick(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
function GetCurrentProject: IOTAProject;
public
// The following are using interfaces accessible via the ToolsAPI
Services: IOTAServices;
ProjectGroup : IOTAProjectGroup;
Project: IOTAProject;
Options : IOTAProjectOptions;
ModServices: IOTAModuleServices;
Module: IOTAModule;
NotifierIndex: Integer; // This is used to disconnect our notifier from the IDE
IsSetUp : Boolean;
SetUpCount : Integer;
DskFileName : String;
procedure SetUp;
procedure SaveSettings;
procedure LoadSettings;
end;
var
FileEventsForm: TFileEventsForm;
procedure Register;
[...]
uses
typinfo;
type
TIdeNotifier = class(TNotifierObject,IOTANotifier,IOTAIDENotifier)
// This is the class we use to receive file notication events from the IDE via the
// interfaces in ToolsAPI.Pas
//
// It needs to implement the IOTANotifier and IOTAIDENotifier interfaces and,// once registered with the IDE,the IDE calls its methods as a kind of call-back
// mechanism so that it gets notified of file events
//
// Note that this file also provides a form for displaying the received event
// notifications and that the IOTANotifier and IOTAIDENotifier interfaces could
// just as easily be implemented by the form itself
protected
procedure AfterCompile(Succeeded: Boolean);
procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
procedure FileNotification(NotifyCode: TOTAFileNotification;
const FileName: string; var Cancel: Boolean);
end;
procedure Register;
// This is necessary to register the package in the IDE
var
Notifier : TIdeNotifier;
begin
FileEventsForm:= TFileEventsForm.Create(Nil);
FileEventsForm.Services := BorlandIDEServices as IOTAServices;
Notifier := TIdeNotifier.Create;
Notifier.Form := FileEventsForm;
FileEventsForm.NotifierIndex := FileEventsForm.Services.AddNotifier(TIdeNotifier.Create);
end;
procedure CloseDown;
begin
FileEventsForm.Services.RemoveNotifier(FileEventsForm.NotifierIndex);
FileEventsForm.Close;
FileEventsForm.Free;
end;
function NotifyCodeString(NotifyCode : TOTAFileNotification) : String;
begin
Result := Copy(GetEnumName(TypeInfo(TOTAFileNotification),Ord(NotifyCode)),4,MaxInt);
end;
procedure TIdeNotifier.AfterCompile(Succeeded: Boolean);
begin
end;
procedure TIdeNotifier.BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
begin
end;
procedure TIdeNotifier.FileNotification(NotifyCode: TOTAFileNotification;
const FileName: string; var Cancel: Boolean);
begin
if True {NotifyCode in [ofnProjectDesktopLoad,ofnActiveProjectChanged]} then begin
FileEventsForm.Show;
FileEventsForm.Memo1.Lines.Add(Format('%s file: %s',[NotifyCodeString(NotifyCode),FileName]));
case NotifyCode of
ofnProjectDesktopLoad,ofnDefaultDesktopLoad : begin
FileEventsForm.DskFileName := FileName;
FileEventsForm.LoadSettings;
end;
ofnProjectDesktopSave,ofnDefaultDesktopSave : begin
if True{CompareText(ExtractFileExt(FileName),'.DSK') = 0} then begin
FileEventsForm.Caption := FileName;
FileEventsForm.Timer1.Enabled := True; // causes DSK file to be updated after Timer1.Interval (=2000ms)
end;
end;
end; { case }
end;
end;
procedure TFileEventsForm.btnClearClick(Sender: TObject);
begin
Memo1.Lines.Clear;
end;
function TFileEventsForm.GetCurrentProject: IOTAProject;
var
i: Integer;
begin
Result := nil;
ModServices := BorlandIDEServices as IOTAModuleServices;
for i := 0 to ModServices.ModuleCount - 1 do
begin
Module := ModServices.Modules[i];
if Supports(Module,IOTAProjectGroup,ProjectGroup) then begin
Result := ProjectGroup.ActiveProject;
Options := Result.ProjectOptions;
Exit;
end
else if Supports(Module,IOTAProject,Project) then
begin // In the case of unbound packages,return the 1st
if Result = nil then begin
Result := Project;
Options := Result.ProjectOptions;
end;
end;
end;
end;
procedure TFileEventsForm.SetUp;
begin
Project := GetCurrentProject;
Inc(SetUpCount);
Caption := 'Setup done ' + IntToStr(SetUpCount);
IsSetUp := True;
end;
procedure TFileEventsForm.LoadSettings;
var
Ini : TMemIniFile;
S : String;
begin
Ini := TMemIniFile.Create(DSKFileName);
try
S := Ini.ReadString('MySettings','Name','no value');
edMyValue.Text := S;
finally
Ini.Free;
end;
end;
procedure TFileEventsForm.SaveSettings;
var
Ini : TMemIniFile;
S : String;
begin
S := DSKFileName;
Caption := 'Saving: ' + S;
Ini := TMemIniFile.Create(S);
try
Ini.WriteString('MySettings',edMyValue.Text);
Ini.UpdateFile;
Ini.ReadSections(Memo2.Lines);
Memo2.Lines.Add('This file : ' + DSKFileName);
edMyValue.Text := '?';
finally
Ini.Free;
end;
end;
procedure TFileEventsForm.Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False;
SaveSettings;
end;
initialization
finalization
CloseDown;
end.
本文链接:https://www.f2er.com/3033807.html