我想找出在Delphi中,我需要使用哪些服务/接口来向源文件的右键菜单中添加一个项。
例如,如果我右键单击一个单元的选项卡,它有要“关闭页面”、“关闭所有其他页面”、“属性”等项。如果可能的话,我希望将自定义项添加到该菜单中。
我查看了ToolsAPI单元,但是我不知道从哪里开始。我假设有一个接口,我可以用来枚举项目和添加项目,但我不知道从哪里开始查找。
如果这是不可能的,我会满足于代码编辑器的上下文菜单。
也许网上有一些样本,但我还在找,却没有找到。
任何帮助都很感激。
发布于 2016-12-08 12:34:12
雷米·莱博( Remy )通过他与GExperts指南的链接,为你指明了正确的方向。
如果您以前没有做过这类工作,那么开始编写您自己的IDE外接程序可能还需要一些性能,因此我在下面给出了一个关于如何向代码编辑器的弹出菜单中添加项的最小示例。
显然,您要做的是创建一个新的包,将下面的单元添加到其中,然后在IDE中安装包。对单元中的Register的调用完成了在编辑器弹出菜单中安装新项目所必需的工作。
确保在安装包时代码编辑器是打开的。原因是,正如您将看到的,代码检查当时是否有活动编辑器。我留下了如何确保弹出项被添加,即使当时没有激活的代码编辑器。提示:如果您查看正在使用的Delphi的任何版本的ToolsAPI.Pas单元,您会发现它包含各种类型的通知器,您可以使用其中至少一个发出的通知来推迟检查是否有编辑器处于活动状态,直到可能有一个编辑器处于活动状态。
顺便说一句,代码将菜单项添加到上下文菜单中,该菜单从编辑器窗口本身弹出,而不是活动选项卡。IDE外接程序的乐趣之一是试验,看看你是否能得到你想要的东西。我自己也没有尝试过,但我怀疑将菜单项添加到编辑器选项卡的上下文菜单中是否会那么困难--因为Delphi是一个Delphi应用程序,从下面的代码中可以看到,您可以使用FindComponent (或者只是迭代组件集合)来查找您想要的内容。但是,如果可以的话,最好是通过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;https://stackoverflow.com/questions/41031594
复制相似问题