博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ocx控件查看_控制HTML帮助查看器OCX控件
阅读量:2531 次
发布时间:2019-05-11

本文共 13422 字,大约阅读时间需要 44 分钟。

ocx控件查看

对用户的帮助 (Help for the user)

Years ago, a printed manual and a comprehensive on-line help - a help file - were mandatory parts of any decent application for commercial usage.

几年前,印刷手册和全面的在线帮助(帮助文件)是任何正当商业应用的必不可少的部分。

Both are rare these days, as applications are expected to be self-explanatory, or the user is directed to obtain help from an online forum or a downloaded PDF.

由于期望应用程序是不言自明的,或者用户被引导从在线论坛或下载的PDF中寻求帮助,因此这两种情况如今都很少见。

Still, a good and context-sensitive help file can be the optimum help for the user, and - if present - the fastest method, ready at hand as it is; no time wasted on browsing and sorting good info from all the irrelevant stuff.

但是,良好的上下文相关帮助文件仍然可以为用户提供最佳帮助,并且如果存在的话,它也是最快的方法,可以直接使用。 不要浪费时间浏览和整理所有不相关内容中的良好信息。

Writing a good help file is an art in itself, which not will be touched here. However, if this is what you need, a good place to start is one of these sites (examples only, no affiliation to these): or , which is also the origin of the sample help file used here (see Download later).

编写一个好的帮助文件本身就是一门艺术,这里不再赘述。 但是,如果这是您需要的,那么一个不错的起点是以下站点之一(仅作为示例,与这些站点无关): 或 ,这也是此处使用的示例帮助文件的来源(请参阅下载)后来)。

查看帮助文件 (Viewing the help file)

The help file itself is of a special compressed format - with the extension .chm - which requires a special viewer. That viewer is often the native viewer of Windows, Microsoft HTML Help Viewer, an OCX control.

帮助文件本身具有特殊的压缩格式-带有扩展名。 chm-需要特殊的查看器。 该查看器通常是Windows的本机查看器,即Microsoft HTML Help Viewer (一种OCX控件)。

From VBA, the viewer is opened via one of two API calls to the control, hhctrl.ocx, which normally is present on all newer Windows installs. This would be very simple, if not for these traps:

从VBA中,通过对控件hhctrl.ocx的两个API调用之一打开查看器,该控件通常出现在所有较新的Windows安装中。 如果没有这些陷阱,这将非常简单:

  • If a context ID is passed for no purpose, the application may crash

    如果无缘无故地传递了上下文ID,则应用程序可能会崩溃
  • If the help viewer is not closed before exiting the application, the application may crash

    如果在退出应用程序之前没有关闭帮助查看器,则该应用程序可能会崩溃
  • the help viewer will not by itself move to the Contents tab when a context is opened

    打开上下文时,帮助查看器本身不会移动到“目录”选项卡

The documentation on the control is sparse and rudimentary, almost nil, and these traps are to be found by trial and error or to be extracted bit by bit at various fora and blog posts. However, once taken care of, the usage is straight-forward.

控件上的文档很少且基本,几乎没有,这些陷阱将通过反复试验来发现,或者在各种论坛和博客文章中一点一点地提取出来。 但是,一旦解决,用法就很简单。

API调用 (The API calls)

These days, API calls should be declared to work in both 32- and 64-bit VBA environments, as more and more corporations move to the 64-bit version of Microsoft Office.

如今,随着越来越多的公司迁移到64位版本的Microsoft Office,应该声明API调用可以在32位和64位VBA环境中工作。

This is done in the header of the code module, and also a special UDT (User Defined Type) for the search function is declared:

这是在代码模块的头中完成的,还声明了用于搜索功能的特殊UDT(用户定义类型):

