#Include "window9.bi"

Enum eHelp
	HH_DISPLAY_TOPIC  = 0
	HH_DISPLAY_TOC    = 1
	HH_DISPLAY_INDEX  = 2
	HH_KEYWORD_LOOKUP = 13
	HH_HELP_CONTEXT =   16
	HH_CLOSE_ALL      = 18
End Enum

#Ifndef HH_AKLINK
Type HH_AKLINK
	cbStruct      As Long
	fReserved     As Byte
	pszKeywords   As ZString Ptr
	pszUrl        As ZString Ptr
	pszMsgText    As ZString Ptr
	pszMsgTitle   As ZString Ptr
	pszWindow     As ZString Ptr
	fIndexOnFail  As Byte
End Type
#EndIf

Type THelp
	Memdll As Any Ptr
	HtmlHelp As Function ( ByVal hwndCaller As Long, pszFile As ZString Ptr,_
	ByVal uCommand As Long, ByVal dwData As Long ) As Long
	bFlag As BOOL
End Type

Dim Shared tHelp As THelp

Function OpenHelp(szPathHelp As String ,szTopic As String , iParam As Integer = FB_IGNORE) As HWND Export

	If tHelp.bFlag = FALSE Then

		tHelp.bFlag = TRUE

		tHelp.Memdll=DylibLoad("hhctrl.ocx")

		If tHelp.Memdll=0 Then

			Print "Error Load hhctrl.ocx"

			Return 0

		EndIf

		tHelp.HtmlHelp=DylibSymbol(tHelp.Memdll, "HtmlHelpA")

		If tHelp.HtmlHelp = 0 Then

			Print "Error Get HtmlHelpA"

			Return 0

		EndIf

	EndIf

	Dim HHAKLINK As HH_AKLINK

	HHAKLINK.cbStruct = SizeOf(HH_AKLINK)

	HHAKLINK.pszKeywords = StrPtr(szTopic)

	HHAKLINK.fIndexOnFail = TRUE

	Select Case iparam

		Case 1

			Return Cast(HWND,tHelp.HtmlHelp(0,szPathHelp,HH_KEYWORD_LOOKUP,Cast(Long,@HHAKLINK)))

		Case 2

			tHelp.HtmlHelp(0,szPathHelp,HH_DISPLAY_TOC,Cast(Long,StrPtr(szTopic)))

			Return Cast(HWND,tHelp.HtmlHelp(0,szPathHelp,HH_KEYWORD_LOOKUP,Cast(Long,@HHAKLINK)))

		Case 3

			tHelp.HtmlHelp(0,szPathHelp,HH_DISPLAY_INDEX,Cast(Long,StrPtr(szTopic)))

			Return Cast(HWND,tHelp.HtmlHelp(0,szPathHelp,HH_KEYWORD_LOOKUP,Cast(Long,@HHAKLINK)))
			
		Case Else
			
			Return Cast(HWND, tHelp.HtmlHelp(0,szPathHelp,HH_DISPLAY_TOPIC,Cast(Long,StrPtr(szTopic))))

	End Select

End Function

Sub CloseHelp() Export

	tHelp.HtmlHelp(0,0,HH_CLOSE_ALL,0)

	Sleep(50)

	DylibFree tHelp.Memdll

	tHelp.bFlag = FALSE

End Sub






