Site home page Previous Contents

up Microsoft HTML Help in Visual Basic

VB 5 ...

1. Some knowledge about Microsoft HTML Help itself.

2. Create separate module for this code:

Private Const HH_DISPLAY_TOPIC As Long = &H0
Private Const HH_HELP_FINDER As Long = &H0
Private Const HH_SET_WIN_TYPE As Long = &H4
Private Const HH_GET_WIN_TYPE As Long = &H5
Private Const HH_GET_WIN_HANDLE As Long = &H6
Private Const HH_SYNC As Long = &H9
Private Const HH_KEYWORD_LOOKUP As Long = &HD
Private Const HH_DISPLAY_TEXT_POPUP As Long = &HE
Private Const HH_HELP_CONTEXT As Long = &HF
Private Const HH_TP_HELP_CONTEXTMENU As Long = &H10
Private Const HH_TP_HELP_WM_HELP As Long = &H11

Private Declare Function HtmlHelp Lib "hhctrl.ocx" Alias "HtmlHelpA" (ByVal hwndCaller As Long, ByVal pszFile As String, ByVal uCommand As Long, ByVal dwData As Any) As Long

' Set value of this variable in your initializing code
Global GLO_HTMLHelpFileName As String


' this function searches for HelpContextID in active form and control
' and then calls HTML Help
Public Function ContextHelp() As Boolean
Dim frm1 As Form
Dim ctl1 As Control
Dim lngContextID As Long

  On Error GoTo ErrProc
  If GLO_HTMLHelpFileName = "" Then GoTo ExitProc

  On Error Resume Next
  Err.Clear
  Set frm1 = Screen.ActiveForm
  If Err <> 0 Or frm1 Is Nothing Then Set frm1 = frmMDI
  Set ctl1 = frm1.ActiveControl
  If Err <> 0 Then Set ctl1 = Nothing
  If Not (ctl1 Is Nothing) Then
    lngContextID = ctl1.HelpContextID
  End If
  If lngContextID = 0 Then
    lngContextID = frm1.HelpContextID
  End If
  If lngContextID = 0 Then
    Call HelpTopicsDialog
  Else
    mhWnd = HtmlHelp(frm1.hwnd, GLO_HTMLHelpFileName, HH_HELP_CONTEXT, lngContextID)
  End If
  ContextHelp = True

ExitProc:
  On Error Resume Next
  Set frm1 = Nothing
  Set ctl1 = Nothing
Exit Function

ErrProc:
  If MsgBox(Err.Description & vbCrLf & "Continue?", vbYesNo + vbQuestion + vbDefaultButton2, "ContextHelp") = vbYes Then
    Resume Next
  Else
    Resume ExitProc
  End If
End Function

' Display Help Topics Dialog
Public Function HelpTopicsDialog() As Boolean

  On Error GoTo ErrProc
  If GLO_HTMLHelpFileName = "" Then GoTo ExitProc

  On Error Resume Next
  mhWnd = HtmlHelp(frmMDI.hwnd, GLO_HTMLHelpFileName, HH_DISPLAY_TOPIC, CLng(0))
  HelpTopicsDialog = True

ExitProc:
  On Error Resume Next
  Exit Function

ErrProc:
  MsgBox "Error #" & Err.Number & " - " & Err.Description, vbExclamation
End Function

3. Set KeyPreview property of every form to True and in the KeyDown event procedure of every form insert the code:

If KeyCode=vbKeyF1 and Shift = 0 Then ContextHelp()


Author: Yuri Volkov
Last changed: 2002-11-12

1