' API calls for the HTML Help Viewer control.' Sample help file for download:' http://www.innovasys.com/download/examplechmzipfile?ZipFile=%2FStatic%2FHS%2FSamples%2FHelpStudioSample_CHM.zip' Open a compiled HTML help file (.chm) or close all opened help files.#If VBA7 Then    Private Declare PtrSafe Function HTMLHelpShowContents Lib "hhctrl.ocx" Alias "HtmlHelpA" ( _        ByVal hwnd As LongPtr, _        ByVal lpHelpFile As String, _        ByVal wCommand As Long, _        ByVal dwData As Long) _        As Long#Else    Private Declare Function HTMLHelpShowContents Lib "hhctrl.ocx" Alias "HtmlHelpA" ( _        ByVal hwnd As Long, _        ByVal lpHelpFile As String, _        ByVal wCommand As Long, _        ByVal dwData As Long) _        As Long#End If' Open a compiled HTML help file (.chm) with the Search tab active.#If VBA7 Then    Private Declare PtrSafe Function HTMLHelpShowSearch Lib "hhctrl.ocx" Alias "HtmlHelpA" ( _        ByVal hwnd As LongPtr, _        ByVal lpHelpFile As String, _        ByVal wCommand As Long, _        ByRef dwData As HhFtsQuery) _        As Long#Else    Private Declare Function HTMLHelpShowSearch Lib "hhctrl.ocx" Alias "HtmlHelpA" ( _        ByVal hwnd As Long, _        ByVal lpHelpFile As String, _        ByVal wCommand As Long, _        ByRef dwData As HhFtsQuery) _        As Long#End If' User Defined Types.'' UDT for HTMLHelpShowSearch.Private Type HhFtsQuery    cbStruct          As Long       ' Size of structure in bytes.    fUniCodeStrings   As Long       ' TRUE if all strings are unicode.    pszSearchQuery    As String     ' String containing the search query.    iProximity        As Long       ' Word proximity.    fStemmedSearch    As Long       ' TRUE for StemmedSearch only.    fTitleOnly        As Long       ' TRUE for Title search only.    fExecute          As Long       ' TRUE to initiate the search.    pszWindow         As String     ' Window to display in.End Type

The interesting parameter is wCommand which controls what the viewer control does and how it appears. It must have one of a few special values which - for ease of use and to prevent errors - has been collected in an Enum:

有趣的参数是wCommand ,它控制查看器控件的功能以及它的显示方式。 它必须具有几个特殊值之一,为了便于使用和防止错误,该值已在Enum中收集:

' Commands to control the appearance of the viewer when launched.Public Enum hhCommand    ' Select the last opened tab.    DisplayTopic = &H0    ' Select the Contents tab.    DisplayContents = &H1    ' Select the Index tab.    DisplayIndex = &H2    ' Select the Search tab.    DisplaySearch = &H3    ' Select the Contents tab and open a topic by its index.    OpenContext = &HF    ' Close all windows opened by the viewer.    CloseAll = &H12End Enum

Now, everything is prepared.

现在,一切准备就绪。

操作帮助查看器控件 (Operating the Help Viewer control)

One function does it all, controlled by the key parameter: Command.

一个功能可以完成所有工作,并由关键参数Command来控制

The next parameters are HelpFile to specify to help file to open. The last parameter, Context, is used to pass the  Context ID of a specific help page that should be opened, ready to be read.

下一个参数是HelpFile,用于指定要打开的帮助文件。 最后一个参数Context用来传递应打开的特定帮助页面的Context ID,以供读取。

Some typical examples for the usage are listed in the header of the function, HelpControl:

函数的标题HelpControl中列出了一些用法的典型示例:

