Delphi OpenTools API:如何获取 AfterCompile 通知?
Posted
技术标签:
【中文标题】Delphi OpenTools API:如何获取 AfterCompile 通知?【英文标题】:Delphi OpenTools API: How to get AfterCompile notifications? 【发布时间】:2011-10-31 15:23:43 【问题描述】:OpenTools API 定义了一个接口,用于在编译之前和之后进行编译:
IOTAIDENotifier = interface(IOTANotifier)
['E052204F-ECE9-11D1-AB19-00C04FB16FB3']
procedure FileNotification(NotifyCode: TOTAFileNotification; const FileName: string; var Cancel: Boolean); // This procedure is called for many various file operations within the IDE
procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean); overload; // This function is called immediatately before the compiler is invoked. Set Cancel to True to cancel the compile
procedure AfterCompile(Succeeded: Boolean); overload; // This procedure is called immediately following a compile. Succeeded will be true if the compile was successful
end;
我创建了一个导出此界面的向导:
TDBGExportWizard = class(TNotifierObject, IOTANotifier, IOTAIDENotifier, IOTAIDENotifier50, IOTAWizard)
public
IOTANotifier
// procedure AfterSave; //This procedure is called immediately after the item is successfully saved. This is not called for IOTAWizards
// procedure BeforeSave; //This function is called immediately before the item is saved. This is not called for IOTAWizard
// procedure Destroyed; // The associated item is being destroyed so all references should be dropped. Exceptions are ignored.
// procedure Modified; // This associated item was modified in some way. This is not called for IOTAWizards
IOTAIDENotifier
procedure FileNotification(NotifyCode: TOTAFileNotification; const FileName: string; var Cancel: Boolean);
procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean); overload;
procedure AfterCompile(Succeeded: Boolean); overload;
IOTAIDENotifier50
procedure BeforeCompile(const Project: IOTAProject; IsCodeInsight: Boolean; var Cancel: Boolean); overload;
procedure AfterCompile(Succeeded: Boolean; IsCodeInsight: Boolean); overload;
IOTAWizard
function GetIDString: string;
function GetName: string;
function GetState: TWizardState;
procedure Execute;
end;
并通过以下方式注册向导:
procedure Register;
begin
RegisterPackageWizard(TDBGExportWizard.Create);
end;
但两者都不是:
IOTAIDENotifier.BeforeCompile
IOTAIDENotifier.AfterCompile
IOTAIDENotifier.FileNotification
IOTAIDENotifier50.BeforeCompile
IOTAIDENotifier50.AfterCompile
被调用。我做错了什么?
唯一的称呼是
IOTAWizard.GetName
IOTAWizard.GetIDString
按这个顺序。
【问题讨论】:
【参考方案1】:以下来自我们软件的一小部分(我剪掉了一些代码,但它应该可以工作)
unit UnitSymbolInsert;
interface
procedure Register;
implementation
uses
SysUtils, ToolsApi, Classes, Types, ActiveX, UnitSymbols;
type
TCITNotifier = class(TNotifierObject, IOTANotifier, IOTAIDENotifier, IOTAIDENotifier50 )
protected
// IOTAIDENotifier
procedure FileNotification(NotifyCode: TOTAFileNotification; const FileName: string; var Cancel: Boolean);
procedure BeforeCompile(const Project: IOTAProject; var Cancel: Boolean); overload;
procedure AfterCompile(Succeeded: Boolean); overload;
// IOTAIDENotifier50
procedure BeforeCompile(const Project: IOTAProject; IsCodeInsight: Boolean; var Cancel: Boolean); overload;
procedure AfterCompile(Succeeded: Boolean; IsCodeInsight: Boolean); overload;
end;
var
NotifierIndex: Integer = -1;
procedure Register;
var
Services: IOTAServices;
begin
if not Supports(BorlandIDEServices, IOTAServices, Services) then Exit;
NotifierIndex := Services.AddNotifier(TCITNotifier.Create);
end;
procedure UnRegister;
var
Services: IOTAServices;
begin
if (NotifierIndex < 0) or not Supports(BorlandIDEServices, IOTAServices, Services) then Exit;
Services.RemoveNotifier(NotifierIndex);
end;
TCITNotifier
procedure TCITNotifier.AfterCompile(Succeeded, IsCodeInsight: Boolean);
begin
// Only when we have a succesfully build for a project.
if not Succeeded or IsCodeInsight then Exit;
// do something useful here!!!!
end;
procedure TCITNotifier.BeforeCompile(const Project: IOTAProject; IsCodeInsight: Boolean; var Cancel: Boolean);
begin
// Not used
end;
procedure TCITNotifier.AfterCompile(Succeeded: Boolean);
begin
// Not used
end;
procedure TCITNotifier.BeforeCompile(const Project: IOTAProject; var Cancel: Boolean);
begin
// Not used
end;
procedure TCITNotifier.FileNotification(NotifyCode: TOTAFileNotification; const FileName: string; var Cancel: Boolean);
begin
// Not used
end;
initialization
;
finalization
UnRegister;
end.
希望我没有剪掉太多。 构建一个 dpk 并将其安装到 IDE 中。
【讨论】:
看起来令人印象深刻,但这与 OP 发布的课程有何不同? @GolezTrol 确实它们看起来一样。不同之处在于注册部分。 OP 将该类注册为一个单独的向导,但 Ritsaert 将该类添加到当前的 IDE 通知程序中。后者有效。 @GolezTrol 不同之处在于这是它应该工作的方式。如果您不注册通知程序,则永远不会调用回调。 This one 应该仍然可以工作(至少作为示例)。IOTAWizard
的删除,我现在什么也得不到。以上是关于Delphi OpenTools API:如何获取 AfterCompile 通知?的主要内容,如果未能解决你的问题,请参考以下文章
Delphi OpenTools API - 编辑项目需要子句