#include once "window9.bi"

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

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

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

Dim Shared As THelp g_tHelp

Function OpenHelp(ByRef szPathHelp As USTRING, ByRef szTopic As USTRING, byval iParam As Integer) As HWND Export
	If g_tHelp.bFlag = FALSE Then
		g_tHelp.bFlag = TRUE
		g_tHelp.Memdll=DylibLoad("hhctrl.ocx")
		If g_tHelp.Memdll=0 Then
			Print "Error Load hhctrl.ocx"
			Return 0
		End If
		#ifdef UNICODE
			g_tHelp.HtmlHelp=DylibSymbol(g_tHelp.Memdll, "HtmlHelpW")
		#else	
			g_tHelp.HtmlHelp=DylibSymbol(g_tHelp.Memdll, "HtmlHelpA")
		#EndIf		
		If g_tHelp.HtmlHelp = 0 Then
			Print "Error Get HtmlHelp"
			Return 0
		End If
	End If
	Dim HHAKLINK As HH_AKLINK
	HHAKLINK.cbStruct = SizeOf(HH_AKLINK)
	HHAKLINK.fIndexOnFail = TRUE
	#ifdef UNICODE
		HHAKLINK.pszKeywords = *szTopic
		Select Case iparam
			Case 1
				Return Cast(HWND,g_tHelp.HtmlHelp(0,*szPathHelp,HH_KEYWORD_LOOKUP,Cast(integer,@HHAKLINK)))
			Case 2
				g_tHelp.HtmlHelp(0,*szPathHelp,HH_DISPLAY_TOC,Cast(integer,*szTopic))
				Return Cast(HWND,g_tHelp.HtmlHelp(0,*szPathHelp,HH_KEYWORD_LOOKUP,Cast(integer,@HHAKLINK)))				
			Case 3
				g_tHelp.HtmlHelp(0,*szPathHelp,HH_DISPLAY_INDEX,Cast(integer,*szTopic))
				Return Cast(HWND,g_tHelp.HtmlHelp(0,*szPathHelp,HH_KEYWORD_LOOKUP,Cast(integer,@HHAKLINK)))
			Case Else
				Return Cast(HWND, g_tHelp.HtmlHelp(0,*szPathHelp,HH_DISPLAY_TOPIC,Cast(integer,*szTopic)))
		End Select  	 	
	#else	
		HHAKLINK.pszKeywords = StrPtr(szTopic)	
		Select Case iparam
			Case 1
				Return Cast(HWND,g_tHelp.HtmlHelp(0,szPathHelp,HH_KEYWORD_LOOKUP,Cast(integer,@HHAKLINK)))
			Case 2
				g_tHelp.HtmlHelp(0,szPathHelp,HH_DISPLAY_TOC,Cast(integer,StrPtr(szTopic)))
				Return Cast(HWND,g_tHelp.HtmlHelp(0,szPathHelp,HH_KEYWORD_LOOKUP,Cast(integer,@HHAKLINK)))				
			Case 3
				g_tHelp.HtmlHelp(0,szPathHelp,HH_DISPLAY_INDEX,Cast(integer,StrPtr(szTopic)))
				Return Cast(HWND,g_tHelp.HtmlHelp(0,szPathHelp,HH_KEYWORD_LOOKUP,Cast(integer,@HHAKLINK)))
			Case Else
				Return Cast(HWND, g_tHelp.HtmlHelp(0,szPathHelp,HH_DISPLAY_TOPIC,Cast(integer,StrPtr(szTopic))))
		End Select
	#EndIf
End Function

Sub CloseHelp() Export
	g_tHelp.HtmlHelp(0,0,HH_CLOSE_ALL,0)
	Sleep(50)
	DylibFree g_tHelp.Memdll
	g_tHelp.bFlag = FALSE
End Sub






