Third-Party Help Menu Item (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following unit uses the ToolsAPI to add a menu item ("My Company") to the Help > Third-Party Help menu of the IDE, and then add two child menu items to that new menu item:

  • "Offline Help", which opens a CHM file.
  • "Online Help", which opens a web page.

To use this code example:

  1. Add the unit code to a design-time package that is configured to use the Tools API.
  2. Replace 'Path\to\your\local\help\file' with the actual path to your help file. The code is meant to work with a CHM file, but other file formats should work as well.
  3. Replace 'http://example.com' with the main URL of your help.
  4. Compile the package to generate the <package>.bpl.
  5. Go to Component > Install Packages and add the path to the <package>.bpl.

Code

unit Unit1;

interface

uses
  System.SysUtils, ToolsAPI, Vcl.Menus, Winapi.ShellAPI, Winapi.Windows;

var
    NTAServices: INTAServices;
    MyHelpMenuItem: TMenuItem;
    MyOfflineHelpMenuItem: TMenuItem;
    MyOnlineHelpMenuItem: TMenuItem;

implementation

type
  TMethodContainer = class(TObject)
    procedure OnlineHelpClick(Sender: TObject);
    procedure OfflineHelpClick(Sender: TObject);
  end;

procedure OpenLocalFile(Path: String);
var
  SEInfo: TShellExecuteInfo;
begin
  if FileExists(Path) then
  begin
    FillChar(SEInfo, SizeOf(SEInfo), 0);
    SEInfo.cbSize := SizeOf(TShellExecuteInfo);
    SEInfo.Wnd := 0;
    SEInfo.lpFile := PChar(Path);
    SEInfo.lpParameters := nil;
    SEInfo.nShow := SW_SHOWDEFAULT;
    SEInfo.lpVerb := 'open';
    SEInfo.lpDirectory := PChar(ExtractFileDir(Path));
    ShellExecuteEx(@SEInfo);
  end;
end;

procedure OpenUrl(Url: String);
var
  SEInfo: TShellExecuteInfo;
begin
  FillChar(SEInfo, SizeOf(SEInfo), 0);
  SEInfo.cbSize := SizeOf(TShellExecuteInfo);
  SEInfo.Wnd := 0;
  SEInfo.lpFile := PChar(Url);
  SEInfo.lpParameters := nil;
  SEInfo.nShow := SW_SHOWDEFAULT;
  SEInfo.lpVerb := 'open';
  SEInfo.lpDirectory := nil;
  ShellExecuteEx(@SEInfo);
end;

procedure TMethodContainer.OfflineHelpClick(Sender: TObject);
begin
  OpenLocalFile('Path\to\your\local\help\file');
end;

procedure TMethodContainer.OnlineHelpClick(Sender: TObject);
begin
  OpenUrl('http://example.com');
end;

procedure Initialize(Sender: TObject);
var
  aMethodContainer:TMethodContainer;
begin
  if Supports(BorlandIDEServices, INTAServices, NTAServices) then
  begin
    aMethodContainer := TMethodContainer.Create;
    MyHelpMenuItem := TMenuItem.Create(nil);
    MyHelpMenuItem.Name := 'MyCompanyHelpMenuItem';
    MyHelpMenuItem.Caption := 'My Company';
    MyHelpMenuItem.ImageIndex := 16;
    NTAServices.AddActionMenu('HelpThirdPartyMenuItem', nil, MyHelpMenuItem, False, True);

    MyOnlineHelpMenuItem := TMenuItem.Create(MyHelpMenuItem);
    MyOnlineHelpMenuItem.Caption := 'Online Help';
    MyOnlineHelpMenuItem.ImageIndex := 153;
    MyOnlineHelpMenuItem.OnClick := aMethodContainer.OnlineHelpClick;
    NTAServices.AddActionMenu('MyCompanyHelpMenuItem', nil, MyOnlineHelpMenuItem, False, True);

    MyOfflineHelpMenuItem := TMenuItem.Create(MyHelpMenuItem);
    MyOfflineHelpMenuItem.Caption := 'Offline Help';
    MyOfflineHelpMenuItem.ImageIndex := 16;
    MyOfflineHelpMenuItem.OnClick := aMethodContainer.OfflineHelpClick;
    NTAServices.AddActionMenu('MyCompanyHelpMenuItem', nil, MyOfflineHelpMenuItem, False, True);
  end;
end;

procedure Finalize(Sender: TObject);
begin
  MyHelpMenuItem.Free;
end;

initialization

Initialize(nil);

finalization

Finalize(nil);

end.

Uses

See Also