Learn how to show the "Show Chrome Dev Tools option" in the icon menu properly without have this tedious error.

CefSharp ChromeDevToolsSystemMenu does not exist in the current context

This error is common when you're trying to show the Show Chrome Dev Tools option in the list of the Icon Menu in a winforms application.

To solve this error you need to create this method, as it doesn't exists in the library. 

Our method needs to use the DLLImport method, therefore we need to add a reference that allow us to do this, import the following component in your form :

using System.Runtime.InteropServices;

Now create the function that will allow us to create the option to appear this option in the icon list menu :

private static class ChromeDevToolsSystemMenu
{
    // P/Invoke constants
    public const int WM_SYSCOMMAND = 0x112;

    private const int MF_STRING = 0x0;
    private const int MF_SEPARATOR = 0x800;

    // ID for the Chrome dev tools item on the system menu
    public const int SYSMENU_CHROME_DEV_TOOLS = 0x1;

    // P/Invoke declarations
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr GetSystemMenu(IntPtr hWnd, bool bRevert);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool AppendMenu(IntPtr hMenu, int uFlags, int uIDNewItem, string lpNewItem);

    public static void CreateSysMenu(Form frm)
    {
        // in your form override the OnHandleCreated function and call this method e.g:
        // protected override void OnHandleCreated(EventArgs e)
        // {
        //     ChromeDevToolsSystemMenu.CreateSysMenu(frm,e);
        // }

        // Get a handle to a copy of this form's system (window) menu
        var hSysMenu = GetSystemMenu(frm.Handle, false);

        // Add a separator
        AppendMenu(hSysMenu, MF_SEPARATOR, 0, string.Empty);

        // Add the About menu item
        // You can customize the message instead dev tools
        AppendMenu(hSysMenu, MF_STRING, SYSMENU_CHROME_DEV_TOOLS, "&Chrome Dev Tools");
    }
}

Note that this method doesn't do anything by itself. We need to initialize it and filter the events in the WndProc event that we need to override in our class. Override the WndProc in the class using the following method :

/// <summary>
///  Note that the <chromeBrowser> variable needs to be replaced by your own instance of  ChromiumWebBrowser
/// </summary>
protected override void WndProc(ref Message m)
{
    base.WndProc(ref m);

    // Test if the About item was selected from the system menu
    if ((m.Msg == ChromeDevToolsSystemMenu.WM_SYSCOMMAND) && ((int)m.WParam == ChromeDevToolsSystemMenu.SYSMENU_CHROME_DEV_TOOLS))
    {
        chromeBrowser.ShowDevTools();
    }
}

Note: Be careful as you need to replace the chromeBrowser variable for your own instance of ChromiumWebBrowser as the summary says.

Finally, you can proceed to use the ChromeDevToolsSystemMenu method without errors in your class. Is a good practice to enable it in the constructor of your class (form) :

public Form1()
{ 
   // Other methods as InitializeComponents and chromium
   InitializeComponent();
   InitializeChromium();

   // Enable the option in the icon list menu of the form
   ChromeDevToolsSystemMenu.CreateSysMenu(this);
}

Senior Software Engineer at Software Medico. Interested in programming since he was 14 years old, Carlos is a self-taught programmer and founder and author of most of the articles at Our Code World.

Sponsors