Delphi程序與Chm幫助關(guān)聯(lián)的簡單實(shí)現(xiàn)
Chm格式的幫助是從windows98以后才出現(xiàn)的新的格式,與.hlp格式相比,具有更簡單的編輯方式、更豐富的畫面。它是通過Chm制作工具對網(wǎng)頁文件進(jìn)行編譯得到的,所以理論上你可以把幫助文件做的和網(wǎng)頁一樣漂亮。
最簡單的制作方法:先用FronPage制作幫助文件,然后用HTML Help Workshop編譯就可以得到*.chm的幫助文件了。HTML Help Workshop可以到微軟的網(wǎng)站上去下載。
應(yīng)用程序中的幫助可分為上下文關(guān)聯(lián)和非關(guān)聯(lián)兩種。上下文關(guān)聯(lián),是指用戶按F1鍵后,出現(xiàn)與當(dāng)前焦點(diǎn)對象(如窗體、文本框、下拉列表框)相關(guān)的幫助畫面;不同對象,出現(xiàn)的幫助不同。非關(guān)聯(lián)幫助,是指在程序任何位置按F1鍵后,出現(xiàn)同一幫助畫面。下面就這兩種方式,談?wù)勗贒elphi中的簡單實(shí)現(xiàn)方法。
一、非關(guān)聯(lián)的chm幫助
在Delphi中,你可以通過ShellExecute函數(shù)直接調(diào)用chm幫助文件,具體如下:
uses shellapi
.......
var HWndHelp:Hwnd;
i:integer;
begin
//檢查幫助窗口是否已經(jīng)存在
HWndHelp:=FindWindow(nil,conHelpTitle);
if HwndHelp<>0 then // 如存在則關(guān)閉
SendMessage(HwndHelp,WM_CLOSE,0,0);
i:=ShellExecute(handle, 'open',Pchar(strCurExePath+'/help.chm''),nil, nil, sw_ShowNormal);
if i<>42 then
Showmessage(' help.chm 幫助文件損壞!');
end;
二、上下文關(guān)聯(lián)的chm幫助
在Delphi中實(shí)現(xiàn)上下文關(guān)聯(lián)的chm幫助,可以調(diào)用Windows系統(tǒng)目錄System32下的HHCTRL.OCX控件中的HtmlHelpA函數(shù)實(shí)現(xiàn)。 需要以下幾個步驟:
1 設(shè)置相關(guān)控件的HelpContext屬性。
例,主窗體frmMain::10100 ,其中的文本框 edtInput:10101
對話框dlgReport:10200 ,其中的組合列表框 cbReportEdit:10201
2 聲明HtmlHelpA函數(shù)
function HtmlHelpA (hwndcaller:Longint; lpHelpFile:string; wCommand:Longint;dwData:string): HWND;stdcall; external 'hhctrl.ocx'
3 F1按鍵響應(yīng)
//公用函數(shù)ShowChmHelp顯示不同幫助畫面。
PRocedure ShowChmHelp(sTopic:string);
var i:integer;
begin
i:=HtmlHelpA(
application.Handle,Pchar(ExePath+'/help.chm’),HH_DISPLAY_TOPIC,sTopic);
if i=0 then
begin
Showmessage(' help.chm 幫助文件損壞!');
exit;
end;
end;
….
function TfrmMain.FormHelp(Command:
Word; Data: Integer; var CallHelp: Boolean): Boolean;
begin
case Data of
10100: ShowChmHelp(frmMain.htm);
10101: ShowChmHelp('edtInput.htm');
…
else ShowChmHelp(default.htm');
end;
end;
function TdlgReport.FormHelp(Command: Word; Data: Integer; var CallHelp: Boolean): Boolean;
begin
case Data of
10200: ShowChmHelp('dlgReport.htm');
10201: ShowChmHelp(cbReportEdit.htm');
…
else ShowChmHelp(default.htm');
end;
end;