如何从delphi IDE专家中枚举IDE的形式

时间:2011-05-29 21:03:34

标签: delphi delphi-2007 toolsapi

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

2 个答案:

答案 0 :(得分:8)

Screen.Forms仍应使用DLL。只需确保使用选中的“use runtime packages”链接器选项编译DLL。这样,您的DLL将使用与IDE相同的VCL实例,您将可以访问所有相同的全局变量,包括Screen

答案 1 :(得分: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[I];
        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.