How to add new items to the native Context Menu in CefSharp

By FoxLearn 6/29/2024 3:10:03 AM   210
To add new items to the native Context Menu in CefSharp, you can follow these steps.

How to add new items to the native Context Menu in CefSharp

You can create a custom Menu Handler, then implement your custom context menu handler by inheriting from IContextMenuHandler. This interface provides methods to handle context menu creation and actions.

using CefSharp;
using CefSharp.WinForms;
using System;
using System.Collections.Generic;

public class CustomContextMenuHandler : IContextMenuHandler
{
    public void OnBeforeContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
    {        
        // You can add a separator in case that there are more items on the list
        if (model.Count > 0)
            model.AddSeparator();

        // Adding a new item to the context menu
        model.AddItem((CefMenuCommand)26501, "Custom Item", 26501); 

        // Add a separator
        model.AddSeparator();

        // Add another example item
        model.AddItem((CefMenuCommand)26503, "Display alert message");
    }

    public bool OnContextMenuCommand(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, CefMenuCommand commandId, CefEventFlags eventFlags)
    {
        // Handle the command if needed
        if (commandId == (CefMenuCommand)26501)
        {
            // Do something when the custom menu item is clicked
            return true; // Return true to indicate that the command was handled
        }

        // Handle to display alert message
        if (commandId == (CefMenuCommand)26503)
        {
            MessageBox.Show("This is test message !");
            return true;
        }
        return false; // Return false if the command was not handled
    }

    // Other IContextMenuHandler methods can be implemented as needed
    public void OnContextMenuDismissed(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame)
    {
    }

    public bool RunContextMenu(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model, IRunContextMenuCallback callback)
    {
        return false; // Return false if you want to use the default context menu
    }
}

Finally, Set your custom Menu Handler as default on the browser as shown below.

CefSettings settings = new CefSettings();

// Initialize cef with the provided settings
Cef.Initialize(settings);

// Create a browser component
ChromiumWebBrowser chromeBrowser = new ChromiumWebBrowser("www.foxlearn.com");

// Register your Custom Menu Handler as default
chromeBrowser.MenuHandler = new CustomContextMenuHandler();

After implementing the above steps, build your application and run it. Now, when you right-click within the ChromiumWebBrowser control, you should see your custom context menu item added to the menu.

How to disable context menu in CefSharp chromium browser

To prevent the native Context Menu from appearing, you can implement the IContextMenuHandler interface, and then in IContextMenuHandler.OnBeforeContextMenu you need to call the Clear method.

public void OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
{
    model.Clear();
}

You need to the ChromiumWebBrowser's MenuHandler property to an instance of your implementation.

chromeBrowser.MenuHandler = new CustomContextMenuHandler();

Remove all menu items. Can be used to disable context menu.