简体   繁体   中英

CefSharp - no copy shortcut menu item

I recently added cefsharp from nuget to a project in visual studio. It's working with browsing and all but I need my user to be able to copy his email address from the browser by right clickong on it clicking on copy shortcut.

But cefsharp is only showing 4 options - back, forward, print, view source. There is no copy shortcut option.

I have not initialized the browser with any settings. Just created a chromiumbrowser item and added it to the controls on the form.

Anyone knows why this is happening? I also tried to add a menu item but can't do that as i keep getting an error - "you must use new keyword". Also there is no copy shortcut method in browser which i can call through code.

Can someone help me and explain how to achieve this? How to add more options to right click and how to right click and copy link address. Or recommend me some other browser component? I have tried awesomium but half of the sites don't load on it.

You can create your own context menu by implementing IContextMenuHandler . You didn't specify if you're using the WinForms or WPF version, but there ample examples on GitHub for both.

WinForms https://github.com/cefsharp/CefSharp/blob/master/CefSharp.WinForms.Example/Handlers/MenuHandler.cs

WPF https://github.com/cefsharp/CefSharp/blob/master/CefSharp.Wpf.Example/Handlers/MenuHandler.cs

For either flavour, you assign your IContextMenuHandler implementation to the MenuHandler property of ChromiumWebBrowser . In this instance, I'm following the GutHub WinForms example and implemented IContextMenuHandler in a class called MenuHandler . Below is an example for WinForms but it can be easily transposed to WPF .

internal class MenuHandler : IContextMenuHandler
{
    private const int Copy = 26503;

    void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
    {
        //Add new custom menu items
        model.AddItem((CefMenuCommand)Copy, "Copy Link Address");
    }

    bool IContextMenuHandler.OnContextMenuCommand(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
    {
        if ((int)commandId == Copy)
        {
            //using System.Windows.Forms;
            Clipboard.SetText(parameters.SourceUrl);
        }
        return false;
    }

    void IContextMenuHandler.OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
    {

    }

    bool IContextMenuHandler.RunContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
    {
        return false;
    }
}

Then it's just a case of assigning an instance to the MenuHandler property of ChromiumWebBrowser

browser.MenuHandler = new MenuHandler();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM