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 pCommandTarget As olelib.IOleCommandTarget, ByVal pFrame As olelib.IOleInPlaceFrame, ByVal pDoc As olelib.IOleInPlaceUIWindow)
    'IDocHostUIHandler.ShowUI dwID, pActiveObject, pCommandTarget, 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对话框。

仅仅是最简单的示例。

 

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

评论 6 您还未登录,请先 登录 后发表或查看评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页

打赏作者

vansoft

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值