可以使用 CEFSharp 向用​​户显示 URI 吗?

Can the URI be displayed to the user using CEFSharp?

在我的 Windows 表单应用程序 (WinForms) 中,我使用 CEFSharp 打开网页。我想修改右键单击上下文以允许用户显示打开的URL。目前,上下文有"Back"、"Forward"、"Print..."和"View Source"

@amaitland 的评论引起了轰动。这是我的实现。我希望这对其他人有帮助。

当您初始化您的 WinForms.ChromiumWebBrowser 实例时,您将其 属性 MenuHandler 设置为您的 IContextMenuHandler 实例。

    chromeBrowser = New WinForms.ChromiumWebBrowser(uri)
    chromeBrowser.MenuHandler = New Classes.CefBasicMenuHandler()
    Controls.Add(chromeBrowser)

您可以在 IContextMenuHandler 的实现中控制上下文菜单。

Public Class CefBasicMenuHandler
Implements IContextMenuHandler

private const ShowDevTools as Integer = 26501
private const CloseDevTools as Integer = 26502
Private Const CopyUrlAddress as Integer = 26503

Public Sub OnBeforeContextMenu(browserControl As IWebBrowser, browser As IBrowser, frame As IFrame, parameters As IContextMenuParams, model As IMenuModel) Implements IContextMenuHandler.OnBeforeContextMenu
    '//To disable the menu then call clear
    model.Clear()

    '//Add new custom menu items
    model.AddItem(CType(CopyUrlAddress, CefMenuCommand), "Copy URL address")
    model.AddSeparator()
    model.AddItem(CType(ShowDevTools, CefMenuCommand), "Show DevTools")
    model.AddItem(CType(CloseDevTools, CefMenuCommand), "Close DevTools")
    model.AddSeparator()
    model.AddItem(CefMenuCommand.Reload, "Reload")
    model.AddItem(CefMenuCommand.Copy, "Copy")
End Sub

Public Function OnContextMenuCommand(browserControl As IWebBrowser, browser As IBrowser, frame As IFrame, parameters As IContextMenuParams, commandId As CefMenuCommand, eventFlags As CefEventFlags) As Boolean Implements IContextMenuHandler.OnContextMenuCommand
    Dim commandId1 As Integer = CType(commandId, Integer)

    If commandId1 = ShowDevTools Then
        browser.ShowDevTools()
    End If
    If commandId1 = CloseDevTools Then
        browser.CloseDevTools()
    End If
    If commandId1 = CopyUrlAddress Then
        Clipboard.SetText(parameters.PageUrl)
    End If

    Return False
End Function

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

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