簡體   English   中英

使用ToolsAPI在Delphi IDE中將菜單項添加到單位的選項卡上下文菜單

[英]Add menu item to unit's tab context menu in Delphi IDE using ToolsAPI

我正在尋找要在Delphi IDE中將項目添加到源文件的右鍵菜單中所需的服務/接口。

例如,如果我右鍵單擊某個設備的選項卡,則其中包含“關閉頁面”,“關閉所有其他頁面”,“屬性”等項目。如果可能,我想向該菜單添加自定義項目。

我查看了ToolsAPI單元,但不知道從哪里開始。 我假設有一個可以用來枚舉項目和添加項目的界面,但是我不知道從哪里開始尋找。

如果那不可能,我將選擇代碼編輯器的上下文菜單。

也許網上有一些樣品,但是我還在尋找,沒有發現。

任何幫助表示贊賞。

雷米·勒博(Remy Lebeau)指向GExperts指南的鏈接為您指明了正確的方向。

如果您以前沒有做過這種事情,那么開始編寫自己的IDE加載項仍然會有一些性能,因此,我在下面提供了一個如何向其中添加項的最小示例。代碼編輯器的彈出菜單。

顯然,您要做的是創建一個新程序包,向其添加下面的單元,然后在IDE中安裝該程序包。 在單元中調用Register可以完成將新項目安裝到編輯器彈出菜單中所需的操作。

在安裝軟件包時,請確保代碼編輯器已打開。 如您所見,原因是該代碼檢查當時是否有活動的編輯器。 我留下了如何確保即使當前沒有活動的代碼編輯器,也要添加彈出項的方法。 提示:如果查看所使用的任何版本的Delphi的ToolsAPI.Pas單元,您會發現它包含各種通知程序,並且您可以使用至少其中之一的通知來推遲檢查是否存在是活躍的編輯器,直到有可能成為可能為止。

順便說一句,該代碼將菜單項添加到上下文菜單,該上下文菜單彈出編輯器窗口本身而不是活動選項卡。 IDE加載項的樂趣之一是嘗試查看是否可以完全獲得所需的樂趣。 我自己還沒有嘗試過,但是我懷疑將菜單項添加到一個編輯器選項卡的上下文菜單中會很困難-鑒於Delphi IDE是Delphi應用程序,如下面的代碼所示,您可以使用FindComponent(或僅遍歷Components集合)來查找所需的內容。 但是,如果可以,最好通過ToolsAPI接口定位。 請參閱下面的更新

interface

uses
  Classes, Windows, Menus, Dialogs, ToolsAPI;

type

   TIDEMenuItem = class(TNotifierObject, IOTAWizard, IOTAMenuWizard)
     function GetName: string;
     function GetIDString: string;
     function GetMenuText: string;
     function GetState: TWizardState;
     procedure Execute;
   end;

   TIDEMenuHandler = class(TObject)
     procedure HandleClick(Sender: TObject);
   end;

procedure Register;

implementation

var
  MenuItem: TMenuItem;
  IDEMenuHandler: TIDEMenuHandler;
  EditorPopUpMenu : TPopUpMenu;

procedure TIDEMenuItem.Execute;
begin
  ShowMessage('Execute');
end;

function TIDEMenuItem.GetIDString: string;
begin
  Result := 'IDEMenuItemID';
end;

function TIDEMenuItem.GetMenuText: string;
begin
  Result := 'IDEMenuItemText';
end;

function TIDEMenuItem.GetName: string;
begin
  Result := 'IDEMenuItemName';
end;

function TIDEMenuItem.GetState: TWizardState;
begin
  Result := [wsEnabled];
end;

procedure TIDEMenuHandler.HandleClick(Sender: TObject);
begin
  ShowMessage(TIDEMenuItem(Sender).GetName + ' Clicked');
end;

procedure AddIDEMenu;
var
  NTAServices: INTAServices40;
  EditorServices: IOTAEditorServices;
  EditView: IOTAEditView;
begin
  NTAServices := BorlandIDEServices as INTAServices40;

  EditorServices := BorlandIDEServices as IOTAEditorServices;
  EditView := EditorServices.TopView;

  if Assigned(EditView) then begin
    EditorPopUpMenu := TPopUpMenu(EditView.GetEditWindow.Form.FindComponent('EditorLocalMenu'));
    Assert(EditorPopUpMenu <>Nil);

    IDEMenuHandler := TIDEMenuHandler.Create;
    MenuItem := TMenuItem.Create(Nil);
    MenuItem.Caption := 'Added IDE editor menu item';
    MenuItem.OnClick := IDEMenuHandler.HandleClick;
    EditorPopUpMenu.Items.Add(MenuItem)
  end
  else
    ShowMessage('Code editor not active');
end;

procedure RemoveIDEMenu;
begin
  if MenuItem <> Nil then begin
    EditorPopUpMenu.Items.Remove(MenuItem);
    FreeAndNil(MenuItem);
    IDEMenuHandler.Free;
  end;
end;

procedure Register;
begin
  RegisterPackageWizard(TIDEMenuItem.Create);
  AddIDEMenu;
end;

initialization

finalization
  RemoveIDEMenu;
end.

更新:以下代碼查找編輯器窗口的TabControl,並將菜單項添加到其上下文菜單。 但是,請注意,這並不意味着打開了第二個編輯器窗口。

procedure AddIDEMenu;
var
  NTAServices: INTAServices40;
  EditorServices: IOTAEditorServices;
  EditView: IOTAEditView;
  TabControl : TTabControl;

  function FindTabControl(AComponent : TComponent) : TTabControl;
  var
    i : Integer;
  begin
    Result := Nil;
    if CompareText(AComponent.ClassName, 'TXTabControl') = 0 then begin
      Result := TTabControl(AComponent);
      exit;
    end
    else begin
      for i := 0 to AComponent.ComponentCount - 1 do begin
        if CompareText(AComponent.Components[i].ClassName, 'TXTabControl') = 0 then begin
          Result := TTabControl(AComponent.Components[i]);
          exit;
        end
        else begin
          Result := FindTabControl(AComponent.Components[i]);
          if Result <> Nil then
            exit;
        end;              
      end;
    end;
  end;

begin
  NTAServices := BorlandIDEServices as INTAServices40;

  EditorServices := BorlandIDEServices as IOTAEditorServices;
  EditView := EditorServices.TopView;

  if Assigned(EditView) then begin
    TabControl := FindTabControl(EditView.GetEditWindow.Form);
    Assert(TabControl <> Nil, 'TabControl not found');
    EditorPopUpMenu := TabControl.PopupMenu;
    Assert(EditorPopUpMenu <> Nil, 'PopUP menu not found');
    //EditorPopUpMenu := TPopUpMenu(EditView.GetEditWindow.Form.FindComponent('EditorLocalMenu'));
    Assert(EditorPopUpMenu <>Nil);

    IDEMenuHandler := TIDEMenuHandler.Create;
    MenuItem := TMenuItem.Create(Nil);
    MenuItem.Caption := 'Added IDE editor menu item';
    MenuItem.OnClick := IDEMenuHandler.HandleClick;
    EditorPopUpMenu.Items.Add(MenuItem)
  end
  else
    ShowMessage('No editor active');
end;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM