最好先判断一下,帮助文件存在否`~ uses shellapi;procedure Tfrm_stumain.N30Click(Sender: TObject); var HWndHelp:Hwnd; i:integer; begin //检查帮助窗口是否已经存在 HWndHelp:=FindWindow(nil,conHelpTitle); if HwndHelp<>0 then // 如存在则关闭 SendMessage(HwndHelp,WM_CLOSE,0,0); i:=ShellExecute(handle, 'open',Pchar( 'Help.chm'),nil, nil, sw_ShowNormal); if i<>42 then Showmessage('对不起,帮助文件已经损坏!'); end;
interface
uses Windows;
type DWORD_PTR = ^DWORD;
Function
HtmlHelp(hwndCaller:HWND;strFile:String;
uCommand:UINT; dwData:DWORD_PTR ):HWND;
procedure CloseHtmlHelp;
implementation
uses
SysUtils;
const
HHControlInstance:THandle=0;
dwCookie :DWORD = 0;
var
HtmlHelpA:function ( hwndCaller:HWND; pszFile:PChar ; uCommand:UINT; dwData:DWORD_PTR ):HWND;stdcall;
function HtmlHelp(hwndCaller:HWND;strFile:String; uCommand:UINT; dwData:DWORD_PTR ):HWND;
var
LFileName:String;
p:PChar;
begin
if HHControlInstance=0 then
begin
LFileName := StringOfChar( ' ', 256);
p := PChar( LFilename );
GetSystemDirectory(p,255);
StrCat(p,'\HHCTRL.OCX');
HHControlInstance := LoadLibrary( P );
if HHControlInstance = 0 then
raise exception.Create('Help system not installed!'#13' HTMLHELP cannot displayed!');
@HtmlHelpA := GetProcAddress( HHControlInstance, 'HtmlHelpA');
if @HtmlHelpA = nil then
raise exception.Create('Function HTMLHELP cannot loaded!');
HtmlHelpA( 0, nil,$001C , (@dwCookie));
end;
result := HtmlHelpA( hwndCaller, PChar( strFile ), uCommand, dwData );
end;
procedure CloseHtmlHelp;
begin
if HHControlInstance<>0 then
begin
HtmlHelpA( 0, nil, $001D, DWORD_PTR(dwCookie));
FreeLibrary(HHControlInstance);
end;
end;
end.
两个函数分别初始化和释放调用接口。其它模块只须按约定调用即可。例如:
HtmlHelp( handle, htmlhelpfilename+'::/welcome.htm',$0000, nil);
显示htmlhelpfilename对应的帮助文件的welcome页面。
上下文敏感帮助需要借用Delphi对WinHelp的支持。当用户按 F1 键时,程序将自动触发OnHelp事件,截获它,编写自己的处理代码即可。 ... ...
Application.HelpFile := htmlhelpfilename;
tmpOnHelp := Application.OnHelp;
Application.OnHelp := AppHtmlHelp;
... ...
function TForm1.AppHtmlHelp(Command: Word; Data: Longint;
var CallHelp: Boolean): Boolean;
var ret:integer;Hfile:string;
begin
if not CallHelp then exit;
AppPath := ExtractFilePath(Application.ExeName);
Hfile := AppPath + Application.HelpFile;
case Command of
HELP_FINDER, HELP_CONTENTS:
ret := HtmlHelp(handle, pchar(Hfile), $0001, nil);
HELP_QUIT:
ret := HtmlHelp(0, '', $0012, nil);
HELP_CONTEXT:
ret := HtmlHelp(handle, pchar(Hfile), $000f, DWORD_PTR(data));
end;
result:=ret<>0;
CallHelp := False;
end;
这样,我们就有另一种方法调用帮助文件,与传统WinHelp调用方法一样: application.helpcommand(HELP_FINDER, 0);
application.helpcommand(HELP_quit, 0);
uses shellapi;procedure Tfrm_stumain.N30Click(Sender: TObject);
var
HWndHelp:Hwnd;
i:integer;
begin
//检查帮助窗口是否已经存在
HWndHelp:=FindWindow(nil,conHelpTitle);
if HwndHelp<>0 then // 如存在则关闭
SendMessage(HwndHelp,WM_CLOSE,0,0);
i:=ShellExecute(handle, 'open',Pchar( 'Help.chm'),nil, nil, sw_ShowNormal);
if i<>42 then
Showmessage('对不起,帮助文件已经损坏!');
end;
ShellExecute(Handle, 'open', Pchar('c:\xx.chm'), nil, nil, SW_SHOWNORMAL);