在网页看到过纯理论的文章,在摸索了N久之后,终于用VB6实现。
1、找一个olelib.tlb的文件,网上有下。
2、新建VB工程,加入webbrowser控件,引用上面的文件。
Option Explicit
Implements IDocHostUIHandler
Dim mshow As Boolean
Dim mICustomDoc As ICustomDoc
Private oTest As cTest
Private Sub Form_Load()
WebBrowser1.Navigate2 App.Path & "/test.html"
End Sub
Private Sub IDocHostUIHandler_EnableModeless(ByVal fEnable As olelib.BOOL)
'IDocHostUIHandler.EnableModeless fEnable
End Sub
Private Function IDocHostUIHandler_FilterDataObject(ByVal pDO As olelib.IDataObject) As olelib.IDataObject
'Set IDocHostUIHandler_FilterDataObject = IDocHostUIHandler.FilterDataObject(pDO)
End Function
Private Function IDocHostUIHandler_GetDropTarget(ByVal pDropTarget As olelib.IDropTarget) As olelib.IDropTarget
'Set IDocHostUIHandler_GetDropTarget = IDocHostUIHandler.GetDropTarget(pDropTarget)
End Function
Private Function IDocHostUIHandler_GetExternal() As Object
Set oTest = New cTest
Set IDocHostUIHandler_GetExternal = oTest ' IDocHostUIHandler.GetExternal
End Function
Private Sub IDocHostUIHandler_GetHostInfo(pInfo As olelib.DOCHOSTUIINFO)
'IDocHostUIHandler.GetHostInfo pInfo
End Sub
Private Sub IDocHostUIHandler_GetOptionKeyPath(pOLESTRchKey As Long,ByVal dw As Long)
'IDocHostUIHandler.GetOptionKeyPath pOLESTRchKey,dw
End Sub
Private Sub IDocHostUIHandler_HideUI()
'IDocHostUIHandler.HideUI
End Sub
Private Sub IDocHostUIHandler_OnDocWindowActivate(ByVal fActivate As olelib.BOOL)
'IDocHostUIHandler.OnDocWindowActivate fActivate
End Sub
Private Sub IDocHostUIHandler_OnFrameWindowActivate(ByVal fActivate As olelib.BOOL)
'IDocHostUIHandler.OnFrameWindowActivate fActivate
End Sub
Private Sub IDocHostUIHandler_ResizeBorder(prcBorder As olelib.RECT,ByVal pUIWindow As olelib.IOleInPlaceUIWindow,ByVal fRameWindow As olelib.BOOL)
'IDocHostUIHandler.ResizeBorder prcBorder,pUIWindow,fRameWindow
End Sub
Private Sub IDocHostUIHandler_ShowContextMenu(ByVal dwContext As olelib.ContextMenuTarget,pPOINT As olelib.Point,ByVal pCommandTarget As olelib.IOleCommandTarget,ByVal HTMLTagElement As Object)
'IDocHostUIHandler.ShowContextMenu dwContext,pPOINT,pCommandTarget,HTMLTagElement
End Sub
Private Sub IDocHostUIHandler_ShowUI(ByVal dwID As Long,ByVal pActiveObject As olelib.IOleInPlaceActiveObject,ByVal pFrame As olelib.IOleInPlaceFrame,ByVal pDoc As olelib.IOleInPlaceUIWindow)
'IDocHostUIHandler.ShowUI dwID,pActiveObject,pFrame,pDoc
End Sub
Private Sub IDocHostUIHandler_TranslateAccelerator(lpMsg As olelib.MSG,pguidCmdGroup As olelib.UUID,ByVal nCmdID As Long)
'IDocHostUIHandler.TranslateAccelerator lpmsg,pguidCmdGroup,nCmdID
End Sub
Private Function IDocHostUIHandler_TranslateUrl(ByVal dwTranslate As Long,ByVal pchURLIn As Long) As Long
' IDocHostUIHandler_TranslateUrl = IDocHostUIHandler.TranslateUrl(dwTranslate,pchURLIn)
End Function
Private Sub IDocHostUIHandler_UpdateUI()
'IDocHostUIHandler.UpdateUI
End Sub
Private Sub Form_Resize()
On Error Resume Next
WebBrowser1.Top = 0
WebBrowser1.Left = 0
WebBrowser1.Width = Me.ScaleWidth
WebBrowser1.Height = Me.ScaleHeight
End Sub
Private Sub Webbrowser1_DownloadComplete()
Set mICustomDoc = Me.WebBrowser1.Document
mICustomDoc.SetUIHandler Me
End Sub
5、加一类,cTest
Option Explicit
Public Function HelloWorld()
MsgBox "Hello World. ",vbInformation,"Message"
End Function
6、同目录一个HTML文件,test.html
<html>
<head>
<title>Vansoft</title>
<script language="javaScript">
function Test()
{
external.HelloWorld();
}
</script>
</head>
<body>
<p>this is <a href="javaScript:Test()">test</a> function</p>
</body>
</html>
OK了,窗体打开时显示网页,单击test,弹出hello world对话框。
仅仅是最简单的示例。