1

我使用 INTAServices40 在 Delphi 中成功安装了我的菜单项,但问题是 - 下次 Delphi 启动时菜单丢失?!实际上,安装了两个菜单项;帮助菜单下的一个总是显示(IOTAWizardMenu),但工具菜单下的一个(测试菜单项)在下次 Delphi 启动时丢失。如何解决这个问题?

unit TESTMENU;

interface    
uses
   ToolsAPI, Classes, Windows, vcl.Menus, vcl.dialogs;    
type
   TCustomMenuItem = class(TNotifierObject, IOTAWizard, IOTAMenuWizard)
     function GetIDString: string;
     function GetName: string;
     function GetState: TWizardState;
     // Launch the AddIn
     procedure Execute;
     function GetMenuText: string;
   end;

   TCustomMenuHandler = class(TObject)
    // Handle custom menu
     procedure HandleClick(Sender: TObject);
   end;

procedure Register;

implementation    
var
  mnuitem: TMenuItem;
  CustomMenuHandler: TCustomMenuHandler;

procedure TCustomMenuItem.Execute;
begin
  ShowMessage('IOTAWizardMenu based menu item');
end;

function TCustomMenuItem.GetIDString: string;
begin
  Result := 'TMS.MenuSample';
end;

function TCustomMenuItem.GetMenuText: string;
begin
  Result := 'IOTAWizardMenu';
end;

function TCustomMenuItem.GetName: string;
begin
  Result := 'TMSMenuSample';
end;

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

procedure TCustomMenuHandler.HandleClick(Sender: TObject);
begin
  ShowMessage('INTAServices40.MainMenu based menu item');
end;   

procedure AddIDEMenu;
var
  NTAServices: INTAServices40;    
begin
  NTAServices := BorlandIDEServices as INTAServices40;

  // avoid inserting twice
  if NTAServices.MainMenu.Items[9].Find('TEST') = nil then
  begin
    CustomMenuHandler := TCustomMenuHandler.Create;

    mnuitem := TMenuItem.Create(nil);
    mnuitem.Caption := 'TEST';
    mnuitem.OnClick := CustomMenuHandler.HandleClick;
    NTAServices.MainMenu.Items[9].Add(mnuitem)
  end;
end;

procedure RemoveIDEMenu;
var
  NTAServices: INTAServices40;
begin
  if Assigned(mnuitem) then
  begin
    NTAServices := BorlandIDEServices as INTAServices40;
    NTAServices.MainMenu.Items[9].Remove(mnuitem);
    mnuitem.Free;
    if Assigned(CustomMenuHandler) then
      CustomMenuHandler.Free;
  end;
end;

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

initialization
  mnuitem := nil;    
  CustomMenuHandler := nil;    
finalization
  RemoveIDEMenu;
end.

所以,我的第一个问题是如何在每次 Delphi 启动时显示菜单项 TEST。另外,我想在菜单项 TEST 旁边添加图标。有什么方向吗?

谢谢

编辑: 我刚刚发现我的包裹延迟加载。看网上的人说 ForceDemandLoadState(dlDisable) 应该被调用。但是,这对我也没有帮助......

4

1 回答 1

1

NTAServices.MainMenu.Items[9]当 IDE 加载它的包时,可能会在不同的时间返回不同的东西,还有一些菜单项,其子项在运行时由 IDE 管理(例如,窗口菜单)。

您可以按名称查找帮助菜单项组件:

procedure AddIDEMenu;
var
  HelpMenu: TComponent;
begin
  HelpMenu := Application.MainForm.FindComponent('HelpMenu');
  if (HelpMenu is TMenuItem) and (TMenuItem(HelpMenu).Find('TEST') = nil) then
  begin
    CustomMenuHandler := TCustomMenuHandler.Create;

    mnuitem := TMenuItem.Create(nil);
    mnuitem.Caption := 'TEST';
    mnuitem.OnClick := CustomMenuHandler.HandleClick;

    TMenuItem(HelpMenu).Add(mnuitem);
  end;
end;

procedure RemoveIDEMenu;
var
  HelpMenu: TComponent;
begin
  if Assigned(mnuitem) then
  begin
    HelpMenu := Application.MainForm.FindComponent('HelpMenu');
    if HelpMenu is TMenuItem then
      TMenuItem(HelpMenu).Remove(mnuitem);
    mnuitem.Free;
    CustomMenuHandler.Free;
  end;
end;
于 2012-10-08T11:54:49.663 回答