调用chm文件,HtmlHelp函数
最近在改的defects, 有一个调用chm的。被迫看了看网上的文档,发现不太全,决定自己写一个。
一般的人都喜欢用ShellExecute来调用chm文件。ShellExecute函数可以启动各种类型的文件,如果chm 文件比较详细,topic比较多,需要在不同的地方打开不同的topic,那么ShellExecute就达不到要求了。
HtmlHelp函数的使用:
第一步:安装htmlhelp.exe,你可以从微软的网站上下载:http://www.microsoft.com/downloads/details.aspx?FamilyID=00535334-c8a6-452f-9aa0-d597d16580cc&displaylang=en
第二步:把htmlhelp.lib引入进来,并且 #include<htmlhelp.h>
第三步:现在可以用了。
HtmlHelp(NULL, cstr1, HH_DISPLAY_TOPIC, (DWORD)cstr2.GetBuffer());
第一个参数:是父窗口句柄。
第二个参数cstr1: 是你chm的名字,比如C:/sample.chm,
第三个参数HH_DISPLAY_TOPIC:是你显示的方式,目前的传入的HH_DISPLAY_TOPIC是说cstr2是topic。
第四个参数cstr2: 是根据第3个参数不同传入不同的类型的。
具体可以参考MSDN,我会在文章最后附上MSDN上面的解释。
目前就有一个问题了,你说你也不知道这个chm里面的topic是什么。
把chm的文件后缀改成 .zip。 解压以后发现一个ContextId*.h 文件。自己进去找。
介绍一个例子,在下面的网址,这个不但是一个例子,还是一个小工具可以查看chm文件里面的Context id 和 topic。 值得一看,但是他的输入路径有点问题,自己改一下就能用了。
http://www.codeproject.com/KB/winhelp/htmlhelp.aspx
About the HTML Help API Function
The HTML Help API has one function that displays a help window. Using the API commands, you can specify which topic to display in the help window, whether the help window is a three-pane Help Viewer or a pop-up window, and whether the HTML topic file should be accessed via a context ID, an HTML Help URL, or a Keyword link (KLink) lookup.
HTML Help API syntax
HWND HtmlHelp( HWND hwndCaller, LPCSTR pszFile, UINT uCommand, DWORD dwData) ;
Parameters
-
hwndCaller
-
Specifies the handle (
hwnd) of the window calling
HtmlHelp(). The help window is owned by this window.
When the help window is closed, HtmlHelp() will return focus to the owner unless the owner is the desktop. If hwndCaller is the desktop, then the operating system determines where focus is returned.
In addition, if HtmlHelp() sends any notification messages from the help window, they are sent to hwndCaller as long as you have enabled notification message tracking in the help window definition.
pszFile
-
Depending on the
uCommand value, specifies the
file path to either a compiled help (.chm) file, or a topic file within a specified help file.
A window type name can also be specified, preceded with a greater-than (>) character.
If the specified command does not require a file, this value may be NULL.
uCommand
- Specifies the command to complete. dwData
- Specifies any data that may be required, based on the value of the uCommand parameter.
Return Value
Depending on the specified uCommand and the result, HtmlHelp() returns one or both of the following:
- The handle (hwnd) of the help window.
- NULL. In some cases, NULL indicates failure; in other cases, NULL indicates that the help window has not yet been created.
Example
The following example calls the HH_DISPLAY_TOPIC command to open the help file named Help.chm and display its default topic in the help window named Mainwin. Generally, the help window specified in this command is a standard HTML Help Viewer.
HWND hwnd = HtmlHelp( GetDesktopWindow(), "c://Help.chm::/Intro.htm>Mainwin", HH_DISPLAY_TOPIC, NULL) ;
Note When using the HTML Help API, set the stack size of the hosting executable to at least 100k. If the defined stack size is too small, then the thread created to run HTML Help will also be created with this stack size, and failure could result. Optionally, you can remove /STACK from the link command line, and remove any STACK setting in the executable's DEF file (default stack size is 1MB in this case). You can also you can set the stack size using the /Fnumber compiler command (the compiler will pass this to the linker as /STACK).