I hardly use help files, but some older systems do, and when porting really old Delphi code, often odd implementations of accessing them through HHCTRL.OCX
are used.
Since I tend to forget the correct way using the HtmlHelpViewer
unit, here are some links:
- [WayBack] How to Connect HTML Help with your Delphi Application
- [WayBack] How to Integrate a CHM File with Your Application
- [WayBack] How to use CHM HTML Help file with Delphi XE application? – Stack Overflow
Very old code involving the OCX file:
- [WayBack] www.stbbs.net/~junichi/adelieworks/hhwd/files/awhhelp.pas
- [WayBack] MiniHtmlHlp.pas in delphi-nonvcl-librarys | source code search engine
Quote from the first link [WayBack] How to Connect HTML Help with your Delphi Application:
Linking HTML Help (CHM) Files
You should add the “HTMLHelpViewer” unit to the “Uses” clause in the main form of your application. Then set the full path to your CHM file to Application.HelpFile property. To do so, you can add the following line to the main form’s “On Create” event handler:
Application.HelpFile := ExtractFilePath(Application.ExeName) + 'HelpFile.chm';where “HelpFile.chm” is the actual name of your HTML Help file, located in the same directory as your application’s executable file.
Using HTML Help from Code
When you need to display your help file or a specific help topic, or perform others actions, you can use the following calls:
Displaying a help topic
Application.HelpContext(IDH_TOPIC);where
IDH_TOPIC
is the ContextId value of the topic to display.Displaying the Table of Contents tab
HtmlHelp(0, Application.HelpFile, HH_DISPLAY_TOC, 0);Displaying the Index tab
HtmlHelp(0, Application.HelpFile, HH_DISPLAY_INDEX, DWORD(PWideChar('Test')));Displaying the Search tab
var Query: THH_Fts_QueryW; begin with Query do begin cbStruct := SizeOf(THH_Fts_QueryW); fUniCodeStrings := True; pszSearchQuery := ''; iProximity := 0; fStemmedSearch := True; fTitleOnly := False; fExecute := True; pszWindow := nil; end; HtmlHelp(0, Application.HelpFile, HH_DISPLAY_SEARCH, DWORD(@Query)); end;Performing Keyword Lookup
Application.HelpKeyword('Test');Providing Help for Controls
You can link specific help topics with any controls located on the form. In this case a control will automatically display the corresponding help topic when the user focuses it and presses F1. Also, you can add the standard [?] button to the caption area of the form: using the Object Inspector, set the form’s BorderStyle property as bsDialog and biHelp member of the BorderIcons property to True. Then set the controls’ HelpContext properties that should correspond to the topic ContextId values as defined in your help project.
–jeroen
//——————————————————————————— | |
// Copyright (C) 2001-2002 by Junichi Sakamoto <adelieworks@yahoo.co.jp> | |
// Ver.2.0 | |
// | |
// 本ファイルの著作権は坂本純一が保持します。 | |
// 本ファイルは,改版の有無に関わらず,著作者に無断で転載することを禁じます。 | |
// (一切の転載を禁じるもではありません) | |
// 本ファイルの使用から生ずるいかなる被害・損害も、著作者は一切責任を負いません。 | |
// 本ファイルをそのままあるいは改版の上使用して生成された実行可能なバイナリファイル | |
// に対して、著作者は著作権を主張しません。本ファイルを改版の上使用することに対して | |
// 著作者は異議を申し立てません。 | |
// 本ファイルの利用は、上記但書に沿う限り、商用・非商用を問わず可能です。 | |
// | |
//——————————————————————————— | |
// 改版履歴 | |
// Ver.2.0 | |
// 1) デストラクタでHH_UNINITIALIZEファンクションを呼び出すのを削除(WinXP対応) | |
// 2) 設計時はOnHelpイベントをトラップしないよう修正(IDE終了時のアプリケーショ | |
// ンエラー対応) | |
// 3) AutoInstallプロパティを追加、OnHelpイベントハンドラを自動でセットアップ | |
// するかしないかを選べるように改良。 | |
// | |
// Ver.1.0 | |
//——————————————————————————— | |
unit awhhelp; | |
interface | |
uses | |
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ActiveX; | |
type | |
/////////////////////////////////////////////// | |
// HTMLヘルプサポートコンポーネント | |
THTMLHelp = class(TComponent) | |
protected | |
FCookie : Dword; | |
FPopupPos : TPoint; | |
FAutoInstall : Boolean; | |
procedure InitHtmlHelp(); | |
public | |
constructor Create(AOwner : TComponent); override; | |
function OnApplicationHelp(Command:Word; Data:longint; var CallHelp:boolean):boolean; | |
procedure Loaded; override; | |
published | |
property AutoInstall: Boolean read FAutoInstall write FAutoInstall default True; | |
end; | |
procedure Register; | |
implementation | |
type | |
/////////////////////////////////////////////// | |
// ポップアップ表示パラメータ構造体 | |
HH_POPUP = record | |
cbStruct : integer; // sizeof this structure | |
hinst : THandle; // instance handle for string resource | |
idString : UINT; // string resource id, or text id if pszFile is specified in HtmlHelp call | |
pszText : LPCTSTR; // used if idString is zero | |
pt : TPoint; // top center of popup window | |
clrForeground : COLORREF; // use -1 for default | |
clrBackground : COLORREF; // use -1 for default | |
rcMargins : TRect; // amount of space between edges of window and text, -1 for each member to ignore | |
pszFont : LPCTSTR; // facename, point size, char set, BOLD ITALIC UNDERLINE | |
end; | |
/////////////////////////////////////////////// | |
// エラー情報構造体 | |
HH_LAST_ERROR = record | |
cbStruct : integer; | |
hr : HRESULT; | |
description : PWideChar; | |
end; | |
/////////////////////////////////////////////// | |
// HtmlHelp API プロトタイプ | |
function HtmlHelp( | |
hwndCaller:HWND; pszFile:PAnsiChar; uCommand:Dword; dwData:Dword):HWND; stdcall; external 'hhctrl.ocx' name 'HtmlHelpA'; | |
function HtmlHelpA( | |
hwndCaller:HWND; pszFile:PAnsiChar; uCommand:Dword; dwData:Dword):HWND; stdcall; external 'hhctrl.ocx' name 'HtmlHelpA'; | |
function HtmlHelpW( | |
hwndCaller:HWND; pszFile:PWideChar; uCommand:Dword; dwData:Dword):HWND; stdcall; external 'hhctrl.ocx' name 'HtmlHelpW'; | |
const | |
/////////////////////////////////////////////// | |
// HtmlHelp API コマンドID | |
HH_INITIALIZE = $001C; | |
HH_UNINITIALIZE = $001D; | |
HH_DISPLAY_TOPIC = $0000; | |
HH_HELP_CONTEXT = $000F; | |
HH_DISPLAY_TEXT_POPUP = $000E; | |
HH_GET_LAST_ERROR = $0014; | |
/////////////////////////////////////////////// | |
// HTMLヘルプサポートコンポーネントの構築 | |
// Application.OnHelp イベントのハンドラを設定する | |
constructor THTMLHelp.Create(AOwner : TComponent); | |
begin | |
FCookie := Dword(-1); | |
FAutoInstall := True; | |
inherited Create(AOwner); | |
end; | |
procedure THTMLHelp.Loaded; | |
begin | |
inherited Loaded; | |
if( FAutoInstall )then begin | |
if( not ( csDesigning in ComponentState ) )then begin | |
Application.OnHelp := OnApplicationHelp; | |
end; | |
end; | |
end; | |
/////////////////////////////////////////////// | |
// HTMLヘルプシステムの初期化 (本メソッドは何回呼び出しても良い) | |
procedure THTMLHelp.InitHtmlHelp(); | |
begin | |
if FCookie = Dword(-1) then begin | |
HtmlHelp(0,nil,HH_INITIALIZE,Dword(@FCookie) ); | |
end; | |
end; | |
/////////////////////////////////////////////// | |
// Application.OnHelp イベントハンドラ | |
function THTMLHelp.OnApplicationHelp(Command:Word; Data:longint; var CallHelp:boolean):boolean; | |
var | |
helppath : string; | |
popuppath : string; | |
hRet : HWND; | |
hOwner : HWND; | |
popup : HH_POPUP; | |
lasterror : HH_LAST_ERROR; | |
begin | |
result := false; | |
InitHtmlHelp(); | |
hOwner := GetDesktopWindow(); | |
// ヘルプファイル名を取得/決定 | |
helppath := Application.CurrentHelpFile; | |
if helppath = '' then begin | |
helppath := ChangeFileExt(Application.ExeName,'.chm'); | |
end; | |
// 拡張子がchmではない->HTMLヘルプファイルではない場合は既存の実装にまかせる | |
if CompareText(ExtractFileExt(helppath),'.chm') <> 0 then exit; | |
CallHelp := false; | |
// コマンドに応じてHTMLヘルプAPIを呼び出す | |
hRet := HWND(-1); | |
case Command of | |
HELP_CONTENTS, | |
HELP_FINDER: | |
begin | |
hRet := HtmlHelp(hOwner,PChar(helppath),HH_DISPLAY_TOPIC,0); | |
end; | |
HELP_CONTEXT: | |
begin | |
hRet := HtmlHelp(hOwner,PChar(helppath),HH_HELP_CONTEXT,Data); | |
end; | |
HELP_SETPOPUP_POS: | |
begin | |
FPopupPos.x := LOWORD(Data); | |
FPopupPos.y := HIWORD(Data); | |
end; | |
HELP_CONTEXTPOPUP: | |
begin | |
hRet := HtmlHelp(hOwner,PChar(helppath),HH_HELP_CONTEXT,Data); | |
if hRet = 0 then begin | |
FillChar(popup,sizeof(popup),0); | |
popup.cbStruct := sizeof(popup); | |
popup.idString := Data; | |
popup.pt := FPopupPos; | |
popup.clrForeground := COLORREF(-1); | |
popup.clrBackground := COLORREF(-1); | |
popup.rcMargins.left := –1; | |
popup.rcMargins.top := –1; | |
popup.rcMargins.right := –1; | |
popup.rcMargins.bottom := –1; | |
popuppath := helppath+'::/cshelp.txt'; | |
HtmlHelp(Screen.ActiveForm.Handle,PChar(popuppath),HH_DISPLAY_TEXT_POPUP,DWORD(@popup) ); | |
exit; | |
end; | |
end; | |
end; | |
// エラーメッセージがあればメッセージボックスで表示 | |
if hRet = 0 then begin | |
HtmlHelp(hOwner,nil,HH_GET_LAST_ERROR, DWORD(@lasterror) ); | |
if( FAILED(lasterror.hr) )and( lasterror.description <> nil )then begin | |
Application.MessageBox(PChar(AnsiString(lasterror.description)),PChar(Application.Title),MB_OK+MB_ICONSTOP); | |
SysFreeString( lasterror.description ); | |
end; | |
end; | |
end; | |
procedure Register; | |
begin | |
RegisterComponents('adelie works', [THTMLHelp]); | |
end; | |
end. |