4

我在 delphi IDE 专家中工作,我需要枚举 Delphi IDE 显示的所有表单,目前我正在使用该Screen.Forms属性,但我想知道是否存在使用 OTA 的另一种方法。因为Screen.Forms当我的专家是 BPL 但现在我正在迁移到 dll 专家时使用唯一的作品。

4

2 回答 2

8

Screen.Forms应该仍然可以从 DLL 工作。只需确保在选择“使用运行时包”链接器选项的情况下编译 DLL。这样,您的 DLL 将使用与 IDE 相同的 VCL 实例,并且您将可以访问所有相同的全局变量,包括Screen.

于 2011-05-29T21:55:41.707 回答
2

这完全可以通过 OpenToolsAPI 实现。

要提取 IDE 中所有打开的表单的列表,您可以使用以下内容:

procedure GetOpenForms(List: TStrings);
var
  Services: IOTAModuleServices;
  I: Integer;
  Module: IOTAModule;
  J: Integer;
  Editor: IOTAEditor;
  FormEditor: IOTAFormEditor;
begin
  if (BorlandIDEServices <> nil) and (List <> nil) then
  begin
    Services := BorlandIDEServices as IOTAModuleServices;
    for I := 0 to Services.ModuleCount - 1 do
    begin
      Module := Services.Modules[I];
      for J := 0 to Module.ModuleFileCount - 1 do
      begin
        Editor := Module.ModuleFileEditors[J];
        if Assigned(Editor) then
          if Supports(Editor, IOTAFormEditor, FormEditor) then
             List.AddObject(FormEditor.FileName,
               (Pointer(FormEditor.GetRootComponent)));
      end;
    end;
  end;
end;

请注意,该StringList中的指针是一个 IOTAComponent。要将这个问题解决为 TForm 实例,您必须深入挖掘。未完待续。

还可以通过向 IOTAServices 添加 IOTAIDENotifier 类型的通知程序来跟踪在 IDE 中打开的所有表单,如下所示:

type
  TFormNotifier = class(TNotifierObject, IOTAIDENotifier)
  public
    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;

implementation

var
  IdeNotifierIndex: Integer = -1;

procedure Register;
var
  Services: IOTAServices;
begin
  if BorlandIDEServices <> nil then
  begin
    Services := BorlandIDEServices as IOTAServices;
    IdeNotifierIndex := Services.AddNotifier(TFormNotifier.Create);
  end;
end;

procedure RemoveIdeNotifier;
var
  Services: IOTAServices;
begin
  if IdeNotifierIndex <> -1 then
  begin
    Services := BorlandIDEServices as IOTAServices;
    Services.RemoveNotifier(IdeNotifierIndex);
  end;
end;

{ TFormNotifier }

procedure TFormNotifier.AfterCompile(Succeeded: Boolean);
begin
  // Do nothing
end;

procedure TFormNotifier.BeforeCompile(const Project: IOTAProject;
  var Cancel: Boolean);
begin
  // Do nothing
end;

procedure TFormNotifier.FileNotification(NotifyCode: TOTAFileNotification;
  const FileName: String; var Cancel: Boolean);
begin
  if BorlandIDEServices <> nil then
    if (NotifyCode = ofnFileOpening) then
    begin
      //...
    end;
end;

initialization

finalization
  RemoveIdeNotifier;

end.
于 2011-05-29T21:59:29.870 回答