' Displays a compressed HTML help file using the HTML Help File Viewer.' Alternatively, closes all windows of the viewer that may be open.'' Returns True if the operation was successful.' Returns False if the operation couldn't be carried out, for example' if one or more parameter is invalid, or - for closing windows - if' no windows were open.'' Example, display the Contents tab:'   Success = HelpControl(DisplayContents, "d:\path\HelpStudioSample.chm")' Example, display the Contents tab and open the page with context id 7:'   Success = HelpControl(OpenContext, "d:\path\HelpStudioSample.chm", 7)' Example, display the Index tab:'   Success = HelpControl(DisplayIndex, "d:\path\HelpStudioSample.chm")' Example, display the Search tab:'   Success = HelpControl(DisplaySearch, "d:\path\HelpStudioSample.chm")' Example, display the last opened tab and the main page:'   Success = HelpControl(DisplayTopic, "d:\path\HelpStudioSample.chm")' Example, close all opened Help Viewer windows:'   Success = HelpControl(CloseAll)'' Note:'   If the help file is stored on a networked folder and will open, but'   will not display the context pages, move the file to a local folder.'' 2018-04-26. Gustav Brock, Cactus Data ApS, CPH.'Public Function HelpControl( _    ByVal Command As hhCommand, _    Optional ByVal HelpFile As String, _    Optional ByVal Context As Long) _    As Boolean       ' Use default owner handle (Application).    Const OwnerHandle   As Long = 0    ' Neutral values.    Const NoHandle      As Long = 0    Const NoTopic       As Long = 0    Const NoFile        As String = ""    ' Handle of the current Help Viewer window (if any).    Static OpenHandle   As Long       Dim SearchQuery     As HhFtsQuery    Dim Handle          As Long       ' Manage the Help Viewer.    Select Case Command        ' Open the Help Viewer and display a tab.        Case hhCommand.DisplayTopic, _            hhCommand.DisplayContents, _            hhCommand.DisplayIndex            Handle = HTMLHelpShowContents(OwnerHandle, HelpFile, Command, NoTopic)               ' Open the Help Viewer and display the topic having the ID of Context.        Case hhCommand.OpenContext            ' Reset displayed tab to Contents.            Handle = HTMLHelpShowContents(OwnerHandle, HelpFile, hhCommand.DisplayContents, NoTopic)            ' Open help context page.            Handle = HTMLHelpShowContents(OwnerHandle, HelpFile, Command, Context)               ' Open the Help Viewer and display the Search tab.        Case hhCommand.DisplaySearch            SearchQuery.cbStruct = Len(SearchQuery)            Handle = HTMLHelpShowSearch(OwnerHandle, HelpFile, Command, SearchQuery)               ' Close all windows opened by the Help Viewer.        Case hhCommand.CloseAll            If OpenHandle = NoHandle Then                ' Don't waste time on closing non-existing windows.            Else                ' A help file has been opened.                ' Set Handle to return success.                Handle = OpenHandle                ' Make sure, all help windows are closed, and reset OpenHandle.                OpenHandle = HTMLHelpShowContents(OwnerHandle, NoFile, Command, NoTopic)            End If                   Case Else            ' Ignore.    End Select       If Command <> hhCommand.CloseAll Then        If Handle <> NoHandle Then            ' Store the handle of the window.            OpenHandle = Handle        End If    End If       ' Return True if success.    HelpControl = (Handle <> NoHandle)End Function

The in-line comments explain the details of the operation but pay attention to two details.

在线注释解释了操作的细节,但注意两个细节。

First, the command OpenContext causes two calls of the API. First, it sets the viewer to display tab Contents, then it opens the specific context page. This is to avoid that the context page is opened while, say, the Search tab is displayed, which would confuse the user.

首先 ,命令OpenContext导致对API的两次调用。 首先,它将查看器设置为显示“目录”选项卡,然后打开特定的上下文页面。 这是为了避免在显示“搜索”选项卡时打开上下文页面,这会使用户感到困惑。

Second, the function maintains hold of the handle (the "ID") of the help window opened with the static variable OpenHandle

其次 ,该函数保持使用静态变量OpenHandle打开的帮助窗口的句柄 (“ ID”)的保持

This will be 0 (zero) if no help window has been opened. If that is the case, there is no need to call the close command which, otherwise, would take about half second. Not much but, in some cases, that is exactly the level of delay, that could make the application appear "slow" to the user.

如果没有打开帮助窗口,则该值为0(零)。 如果是这种情况,则无需调用close命令,否则将花费大约半秒钟。 数量不多,但在某些情况下,恰恰是延迟级别,可能会使应用程序对用户显得“缓慢”。

访问示例 (An Access example)

In the download, a small Access application which demonstrates the use of the function in a form, is included.

在下载中,包含一个小型Access应用程序,该应用程序以表格形式演示了该功能的用法。

