VB6实现界面与功能代码分离

在网页看到过纯理论的文章,在摸索了N久之后,终于用VB6实现。

1、找一个olelib.tlb的文件,网上有下。

2、新建VB工程,加入webbrowser控件,引用上面的文件。

3、FORM1上加上WebBrowser1。切换至代码。

4、窗体代码(PS:代码我一律不解释。)

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对话框。

仅仅是最简单的示例。

这样做的好处有几个,网上的那篇理论文章也说了,有兴趣的自己去搜索吧。

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...