Delphi XE2: Jumping to an anchor in CHM?

2019-02-17 14:12发布

问题:

In a Delphi XE2 program, how do I jump to an anchor inside a CHM help file topic? The anchor has the following format (extracted from the source of the topic page in HTML HelpViewer showing the CHM file):

<a name="my_anchor_id"></a>

I tried the following:

Application.HelpJump('MyTopicName.htm#my_anchor_id');

Unfortunately, this does not work: It does jump to this topic, but only to the top of the topic, not to the anchor, which is several scrolls down the page.

回答1:

Jumping to an anchor in CHM
Tested with Delphi2010 - DelphiXE2 - Windows XP

How to jump to an anchor in a chm file (Compiled HTML Help) with Delphi.

Yes, it is possible with a HtmlHelp() command to jump to an anchor.
<a name="my_anchor"></a>

 HtmlHelp(0,hpPath+'::/Overview.htm#UsingtheMenus>main',HH_DISPLAY_TOPIC,DWORD(nil));

The LINK in : Overview.htm

  • Note: This link is not needed to jump via the delphi program to
    the anchor.(Only for testing).

Overview.htm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
[...]
<BODY bgColor=#f7f8e2>
<H1>Overview</H1>
<A HREF="Overview.htm#UsingtheMenus">Using the Menus</A>
[...]

The ANCHOR in : Overview.htm

[...]
<A NAME="UsingtheMenus" </A>
<P><STRONG>Using the Menus and Toolbars</STRONG>
[...]

If anyone wants to try it.

Here are more information and a testprogram:

Run Project1.exe and select without :

The Unit1.pas / the command

procedure TForm1.Button2Click(Sender: TObject);
begin
    HtmlHelp(0,hpPath+'::/Overview.htm#UsingtheMenus>main',HH_DISPLAY_TOPIC,DWORD(nil));
end;
  • click the Button Overview.htm#UsingtheMenus

The Result:

The Help file is opened and the overview.htm appears. It was jumped to the anchor.


Download source Project1.exe D2010 and XE2.
Download source sample.chm HtmlHelp project.

Download


Now let's test the other 2 Buttons

procedure TForm1.HelpKeywordClick(Sender: TObject);
begin
  Application.HelpKeyword('UsingtheMenus');
end;

procedure TForm1.HelpContextClick(Sender: TObject);
begin
  Application.HelpContext(IDH_UsingtheMenus);
end;
  • Click the Button HelpKeyword('UsingtheMenus')

The Result:

The Help file is opened and the FirstTopic.htm appears. The text UsingtheMenus is inserted into the searchfield. No jump to the anchor!


  • Click the Button HelpContext(IDH_UsingtheMenus)')

The same result except the search box is empty.


The last 2 clicks demonstrate here:
There are no secret IDs or keywords in the file HelpFile2\sample.chm available.


Now we change the help file so that in addition to Overview.htm#UsingtheMenus the other two methods lead to success.

  • Close the App 'Project.exe`
  • Run 'Project.exe` again.
  • Make sure the with is selected.

Now we use "sample.chm" in folder HelpFile\
with the following settings.

sample.h

#define IDH_Creating_Projects_and_Topics 1005
#define IDH_Overview 1003
#define IDH_UsingtheMenus 1009

sample.ali

IDH_Overview=Overview.htm
IDH_welcom=FirstTopic.htm
IDH_UsingtheMenus=Overview.htm#UsingtheMenus

sample.hcc

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
[...]
        <LI> <OBJECT type="text/sitemap">
            <param name="Name" value="Using the Menus">
            <param name="Local" value="Overview.htm#UsingtheMenus">
        </OBJECT>   
[...]
</HTML>

sample.hhk

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<HTML>
[...]
    <LI> <OBJECT type="text/sitemap">
        <param name="Name" value="Using the Menus">
        <param name="Name" value="Using the Menus">
        <param name="Local" value="Overview.htm#UsingtheMenus">
        </OBJECT>   
[...]
</HTML>

If we compile "sample.chm", we get an error message.

HHC3015: Warning: An alias has been created to "Overview.htm#UsingtheMenus" but the file does not exist.

This is normal because the file "Overview.htm#UsingtheMenus" does not really exist !

Let's try it

Do not forget, the "Help window" after trying each of the following, to close again. Otherwise you can not track the different states.

  • Click the Button HelpKeyword('UsingtheMenus')

The Result:
The Help file is opened and the FirstTopic.htm appears. The text UsingtheMenus is inserted into the searchfield. The text UsingtheMenus is selected in the listbox
If you click Show
It was jumped to the anchor.


  • Click the Button HelpContext(IDH_UsingtheMenus)')

The Result:

The Help file is opened and the overview.htm appears. It was jumped to the anchor.


  • click the Button Overview.htm#UsingtheMenus

The Result:

Same result as above.
The Help file is opened and the overview.htm appears. It was jumped to the anchor.


Now we can use one of three methods to jump to the desired mark.


If anyone is interested in the functioning of the other buttons, then read on and get more information.

will be continued tomorrow.