利用IDocHostUIHandler接口屏蔽WebBrowser的弹出菜单

       简单说两句:大致就是实现IDocHostUIHandler接口,挂接IDocHostUIHandler接口可以利用ICustomDoc接口或IOleObject接口实现,这里演示了利用ICustomDoc接口进行挂接。由于只是想屏蔽webbroswer的弹出菜单,所以只在IDocHostUIHandler的ShowContextMenu方法上做点文章就够了,IDocHostUIHandler的其它的方法不必进行处理,但是你必须实现它们,哪怕你只写上一些注释,好了,下面看代码:
'窗体上一个按钮,一个tetbox,一个checkbox,一个WebBrowser
'这里我依旧引用Edanmo's OLE interfaces & functions v1.81(对应文件olelib.tlb)
Option Explicit
Implements IDocHostUIHandler
Dim mshow As Boolean
Dim mICustomDoc As ICustomDoc

Private Sub Check1_Click()
    mshow = Check1.Value
End Sub

Private Sub Command1_Click()
    Me.WebBrowser1.Navigate Text1.Text
End Sub

Private Sub Form_Load()
    Me.WebBrowser1.Navigate "about:blank"
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 IDocHostUIHandler_GetExternal = 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)
    If mshow = True Then
        IDocHostUIHandler.ShowContextMenu dwContext, pPOINT, pCommandTarget, HTMLTagElement
    End If
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 WebBrowser1_DownloadComplete()
    Set mICustomDoc = Me.WebBrowser1.Document
    mICustomDoc.SetUIHandler Me
End Sub

        最后,需要说明的是,这个程序可能有一些问题,以至于在vb的IDE中它不能很好的工作(至于是什么原因,现在没时间研究了,以后再说吧,有知道的,欢迎你告诉我),不过,编译成exe文件后,它还是可以实现我们的目的的。

你可能感兴趣的:(WebBrowser)