How to customize the WebBrowser context menu in C#

It is hard to find on the internet a detailed and complete solution for modifying the contextual menu due to several reasons.

One of these reasons is that many of the implementations found use the System.Windows.Forms.ContextMenu; you can see one of them here:

Component-Based Development with Visual C#

In these kinds of examples the system menu is not invoked from the ShowContextMenu, instead a user customized menu is. This menu does not allow modifying it as we need.

Another reason is due to the programming language. In the MSDN website a C++ implementation of the ShowContextMenu can be found:

WebBrowser Customization (Part 2)

The problem is that when we want to implement it in C# difficulties such as not being able to call system functions, use the same data types, and many others arise.

Maybe the biggest difficulty can be found when trying to marshall the CComVariant class. A huge variety of solutions can be found on the internet, but they usually do not work (at least in the case mentioned above). Here are some examples of them:

VB Variant Equivalent in C#

Object To Variant

What is the equivalent of Variant data type in C#.NET?

Using the int[] type with size 3 or bigger is one of the ways of solving this.

   1:  int[] variantVar = new int[3];

The VARIANT type can be seen in this MSDN webpage:

VARIANT and VARIANTARG

Once we solved this problem, we can use the IOleCommandTarget function Exec:

   1:  [PreserveSig]
   2:  int Exec(
   3:      ref Guid pguidCmdGroup,
   4:      int nCmdID,
   5:      int nCmdExecOpt,
   6:      // we need to have this an array because callers 
   7:      // need to be able to specify NULL or VT_NULL
   8:      [In, MarshalAs(UnmanagedType.LPArray)] int[] pvaIn,
   9:      [Out, MarshalAs(UnmanagedType.LPArray)] int[] pvaOut
  10:      );

When calling Exec for the first time, we get the handle for the language submenu. We obtain it in variantVar variable:

   1:  int[] nullVariantVar = null;
   2:  int[] variantVar = new int[3];
   3:   
   4:  spCT.Exec(
   5:              ref CGID_ShellDocView,
   6:              SHDVID_GETMIMECSETMENU,
   7:              0,
   8:              nullVariantVar,
   9:              variantVar
  10:              );

Now we must parse variantVar in order to get the result (the handle for the language submenu). The first value that we get is a VARTYPE type, which indicates the kind of variable that we will find next. Then there is a reserved spot of three WORD long, followed by the value we are looking for. So the handle for the submenu is on the second place of the array:

   1:  IntPtr handleSubMenu = new IntPtr(variantVar[2]);

We can replace passing the CComVariant argument to the function by creating a new variable shown in the code below and then call again Exec:

   1:  variantVarIn[0] = VT_INT_PTR;
   2:  // Remember that variantVarIn[1] is reserved
   3:  variantVarIn[2] = handleMenu.ToInt32();
   4:   
   5:  variantVarOut[0] = VT_I4;
   6:  // Remember that variantVarOut[1] is reserved
   7:  variantVarOut[2] = dwID;
   8:   
   9:  // Insert Shortcut Menu Extensions from registry.
  10:  spCT.Exec(
  11:              ref CGID_ShellDocView,
  12:              SHDVID_ADDMENUEXTENSIONS,
  13:              0,
  14:              variantVarIn,
  15:              variantVarOut
  16:              );

We obtain the complete context menu as a result of the instructions shown above. This menu can be modified as much as we desire. Using this, you can add or remove menu items and also their functionality. For example you can call methods implemented in your project from the desired menu item.

Now you can build a customized browser using C# !

Custom Outlook Development

