ChromiumWebBrowser禁用右键单击上下文菜单VB .Net

时间:2019-10-26 20:27:00

标签: vb.net cefsharp

需要在VB .Net中禁用ChromiumWebBrowser右键单击上下文菜单。

我尝试了官方文档中的许多代码示例,并将代码从C转换为VB,但仍然无法正常工作。

如果可以向我展示一些禁用右键单击并避免在vb .Net中拖放ChromiumWebBrowser的示例代码

类文件

Public Class CustomMenuHandler

Public Sub OnBeforeContextMenu(ByVal browserControl As IWebBrowser, ByVal browser As CefSharp.IBrowser, ByVal frame As IFrame, ByVal parameters As IContextMenuParams, ByVal model As IMenuModel)
    model.Clear()
End Sub

Public Function OnContextMenuCommand(ByVal browserControl As IWebBrowser, ByVal browser As CefSharp.IBrowser, ByVal frame As IFrame, ByVal parameters As IContextMenuParams, ByVal commandId As CefMenuCommand, ByVal eventFlags As CefEventFlags) As Boolean
    Return False
End Function

Public Sub OnContextMenuDismissed(ByVal browserControl As IWebBrowser, ByVal browser As CefSharp.IBrowser, ByVal frame As IFrame)

End Sub

End Class

通话类

browser = New ChromiumWebBrowser("google.com")
browser.MenuHandler = New CustomMenuHandler
panel1.Controls.Add(browser)
  

错误:   附加信息:无法将类型为“ Project.CustomMenuHandler”的对象转换为类型为“ CefSharp.IContextMenuHandler”。

3 个答案:

答案 0 :(得分:3)

这是我在社区中的第一个答案

我搜索相同的内容超过几个小时,然后找到了解决方法

所以您首先要实现IContextMenuHandler

之后,您要在每个函数之后实现IContextMenuHandler

Imports System
Imports CefSharp

Imports System.Windows.Forms

Public Class MyCustomMenuHandler
Implements IContextMenuHandler

Public Sub OnBeforeContextMenu(ByVal browserControl As IWebBrowser, ByVal browser As IBrowser, ByVal frame As IFrame, ByVal parameters As IContextMenuParams, ByVal model As IMenuModel) Implements IContextMenuHandler.OnBeforeContextMenu
    model.Clear()
End Sub

Public Function OnContextMenuCommand(ByVal browserControl As IWebBrowser, ByVal browser As IBrowser, ByVal frame As IFrame, ByVal parameters As IContextMenuParams, ByVal commandId As CefMenuCommand, ByVal eventFlags As CefEventFlags) As Boolean Implements IContextMenuHandler.OnContextMenuCommand
    Return False
End Function


Public Sub OnContextMenuDismissed(ByVal browserControl As IWebBrowser, ByVal browser As IBrowser, ByVal frame As IFrame) Implements IContextMenuHandler.OnContextMenuDismissed
End Sub

Public Function RunContextMenu(ByVal browserControl As IWebBrowser, ByVal browser As IBrowser, ByVal frame As IFrame, ByVal parameters As IContextMenuParams, ByVal model As IMenuModel, ByVal callback As IRunContextMenuCallback) As Boolean Implements IContextMenuHandler.RunContextMenu
    Return False
End Function
End Class

答案 1 :(得分:0)

首先,您的CustomMenuHandler类应实现所需的接口,如下所示:

Public Class CustomMenuHandler implements IContextMenuHandler

这将为您解决问题。如果您正在寻找替代方法,则只需捕获MouseDown事件,检查鼠标中按下的键是否为鼠标右键(MouseButton.Right),然后使用{{1 }};

希望这会有所帮助。

答案 2 :(得分:0)

这是正确的解决方案。用Browser.MenuHandler = New CustomMenuHandler()命名:

Imports System
Imports CefSharp

Imports System.Windows.Forms
Public Class CustomMenuHandler
    Implements CefSharp.IContextMenuHandler
    Public Sub OnBeforeContextMenu(ByVal browserControl As IWebBrowser, ByVal browser As IBrowser, ByVal frame As IFrame, ByVal parameters As IContextMenuParams, ByVal model As IMenuModel) Implements IContextMenuHandler.OnBeforeContextMenu
        model.Clear()
    End Sub
    Public Function OnContextMenuCommand(ByVal browserControl As IWebBrowser, ByVal browser As IBrowser, ByVal frame As IFrame, ByVal parameters As IContextMenuParams, ByVal commandId As CefMenuCommand, ByVal eventFlags As CefEventFlags) As Boolean Implements IContextMenuHandler.OnContextMenuCommand
        Return False
    End Function
    Public Sub OnContextMenuDismissed(ByVal browserControl As IWebBrowser, ByVal browser As IBrowser, ByVal frame As IFrame) Implements IContextMenuHandler.OnContextMenuDismissed
    End Sub

    Public Function RunContextMenu(ByVal browserControl As IWebBrowser, ByVal browser As IBrowser, ByVal frame As IFrame, ByVal parameters As IContextMenuParams, ByVal model As IMenuModel, ByVal callback As IRunContextMenuCallback) As Boolean Implements IContextMenuHandler.RunContextMenu
        Return False
    End Function
End Class