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:
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:
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:
Using the int type with size 3 or bigger is one of the ways of solving this.
1: int variantVar = new int;
The VARIANT type can be seen in this MSDN webpage:
Once we solved this problem, we can use the IOleCommandTarget function Exec:
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
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;
5: ref CGID_ShellDocView,
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);
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 = VT_INT_PTR;
2: // Remember that variantVarIn is reserved
3: variantVarIn = handleMenu.ToInt32();
5: variantVarOut = VT_I4;
6: // Remember that variantVarOut is reserved
7: variantVarOut = dwID;
9: // Insert Shortcut Menu Extensions from registry.
11: ref CGID_ShellDocView,
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