In my Windows form application (WinForms), I'm using CEFSharp to open a web page. I want to modify the right-click context to allow the user to display the URL that was opened. Currently, the context has "Back", "Forward", "Print...", and "View Source"
The comment from @amaitland got the ball rolling. Here is my implementation. I hope this helps someone else.
When you initialize your instance of the WinForms.ChromiumWebBrowser, you set it's property MenuHandler to your instance of a IContextMenuHandler.
chromeBrowser = New WinForms.ChromiumWebBrowser(uri)
chromeBrowser.MenuHandler = New Classes.CefBasicMenuHandler()
Controls.Add(chromeBrowser)
Your implementation of an IContextMenuHandler is where you can control the context menu.
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