Forum > Windows
Context Help in .chm file
(1/1)
Timbo:
I'm converting an application developed in Delphi, potentially one of many. It uses context help in a .chm file, with context numbers set in the file and the current context number set within the code. This works correctly with Delphi.
I have a CHMHelpDatabase and LHelpConnector on the main form, and have initialised them according to the examples, and everything seems to be working - except that when, for example, the help context is 200, I get a message, presumably from LHelp, saying "Help context 200 not found".
As as aside, is there a way I can bypass LHelp and simply invoke the default CHM file viewer?
Thanks.
wp:
--- Quote from: Timbo on September 28, 2021, 04:39:09 pm ---is there a way I can bypass LHelp and simply invoke the default CHM file viewer?
--- End quote ---
I don't have a simple example for this, but you could look at the application LazStats (https://sourceforge.net/p/lazarus-ccr/svn/HEAD/tree/applications/lazstats/ - download the snapshot if you don't have svn. No external components are required) which I modified to use chm help massively. Look at the MainUnit (in folder "forms"):
- Add the unit htmlhelp to "uses"; it comes with FPC (packages/winunits-base)
- Add a private method "function HelpHandler(Command: Word; Data: PtrInt; var CallHelp: Boolean): Boolean;" to the main form which does this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---// Call HTML help (.chm file)function TOS3MainFrm.HelpHandler(Command: Word; Data: PtrInt;var s: String; ws: UnicodeString; res: Integer;begin if Command = HELP_CONTEXT then begin // see: http://www.helpware.net/download/delphi/hh_doc.txt ws := UnicodeString(Application.HelpFile); res := htmlhelp.HtmlHelpW(0, PWideChar(ws), HH_HELP_CONTEXT, Data); // Data is HelpContext here end else if Command = HELP_COMMAND then begin s := {%H-}PChar(Data); // Data is pointer to HelpKeyword here, but the Windows help viewer does // not want the KeywordPrefix required for LHelp. if pos(HELP_KEYWORD_PREFIX + '/', s) = 1 then Delete(s, 1, Length(HELP_KEYWORD_PREFIX) + 1); ws := UnicodeString(Application.HelpFile + '::/' + s); res := htmlhelp.HtmlHelpW(0, PWideChar(ws), HH_DISPLAY_TOPIC, 0); end; // Don't call regular help CallHelp := False; Result := res <> 0;end;
- In the main form's OnCreate handler, assign it to the Application.OnHelp event, and set the Application.Helpfile to the name of your chm file:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var helpfn: String;begin ... helpfn := Application.Location + 'LazStats.chm'; if FileExists(helpfn) then begin Application.HelpFile := helpfn; Application.OnHelp := @HelpHandler; end else MessageDlg('LazStats help file not found.', mtError, [mbOK], 0);end;
This is for Windows only. Study the real code in the application which is extended to use the Lazarus-internal help-database instead which - of course - is cross-platform.
Timbo:
Thanks very much.
I had trouble with line 9 in the second snippet of code, getting an error for the @.
I fixed it by setting the OnHelp handler for the form using the Object Inspector instead, and now everything is working perfectly. Thanks again!
marcov:
--- Quote from: Timbo on September 29, 2021, 12:47:54 pm ---Thanks very much.
I had trouble with line 9 in the second snippet of code, getting an error for the @.
--- End quote ---
That's a difference between $mode delphi and $mode objfpc probably. In mode delphi just leave it out.
Navigation
[0] Message Index