We have a team of experts developing plug-ins for Outlook. We can go beyond Outlook API and develop modifications to those functions that lacks of some features that your product may need. Our team leaders are experts running projects and our customers can feel confident that their product will be released in time. Our sales team can be contacted any time in our office in California  (310) 237-6506.
For more information visit Outlook plugin development

  • Kevin

    It took me some time to find a solution to modifying the context menu in the webbrowser control using C#. It seems you may have what I need, however I have no idea to implement it. What’s the next step? How do I actually do what I need to do? I want to eliminate everything except Copy, Cut and Paste, when interacting text in HTML form elements. Can you help me?

  • Robert

    I have the same issue as Kevin. I have seen several ways of doing this but no one has actually posted complete samples of how to implement this.

  • JP

    Yes an actual demonstation of chopping up the webbrowser context menu would be greatly appreciated.

  • http://pctvsoftwarestore.com PCTV Software

    Very useful post, exactly the information I was looking for. The screen shots will make this a step-by-step process.

  • http://www.oma.com.tw/ 熱水器

    I understand the 2concern,I am very y sad to hear that you got your Supra fixed. I remember reading about some of your doubts3x !.

  • http://www.twitter.com/VinitaCorvera Vinita Corvera

    Detailed article can i have your permision to translate into German for our sites viewers? Thanks

  • http://www.twitter.com/MarcellaLasker Marcella Lasker

    Great post can i translate into Spanish for our sites visitors? Thanks

  • Pablo Yabo

    You can translate if you refer the original article with a link, and the text of the link should be the title of the article.

  • http://wowlevelguide.piczo.com/?cr=3 Tiana Reul

    its excellent as your other articles : D, appreciate it for posting .

  • Ampelio Attanasi

    Don’t get me wrong… but given the title “How to customize the WebBrowser context menu in C#” this article actually explains nothing :-

    • http://blog.databigbang.com/ Sebastian Wain

      Can you elaborate?

      • Ampelio Attanasi

        You may have noticed that other three comments say the same (JP, Robert, Kevin). Here it is not said how you can integrate in a WebBrowser a custom menu entry using C#.
        There are some links, pieces of code about variant but no hints on how to write the body of the ShowContextMenu() function in C# so to add a custom menu.

        Lat’s say I want to add an entry to the WebBrowser menu, how should I change the function?

        public int ShowContextMenu(int dwID, tagPOINT pt, object pcmdtReserved, object pdispReserved)
        { // this will just show the usual menu SFalse == 0x00000001
        return SFalse;
        }

        • http://blog.databigbang.com/ Sebastian Wain

          The article was written in 2008, in the context of Internet Explorer 6 and Internet Explorer 7 related technologies. We can take a look at our source code repository for some code snippet but we don’t have a whole public working example.

          If I remember well, in 2008 this information was enough to solve the problem.

          • Ampelio Attanasi

            Hi Sebastian,

            the technology behind the WebBrowser Activex control has not changed at all – it’s already difficult to make the WebBrowserSite thing work (the interface for the OLE interaction are protected) but once done that *nobody* gives a C# implementation for the contextual menu modification (maybe because MSDN only show -badly- how to do that in C++?)
            Don’t worry, I’ll try to solve it somehow by myself – thank you for your time.
            Bye!

  • mena99260

    Folks! I have to declare that developers for this CookiePie are done for good. They are gone!!!

    best
    cookware

    http://www.danscookware.com

  • mena99260

    sized businsses are the backbone of this country, if you ask me! debt consolidation loans

  • mena99260

    Thus, when the placement ended in April 2011, we incorporated as a
    limited company and were hired as consultants by the British Council to
    carry on working on their film collection….
    homepage

  • mena99260

    [Out, MarshalAs(UnmanagedType.LPArray)] int[] pvaOut
    preschool
    programs

  • mena99260

    Don’t get me wrong… but given the title “How to customize the
    WebBrowser context menu in C#” this article actually explains nothing
    :-
    Fort-Lauderdale-Escorts

  • mena99260

    Don’t get me wrong… but given the title “How to customize the
    WebBrowser context menu in C#” this article actually explains nothing
    :-
    alternative allergy treatment

  • mena99260

    Thus, when the placement ended in April 2011, we incorporated as a
    limited company and were hired as consultants by the British Council to
    carry on working on their film collection….

    twitters

  • David John Michael

    The Teach Yourself (related to the Dummies guide books) Beginners Czech is about the best starting place if you are learning on your own. do follow blog comments

  • Ashar Arain

    Cut and Paste, when interacting text in HTML form elements. Can you help me?…
    Free PDF Links

  • Imtiaz Ali

    l demonstation of chopping up the webbrowser context menu would be greatly appreciated.
    cialis

  • Imtiaz Ali

    demonstation of chopping up the webbrowser context menu would be greatly appreciated…
    www

  • Black Men

    eventually it ran out of stock and no one is supplying it anymore. Leads and feedback will be appreciated.
    Den Haag

  • Black Men

    One of these reasons is that many of the implementations found use the
    System.Windows.Forms.ContextMenu; you can see one of them here:
    rasvaimu eestis

  • Imtiaz Ali

    implementations found use the System.Windows.Forms.ContextMenu; you can see one of them here….
    homepage

  • Imtiaz Ali

    found use the System.Windows.Forms.ContextMenu; you can see one of them here……
    find

  • Black Men

    According to the company, it has the feature of flexible pressure calibration and push button configuration.
    free

  • Imtiaz Ali

    Then there is a reserved spot of three WORD long, followed by the value we are looking for.
    frank
    mitchell survival

  • Imtiaz Ali

    problem, we can use the IOleCommandTarget function Exec:
    self
    storage space

  • Imtiaz Ali

    wah jangan lupa kunjungi balik lapak ane nih
    useful information gan,
    storage units cincinnati

  • Black Men

    Another reason is due to the programming language. In the MSDN website a C++ implementation of the ShowContextMenu can be found:
    mile222 instagram followers

  • Imtiaz Ali

    have time to spend hours in the kitchen to prepare your meals everyday…
    hgh
    kits

  • Imtiaz Ali

    on your feet wearing this fantastic toe ring! Featuring a cute
    butterfly-shaped design this toe ring adds a touch of beauty to your
    feet…

    http://buysoundcloudplaysnow.blog.com/

  • Black Men

    A huge variety of solutions can be found on the internet, but they
    usually do not work (at least in the case mentioned above). Here are
    some examples of them:
    freestanding
    baths

  • Imtiaz Ali

    really the best theme on this registry related issue. I fit in with your
    decisions and will excitedly anticipate your next overhauls.
    Online Courses for English
    Exam Preparation

  • Imtiaz Ali

    pohľadu chápeme ako akúkoľvek ponuku, ktorá je určená na predaj na trhu za účelom uspokojenia určitej potreby.
    Online Courses for English
    Exam Preparation

  • Imtiaz Ali

    enough popular and finally founded your blog, it has superb topics..

    artesia border patrol academy

    • Imtiaz Ali

      obviously a lot to know about this. We are really grateful for your blog post.
      διαιτα

  • Imtiaz Ali

    demonstation of chopping up the webbrowser context menu would be greatly appreciated…
    Keystones

  • Imtiaz Ali

    spend hours in the kitchen to prepare your meals everyday…

    mortgage

  • Imtiaz Ali

    Some are done with lots of teacher help; those aren’t eligible for this competition .
    learn

  • Imtiaz Ali

    I have you bookmarked your site to check out the new stuff you post.
    acne-overview

  • Imtiaz Ali

    demonstation of chopping up the webbrowser context menu would be greatly appreciated..
    toronto-limousine-rental.ca

  • ehtisham khan

    Howdy! This post could not be written any better! Reading through this
    article reminds me of my previous roommate! He continually kept
    preaching about this.
    tech finger gloves

  • Imtiaz Ali

    type with size 3 or bigger is one of the ways of solving this…
    Mapleexecutivelivery.com

  • Imtiaz Ali

    blog via Google while searching for such kinda informative post and your post looks very interesting for me.
    Trading
    binario

  • Imtiaz Ali

    Inform me when new comments are additional- checkbox now every time a remark is extra I have four e-mails with the exact same review.
    Chat room

  • Imtiaz Ali

    superbe tuto d’une grande simplicité même pour une débutante comme moi!! J’ai adoré réaliser la pochette..

    hklovechat

  • ehtisham khan

    Cheers for sharing with us your blog. I came across such informative post.Keep up the good work friend.
    click for source

  • Black Men

    In the MSDN website a C++ implementation of the ShowContextMenu can be found: Top GPT Sites

  • Imtiaz Ali

    with us your blog. I came across such informative post.Keep up the good work friend…
    gloves for men

    • Imtiaz Ali

      Salmon Creek, in their own backyard, is home to coho salmon.
      Dordogne

  • Imtiaz Ali

    everything except Copy, Cut and Paste, when interacting text in HTML form elements..
    why do texting gloves work

    • Imtiaz Ali

      Thanks for posting this. Your blog is so interesting and very informative.
      wirral caterers

  • Imtiaz Ali

    valuable asset; change no matter how the years and will continue its modern charm .
    Hotmail account

  • Black Men

    One of these reasons is that many of the implementations found use the
    System.Windows.Forms.ContextMenu; you can see one of them here.. limo hire hertfordshire

  • Imtiaz Ali

    Our sales team can be contacted any time in our office in California (310) 237-6506.

    ice cream machines for home

  • Imtiaz Ali

    Barthel sings hauntingly through an MF-104Z Analog Delay while playing not one,
    ice cream maker crank

  • Imtiaz Ali

    product will be released in time. Our sales team can be contacted any time in our office in California
    ..
    Sillas

    • Imtiaz Ali

      film you are referring. But I intend to do it soon. I love honest romantic movies with a twist.
      what is vps
      hosting

  • Imtiaz Ali

    us your blog. I came across such informative post.Keep up the good work friend…
    mode
    accessoires

  • Aisha Malik

    business get started with a successful social networking campaign.
    punch bags for sale

  • Imtiaz Ali

    found on the internet, but they usually do not work (at least in the case mentioned above)

    Shopping Bags

  • Imtiaz Ali

    menikmati posting ini. Ini benar-benar bagus. Saya suka gaya penulis…
    debloquer iphone
    pas cher

  • jackman3

    projects and our customers can feel confident that their product will
    be released in time. Our sales team can be contacted any time in our
    office in California
    resep kue

  • jackman3

    the internet, but they usually do not work (at least in the case mentioned above). Here are some examples of them:
    http://www.isabisformulae.com

  • Imtiaz Ali

    these fluctuations because I describe levels of giftedness within a fairly broad estimated.
    water heater maintenance

  • Black Men

    Our team leaders are experts running projects and our customers can feel confident that their product will be released in time.http://www.profischnell.de/uebersetzungsbueros/uebersetzungsbuero-ludwigshafen.html

  • Black Men

    In these kinds of examples the system menu is not invoked from the ShowContextMenu, instead a user customized menu is.loft conversions

  • Black Men

    In these kinds of examples the system menu is not invoked from the
    ShowContextMenu, instead a user customized menu is. This menu does not
    allow modifying it as we need..
    watch free live
    streaming soccer online

  • Black Men

    A huge variety of solutions can be found on the internet, but they
    usually do not work (at least in the case mentioned above). Here are
    some examples of them:
    glitter gel ebay

  • http://www.fxtradersclassroom.com ehtisham

    I’m happy I located this blog! From time to time, students want to
    cognitive the keys of productive literary essays composing. Your
    first-class knowledge about this good post can become a proper basis for
    such people
    sandwich platters

  • Imtiaz Ali

    customers can feel confident that their product will
    be released in time
    .
    free vpn service

    • Imtiaz Ali

      along with that my partner and I offer the admiration of our company towards this remarkable submit.
      iphone to tv
      wireless

  • Black Men

    ShowContextMenu, instead a user customized menu is. This menu does not allow modifying it as we need. loft conversions kent

  • Imtiaz Ali

    Championships, at the 2014 Commonwealth Games. But at the end of the day, it is just bike racing ..

    vaporizer pen dry
    herb

  • Imtiaz Ali

    knowledge and kindness in playing with all the pieces was very useful…
    mythemeshop coupon code

  • Black Men

    Another reason is due to the programming language. In the MSDN website a C++ implementation of the ShowContextMenu can be found:health benefits of foods

  • Black Men

    we work fiverr name jackseo40 online on high pr do-follow blog commments links in just 5$. so what are you waiting for.we are here for you so just 5$ give the order now. high quality backlink service

  • Imtiaz Ali

    PROMISE not to abuse but they work so well your Ex will literally be BEGGING..
    breast
    actives

  • Imtiaz Ali

    will take turns directing one another, in short takes, of the opening.,
    health

  • Imtiaz Ali

    f only all bloggers offered the same level of content as you, the internet would be a much better place…
    Miami Beach Real
    Estate

  • Jackman30

    experts running projects and our customers can feel confident that their product will be released
    best
    hypertension remedies without medication

  • Imtiaz Ali

    have seen several ways of doing this but no one has actually posted complete samples of how to implement this..
    gurgaon to jaipur
    cab

  • Imtiaz Ali

    webpage and along with the suitable quantity rely; several folks will most likely be captivated to your account…
    no-adware adwcleaner guide

  • Amezing Live

    programming language. In the MSDN website a C++ implementation of the ShowContextMenu can be found:
    insurance broker
    calgary

  • Imtiaz Ali

    car from delhi to
    jaipur

    This requires the manifest file to have the same name as your application executable,..

  • khan

    hey i cant get the name and status window to change its still on the ugly white not the polished aluminum.
    liberum help desk

  • Ashar Arain

    Good job! You guys do a great blog, and have some great contents. Keep up the good work….

    hamstring stretch device

  • jack150

    Yes an actual demonstation of chopping up the webbrowser context menu would be greatly appreciated.
    info

  • Imtiaz Ali

    he natural sound of your guitar! If you like the way your guitar sounds,
    real estate

  • ramzan

    I am actually getting ready to across this information, is very helpful my friend.
    ny divorce

  • ramzan

    No se aceptan plurales ni verbos infinitivo…… schuhe online

  • John Seo

    light and produce it essential. Lots more people should check this out and understand why side with the story. neuroflexyn

  • Ashar Arain

    We are really grateful for your blog post. You will find a lot of approaches after visiting your post…..
    facebook

  • Ashar Arain

    Thanks for such post and
    please keep it up. Great work..
    paradis