Chris Were
Chris Were

Reputation: 161

Delphi console application: how to add OnHelp to message dialogs?

I have a Delphi console application that updates components on a server. It runs semi-interactively, there's the occasional prompt "are you sure?" etc, via this code:

MessageDlg('Are you sure?', mtConfirmation, [mbYes, mbNo, mbHelp], SOME_HELP_CONTEXT)...

As you can see, I'd like to supply help links to it, in this case going to our website with SOME_HELP_CONTEXT appended to our help page: ?contextid=SOME_HELP_CONTEXT

I am doing this from our GUI programs and I assign Application.OnHelp := myHelper; where myHelper is a method of an object which simply calls ShellExecute to open the web link.

But there's no Application variable in a console app.

Is there a fairly simple way to achieve this?

Upvotes: 3

Views: 601

Answers (1)

Sertac Akyuz
Sertac Akyuz

Reputation: 54832

Since you are using the Dialogs unit, you already have the Application variable. Because, Dialogs uses Controls, which initializes the Application variable that is in the Forms unit. All you have to do is to additionally use the Forms unit in your code.

program Project1;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, Vcl.Dialogs, Vcl.Forms, Winapi.Windows;

type
  THelper = class
  protected
    function OnHelp(Command: Word; Data: THelpEventData; var CallHelp: Boolean): Boolean; virtual;
  end;

{ THelper }

function THelper.OnHelp(Command: Word; Data: THelpEventData;
  var CallHelp: Boolean): Boolean;
begin
  MessageBox(GetActiveWindow, PChar(Format('help request about "%d"', [Data])), '', 0);
  CallHelp := False;
end;

var
  Helper: THelper;
begin
  try
    Helper := THelper.Create;
    Application.OnHelp := Helper.OnHelp;
    MessageDlg('Are you sure?', mtConfirmation, [mbYes, mbNo, mbHelp], 5);
    Helper.Free;
    { TODO -oUser -cConsole Main : Insert code here }
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

Upvotes: 4

Related Questions