It contains an option group to set the action, a button to carry the command out, and a button to close the Help Viewer window:

它包含一个用于设置操作的选项组,一个用于执行命令的按钮以及一个用于关闭“帮助查看器”窗口的按钮:

The full code is held at a minimum:

完整的代码最少保留:

Private Sub CloseHelp_Click()    HelpControl CloseAllEnd SubPrivate Sub Form_Close()    ' Make sure the Help Viewer is closed, or Access may crash when closing.    HelpControl CloseAllEnd SubPrivate Sub OpenHelp_Click()    ' Name of help file.    Const FileName  As String = "HelpStudioSample.chm"    Dim Command     As hhCommand    Dim Context     As Long    Dim HelpFile    As String       ' Position the help file in the folder of the application.    HelpFile = CurrentProject.Path & "\" & FileName       ' Open the Help Viewer.    Select Case Me!HelpGroup.Value        Case 1            Command = DisplayContents        Case 2            Command = DisplayIndex        Case 3            Command = DisplaySearch        Case 4            Command = OpenContext            Context = Val(Nz(Me!ID.Value))    End Select    HelpControl Command, HelpFile, Context   End Sub

Note, in the Close event, the call to close the Help Viewer window, should it have been left open. As mentioned earlier, an open Help Viewer window may cause the application to crash when closing.

请注意,在关闭事件中,如果关闭了“帮助查看器”窗口,则该调用应保持打开状态。 如前所述,打开的“帮助查看器”窗口可能会导致应用程序在关闭时崩溃。

结论 (Conclusion)

Full code and an Access demo application as presented here will provide a good starting point for implementing an on-line help function for an application using either 32- or 64-bit VBA.

此处提供的完整代码和Access演示应用程序将为使用32位或64位VBA的应用程序实现在线帮助功能提供一个良好的起点。

Another example of usage can be found in another of my articles:

在我的另一篇文章中可以找到用法的另一个示例:

下载 (Download)

The full and current code is available for download at GitHub:

完整的当前代码可从GitHub下载:

Also, code and a demo application is here:

另外,代码和演示应用程序在这里:

Sample help file:

样本帮助文件:

I hope you found this article useful. You are encouraged to ask questions, report any bugs or make any other comments about it below.

希望本文对您有所帮助。 鼓励您在下面提出问题,报告任何错误或对此作出任何其他评论。

Note: If you need further "Support" about this topic, please consider using the feature of Experts Exchange. I monitor questions asked and would be pleased to provide any additional support required in questions asked in this manner, along with other EE experts.

注意 :如果您需要有关此主题的更多“支持”,请考虑使用Experts Exchange 功能。 我会监督提出的问题,并很高兴与其他电子工程师一起为以这种方式提出的问题提供所需的任何其他支持。

Please do not forget to press the "Thumbs Up" button if you think this article was helpful and valuable for EE members.

如果您认为本文对EE成员有用且有价值,请不要忘记按下“竖起大拇指”按钮。

翻译自:

ocx控件查看

转载地址:http://fyhzd.baihongyu.com/

你可能感兴趣的文章
考研经验交流
查看>>
手游助手应用源码项目
查看>>
职场心得笔记
查看>>
Android context(Application/Activity)与内存泄露
查看>>
mysql 行转列
查看>>
jquery easyui 经验
查看>>
Kafka官方文档翻译——设计
查看>>
本地推送
查看>>
免费的在线文档翻译神器
查看>>
RabbitMQ --- Publish/Subscribe(发布/订阅)
查看>>
细思极恐-你真的会写java吗
查看>>
Jquery 多选下拉列表插件jquery multiselect之如何去掉默认选中项1
查看>>
安装apache Unable to correct problems, you have held broken packages
查看>>
搭建Sphinx环境及文档
查看>>
实验随笔
查看>>
Weapsy分析终
查看>>
8个免费实用的C++GUI库(转载)
查看>>
d010: 分离自然数
查看>>
软件工程的实践项目的自我目标
查看>>
Java8 in action(1) 通过行为参数化传递代码--lambda代替策略模式
查看>>