Click here to Skip to main content
Click here to Skip to main content

.NET Shell Extensions - Shell Context Menus

By , 5 Mar 2013
 
Prize winner in Competition "Best C# article of January 2013"
Introduction      

Until .NET 4.0 it was impossible to reliably create Windows Shell Extensions using .NET code. With improvements to the framework, it is now possible to use .NET to create these extensions. In this article, I'll show you how to quickly create Shell Context Menus as a C# class library.  

 

Above: A screenshot of an example Shell Context Menu Extension - the 'Count Lines...' item is a custom shell extension that this article demonstrates how to create. 

The Series 

This article is part of the series '.NET Shell Extensions', which includes:
  1. .NET Shell Extensions - Shell Context Menus  
  2. .NET Shell Extensions - Shell Icon Handlers  
  3. .NET Shell Extensions - Shell Info Tip Handlers     
  4. .NET Shell Extensions - Shell Drop Handlers   
  5. .NET Shell Extensions - Shell Preview Handlers   
  6. .NET Shell Extensions - Shell Icon Overlay Handlers
  7. .NET Shell Extensions - Shell Thumbnail Handlers 

What Are Shell Context Menus?  

Shell Context Menus are COM servers that are registered in the system that allow the context menus of shell objects to be extended. This could be the context menu for a specific file type, such as *.txt files, file classes such as 'text files', drives, folders and more. The context menus can be used to provide advanced functionality that can be accessed quickly through Windows Explorer.

Getting Started 

There's a lot of work involved in setting up Shell Extensions. Specific COM interfaces have to be implemented, servers must be built, the registry must be updated in a variety of ways. We're going to use a library I have developed called 'SharpShell' to do all of the hard work - leaving us with the task of creating a lightweight class library that contains our extension class. 

Our Goal 

The code below creates a Shell Extension that allows you to count the lines in any text file by right clicking on it and choosing 'Count Lines'. For the rest of the article I'll show you how to create a library like this. The code is shown first because I want to highlight how straightforward writing these libraries is when using SharpShell.

/// <summary>
/// The CountLinesExtensions is an example shell context menu extension,
/// implemented with SharpShell. It adds the command 'Count Lines' to text
/// files.
/// </summary>
[ComVisible(true)]
[COMServerAssocation(AssociationType.ClassOfExtension, ".txt")]
public class CountLinesExtension : SharpContextMenu
{
    /// <summary>
    /// Determines whether this instance can a shell context show menu, given the specified selected file list.
    /// </summary>
    /// <returns>
    ///   <c>true</c> if this instance should show a shell context menu for the specified file list; otherwise, <c>false</c>.
    /// </returns>
    protected override bool CanShowMenu()
    {
        //  We always show the menu.
        return true;
    }
 
    /// <summary>
    /// Creates the context menu. This can be a single menu item or a tree of them.
    /// </summary>
    /// <returns>
    /// The context menu for the shell context menu.
    /// </returns>
    protected override ContextMenuStrip CreateMenu()
    {
        //  Create the menu strip.
        var menu = new ContextMenuStrip();
 
        //  Create a 'count lines' item.
        var itemCountLines = new ToolStripMenuItem
        {
            Text = "Count Lines...",
            Image = Properties.Resources.CountLines
        };
 
        //  When we click, we'll count the lines.
        itemCountLines.Click += (sender, args) => CountLines();
 
        //  Add the item to the context menu.
        menu.Items.Add(itemCountLines);
 
        //  Return the menu.
        return menu;
    }
 
    /// <summary>
    /// Counts the lines in the selected files.
    /// </summary>
    private void CountLines()
    {
        //  Builder for the output.
        var builder = new StringBuilder();
 
        //  Go through each file.
        foreach (var filePath in SelectedItemPaths)
        {
            //  Count the lines.
            builder.AppendLine(string.Format("{0} - {1} Lines", Path.GetFileName(filePath), File.ReadAllLines(filePath).Length));
        }
 
        //  Show the ouput.
        MessageBox.Show(builder.ToString());
    }
} 

That's pretty clean and simple - now let's look in detail about how to create this Shell Context Menu with SharpShell.

Step 1: Creating the Project 

First, create a new C# Class Library project. 

Tip: You can use Visual Basic rather than C# - in this article the source code is C# but the method for creating a Visual Basic Shell Extension is just the same. 

In this example we'll call the project 'CountLinesExtension'.

Now add the following references: 

  1. System.Windows.Forms
  2. System.Drawing
System.Windows.Forms is needed because we're going to use the WinForms ContextMenuStrip to define the context menu. System.Drawing is needed as we're going to want to use Icons. 

Rename the 'Class1.cs' file to 'CountLinesExtension.cs'. We should now have a project structure that looks like this:

Step 2: Referencing SharpShell   

We now need to add a reference to the core SharpShell library. You can do that in a few different ways:

Add Reference 

Download the 'SharpShell Library' zip file at the top of the article and add a reference to the downloaded SharpShell.dll file.  

Tip: The download on this article is correct at the time of writing - if you need the latest version, use Nuget (as described below) or get the library from sharpshell.codeplex.com.  

Use Nuget

If you have Nuget installed, just do a quick search for SharpShell and install it directly - or get the package details at https://www.nuget.org/packages/SharpShell.

Use CodePlex 

Rather than getting the library from this page, which may not be the latest version, you can always get the very latest version of the library from CodePlex - on the SharpShell home page which is sharpshell.codeplex.com. Nuget will always have the latest stable version - CodePlex may have betas available, and the CodeProject articles will have the version that was available at the time of writing. 

Step 3: Deriving from SharpContextMenu   

Now things get interesting. Derive your CountLinesExtension class from SharpContextMenu:
/// <summary>
/// The Count Lines Context Menu Extension
/// </summary>
public class CountLinesExtension : SharpContextMenu
{
} 

Now we must implement the abstract members of the class. Right click on the SharpContextMenu part of the line and choose 'Implement Abstract Class':

 

This'll create implementations of the two functions needed - CanShowMenu and CreateMenu:

/// <summary>
/// The Count Lines Context Menu Extension
/// </summary>
public class CountLinesExtension : SharpContextMenu
{
    protected override bool CanShowMenu()
    {
        throw new NotImplementedException();
    }
 
    protected override ContextMenuStrip CreateMenu()
    {
        throw new NotImplementedException();
    }
}

By implementing these two functions we can provide all of the functionality needed. Here's what they do:

CanShowMenu

This function is called to determine whether we should show the Conext Menu Extension for a given set of files. The files the user has selected are in the property SelectedItemPaths. We can check these file paths to see whether we actually want to show the menu. If the menu should be shown, return true. If not, return false.

CreateMenu

This function is called to actually create the Context Menu. A standard WinForms ContextMenuStrip is all we need to return.

Here's how we'll implement the two functions.

protected override bool CanShowMenu()
{
    //  We will always show the menu.
    return true;
}
 
protected override ContextMenuStrip CreateMenu()
{
    //  Create the menu strip.
    var menu = new ContextMenuStrip();
 
    //  Create a 'count lines' item.
    var itemCountLines = new ToolStripMenuItem
    {
        Text = "Count Lines"
    };
 
    //  When we click, we'll call the 'CountLines' function.
    itemCountLines.Click += (sender, args) => CountLines();
 
    //  Add the item to the context menu.
    menu.Items.Add(itemCountLines);
 
    //  Return the menu.
    return menu;
}
 
private void CountLines()
{
    //  Builder for the output.
    var builder = new StringBuilder();
 
    //  Go through each file.
    foreach (var filePath in SelectedItemPaths)
    {
        //  Count the lines.
        builder.AppendLine(string.Format("{0} - {1} Lines", Path.GetFileName(filePath), File.ReadAllLines(filePath).Length));
    }
 
    //  Show the ouput.
    MessageBox.Show(builder.ToString());
} 

For CanShowMenu we return true always - shortly we'll see why we don't need to validate that we have text files. For CreateMenu, we build a context menu strip with one item only, that has the caption 'Count Lines' and calls the CountLines function. 

The CountLines function goes through the SelectedItemPaths and counts the lines in each file - then displays a message box with the summary.  

Step 4: Handling the COM Registration     

There are just a few things left to do. First, we must add the COMVisible attribute to our class. 

[ComVisible(true)]
public class CountLinesExtension : SharpContextMenu
Why? Well, even though our class doesn't really look like it, it is in fact a COM server. If you were to look at the base class, you'd see that we're implementing COM interfaces such as  IShellExtInit, IContextMenu, and ISharpShellServer. We don't need to worry about what they do, but for the system to be able to create our extension, it must have this attribute.

Next, we must give the assembly a strong name. There are ways around this requirement, but generally this is the best approach to take. To do this, right click on the project and choose 'Properties'. Then go to 'Signing'. Choose 'Sign the Assembly', specify 'New' for the key and choose a key name. You can password project the key if you want to, but it is not required:

 

The final step - we now need to associate our extension with some file types. We can do that with the COMServerAssociation attribute (provided by SharpShell):

[ComVisible(true)]
[COMServerAssocation(AssociationType.ClassOfExtension, ".txt")]
public class CountLinesExtension : SharpContextMenu

So what have we done here? We've told SharpShell that when registering the server, we want it to be associated with the class of *.txt files. This means that we won't just have it available for anything that ends in *.txt, but anything that is the same class. In basic terms that's most things that share the same icon as the *.txt files.

You can do some pretty funky things with the COMServerAssociation attribute - you can associate with folders, drives, unknown files, specific extensions and so on. The full documentation for this feature is at COM Server Associations on the SharpShell CodePlex site. 

And that's it! Building the project creates the CountLinesExtension assembly, which can be registered as a COM server to add the context menu to the system. Registering the COM server is a debugging and deployment task, so we'll talk about this in detail in the next section. 

Debugging the Shell Extension 

The Shell Extension is going to be hosted in Windows Explorer - due to the roundabout way that .NET COM Servers are loaded, it's damn near impossible to get a debugger into the process and step through the managed code. However, there is a way to debug your extension quickly. SharpShell comes with some tools that make working with SharpShell COM servers a bit easier, and one of them is the Server Manager. We can use this tool to debug our extension.  

Open the Sever Manager tool and use File > Load Server to load the built server file (the dll). You can also drag the server into the main window. Selecting the server will show you some details on it.

The Server Manager is very useful - it will tell you whether the server is installed (32 or 64 bit mode) and some more details.    

If you click load the SharpContextMenu server, then select it, you can go to the 'Tools' menu and choose 'Test Context Menu'.

When you use 'Test Context Menu' you'll get a Test Shell Window. This is a basic implementation of the Windows Explorer application. You can right click on any item to test the Shell Context Menu.

Tip: Regardless of the COMServerAssocations you've set, the Test Shell will always attempt to create the context menu for the items created.

Attaching a debugger to the ServerManager.exe process will allow you to debug into your Context Menu and test its functionality, without having to register the server in Windows. Here's what the Test Shell will look like when running the Count Line Context menu extension.

Installing and Registering the Shell Extension 

There are a number of ways to install and register SharpShell Shell Extensions. In this section I'll detail them all. 

The regasm Tool

You can use the tool 'regasm' to install and register a shell extension. When using regasm, the shell extension will be installed into the registry (i.e. the Class ID of the COM Server will be put in the COM Server Classes section and associated with the path to the actual server file), it will also register the associations.

The Server Manager Tool

The Server Manager Tool is my preferred approach for installing/uninstalling and registering/unregistering, at least during development, because it lets you install and register as separate steps. It will also let you specify whether you're installing/uninstalling etc in 32 bit or 64 bit mode.

Manual Registry Manipulation

Generally a bad approach, but if you absolutely have to then the MSDN documentation for Shell Extensions describes the changes that must be made to the registry to manually register a COM server, or a Managed COM Server. The documentation is listed in the 'Useful Resources' section.  

Useful Resources  

Creating Shortcut Menu Handlers in Windows: The most important resource - the detail on how Shell Context Menu extensions work in Windows. 

SharpShell on CodePlex: The home of the SharpShell project - includes documentation, discussions and the latest source code and releases.

What's Next?

SharpShell will over time provide a mechanism to create all of the available Shell Extensions using .NET. Currently Icon Handlers are implemented (I'm working on the documentation) and Property Sheet Handlers are implemented (with a few bugs to iron out). Each extension type will have an article.

I hope you've found this article useful - for feature requests, bugs or comments, you can use the comments section below or the CodePlex site.   

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Dave Kerr
Software Developer
United Kingdom United Kingdom
Member
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMonjurul Habib16 Feb '13 - 23:44 
nice work
GeneralRe: My vote of 5mvpDave Kerr17 Feb '13 - 0:57 
Thanks Smile | :)

GeneralMy vote of 5memberMihai MOGA16 Feb '13 - 19:17 
This is a great inspiring article. I am pretty much pleased with your good work. You put really very helpful information. Keep it up once again.
GeneralRe: My vote of 5mvpDave Kerr16 Feb '13 - 21:48 
Thanks very much Smile | :)

GeneralMy vote of 5memberSavalia Manoj M14 Feb '13 - 14:42 
Excellent article.
GeneralRe: My vote of 5mvpDave Kerr14 Feb '13 - 20:32 
Thanks Smile | :)

GeneralMy vote of 5memberchandruvelan13 Feb '13 - 22:16 
NICE Article
GeneralRe: My vote of 5mvpDave Kerr13 Feb '13 - 23:05 
Thanks Smile | :)

GeneralMy vote of 5memberMohammad A Rahman10 Feb '13 - 10:32 
Excellent work Smile | :)
GeneralRe: My vote of 5mvpDave Kerr10 Feb '13 - 20:13 
Thanks Smile | :)

GeneralMy vote of 5membercvogt614578 Feb '13 - 11:24 
Excellent article.
GeneralRe: My vote of 5mvpDave Kerr8 Feb '13 - 23:21 
Thanks Smile | :)

GeneralMy vote of 5memberJason McPeak7 Feb '13 - 8:55 
Well written, easy way to add shell extension
GeneralRe: My vote of 5mvpDave Kerr7 Feb '13 - 10:00 
Thanks Smile | :)

Questionprotected override bool CanShowMenu()memberJason McPeak7 Feb '13 - 7:01 
protected override bool CanShowMenu()
{
    return false;
}
Quote:
Make sure the server is loaded in server manager, then choose 'test server in test shell', as long as the server is a context menu server it'll call CanShowMenu on every file.
I agree, I have done this and the menu still displays.
AnswerRe: protected override bool CanShowMenu()mvpDave Kerr7 Feb '13 - 10:00 
Yup, the test shell is fairly dumb in it's approach to testing the context menu, it'll just run CanShowMenu on everything.
 
By the way, if you really want to test the server in a real shell, you can open the server manager, open the test shell, choose 'show open file dialog', navigate to an item you have an association for then right click on it. The only problem is that the server needs to be registered - and registering the server is a pain if you're still developing it!

QuestionMenu iconmemberMayzz6 Feb '13 - 14:05 
Hello,
 
Firstly thank you for this article, these tools and this wonderful library.
 
In your example you put an square icon. I would like to put an icon with a transparent background. The format of your example is BMP. I tried to put a JPEG but the background remains white. I searched to find the transparency color but in vain.
 
Do you have any idea?

AnswerRe: Menu iconmvpDave Kerr7 Feb '13 - 9:58 
Hi,
 
Thanks for your kind words - in terms of using transparency, if you make your resource a PNG file then you will indeed get a transparent background. Make sure that you use a 32 bit per pixel PNG - most image editors will use this automatically save PNG files in this format if they have transparent sections. You can use Paint.NET for free to do this:
 
Paint.NET[^]
 
Let me know if this helps, if not I'll send over a sample Smile | :)

GeneralRe: Menu iconmemberMayzz8 Feb '13 - 0:47 
Thank you for your quick response.
 
I've tried with PNG 24 + Alpha. I also tried with a icon. (Icon.ToBitmap) but nothing works.
 
The picture in the menu is deteriorated. And when I move the cursor to highlight the menu, the icon has a white background.
 
See screenshot to show you an example.
 
http://paf.im/z2riA
 
This is strange because, for example Notepad + + has a transparent icon (like many other programs).
GeneralRe: Menu iconmvpDave Kerr10 Feb '13 - 7:14 
As you say, other programs have transparent icons, so it can be done. I've raised an issue to look into it:
 
http://sharpshell.codeplex.com/workitem/1025[^]
 
If it's config then that's fine, otherwise it may be a code-change. Either way I'll let you know.
 
Cheers

GeneralRe: Menu iconmvpDave Kerr11 Feb '13 - 6:45 
Hi Mayzz,
 
I've updated the 'Count Lines Extension' sample - but only on CodePlex, you can get the source from:
 
http://sharpshell.codeplex.com/SourceControl/changeset/22734[^] (Just press the 'download' button in the toolbar).
 
In this example I use a png for the icon image and it works with transparency fine, see below (two screenshots side by side, moues-out and mouse-over):
 
Menu Item with Transparency Screenshot[^]
 
Try using this as the basis for your work - it's a standard PNG, any should work - why yours have not I don't know. Let me know if you have any more problems!

GeneralRe: Menu iconmemberAlexPi31419 Mar '13 - 6:43 
I don't know what you're talking about.
 
I've compiled your own sample and there IS a while border around the PNG that you've provided.
 
Transparency DOES NOT WORK.
 
This is on Windows 7 x64.
 
Regards,
Alex
GeneralRe: Menu iconmvpDave Kerr19 Mar '13 - 6:46 
Did you compile the sample from the latest cut of the code on the codeplex site rather than the download to this article? If so, it may be a color depth issue, what is the color depth of your screen set to?

GeneralRe: Menu iconmemberAlexPi31419 Mar '13 - 9:00 
Dave,
 
I've researched the problem some more.
 
The issue is not in your code but in Windows.
 
In order to properly show alpha-blended icons in menus, there are a number of approaches.
 
See this write up for some of these:
http://www.nanoant.com/programming/themed-menus-icons-a-complete-vista-xp-solution[^]
 
The HBMMENU_CALLBACK method would probably be the most practical.
 
I've looked through your source and it looks like you'd need to implement
IContextMenu2::HandleMenuMsg and IContextMenu3::HandleMenuMsg
and handle 2 messages.
 
As far as the other approaches, I've tried to create a PARGB32 in .NET with no success.
 
MSDN has a sample application detailing their method: http://msdn.microsoft.com/en-us/library/windows/desktop/bb757020.aspx[^]
 
...but the amount of code is mind boggling, and much more than I'm willing to tackle today Smile | :)
 
All in all, I've just decided to remove my icon and live with it, as this is the simplest solution with universal compatibility.
 
Oh, and
CanShowMenu
was never being called, so I've fixed that and recompiled.
 
Thanks for your effort,
Alex
GeneralRe: Menu iconmvpDave Kerr19 Mar '13 - 22:47 
Hi Alex,
 
Thanks very much for this information, I'm going to raise a ticket in the project to look into this and try and get a solid fix using IContextMenu2 and IContextMenu3.
 
It's interesting you've had this issue, I've had massive problems getting IThumbnailProvider working on a certain laptop, again, the issue being transparency in an HBITMAP.
 
You mentioned that CanShowMenu was not being called - and that you made a change - what was it?
 
Thanks,
 
Dave

QuestionCanShowMenu not being hitmembercryme31 Jan '13 - 6:12 
Attached debugger to ServerManager.exe but the CanShowMenu method was never hit.
AnswerRe: CanShowMenu not being hitmvpDave Kerr4 Feb '13 - 0:30 
Make sure the server is loaded in server manager, then choose 'test server in test shell', as long as the server is a context menu server it'll call CanShowMenu on every file. If this doesn't work, try downloading the source code and running the ServerManager program from that in debug mode Smile | :)

QuestionAfter Registering dll, Context Menu option "CountLines" didnt work PinmemberManfred Duerst30 Jan '13 - 22:52 
Hi Dave
 
I installed and registered CountLinesExtension.dll using ServerManager. In the Test Shell it works fine, not only with .txt but with every file type.
 
Using the real explorer I can't see the shell extension, even when I do "Restart Explorer" with the server manager. I also tried to restart windows (win 7).
 
Question: What are the meaning of the commands "Allways unload DLL" and "Desktop process"?
 
Thanks
Manfred
AnswerRe: After Registering dll, Context Menu option "CountLines" didnt work PinmvpDave Kerr4 Feb '13 - 0:29 
Hi,
 
Are you on Windows 7 32 or 64 bit? Are you registering in the correct architecture?
 
Always unload dll and desktop process are registry flags that affect how explorer handles it's processes, however, they're not used in windows 7.
 
In the test shell, you'll see the extension for every file type. The best thing to try now is the following:
 
1. Make sure that you're running the Server Manager tool as an administrator.
2. If this doesn't work, run server manager as an administrator, go to Diagnostics > Enable Log. Now clear the windows application event log, register the server and right click on a text file. You should get some event log entries in the Application Event log - are any of these errors?
 
Let me know if this works or not!

GeneralRe: After Registering dll, Context Menu option "CountLines" didnt work PinmemberManfred Duerst10 Feb '13 - 20:18 
Hi Dave
 
- I'm using Win 7 64Bit and installing 64Bit version
- The log is empty
- If I do restart explorer, in the windows application log a new message appears expectably "The shell stopped unexpectedly and explorer.exe was restartet"
 
same behaviour as before
GeneralRe: After Registering dll, Context Menu option "CountLines" didnt work PinmvpDave Kerr11 Feb '13 - 0:13 
Hi Manfred,
 
That means that the SharpShell dll isn't being loaded into memory, which means that the context menu dll is probably not loading either. As another test, as long as you are in debug mode and using the debug binaries (at this stage I'd say get the entire solution source code from codeplex and build the latest version) then if you load the assembly in the Server Manager and register it from there, you should definately get event log entries - e.g. "SharpShell loaded into ServerManager.exe process" at the very least.
 
Are you registering via regasm or the Server Manager?

GeneralRe: After Registering dll, Context Menu option "CountLines" didnt work PinmvpDave Kerr11 Feb '13 - 6:37 
Hi Manfred, one more thing - are you using Server Manager to register and if so, do you use 'Install Server (x64)' before using 'Register Server (x64)'?

GeneralRe: After Registering dll, Context Menu option "CountLines" didnt work PinmemberManfred Duerst14 Feb '13 - 23:56 
Hi Dave
 
Yes I'm using Server Manager to register and use 'Install Server (x64)' before using 'Register Server (x64)
GeneralRe: After Registering dll, Context Menu option "CountLines" didnt work PinmvpDave Kerr15 Feb '13 - 0:26 
OK - what about if you enable the log and run the server manager to register - do you see entries in the Application log for sharpshell, such as 'SharpShell loaded in process ServerManager'?

QuestionAfter Registering dll, Context Menu option "CountLines" didnt workmemberMember 237294924 Jan '13 - 5:40 
Hi Dave ,
 
Thanks for the article , it looks simple and neat. Could you please help me in resolving installing issue on it.
 
I registered CountLinesExtension.dll (the downloaded one) using regasm , was successfully registering , tlb files were created. Installed dll key was also found in registry.
 
Once registered, CountLines menu options are not seen in context menu for text file types. Could you please guide me if I have missed some steps or missed out some thing in the installation process?
 
Thanks
AnswerRe: After Registering dll, Context Menu option "CountLines" didnt workmvpDave Kerr24 Jan '13 - 11:40 
Hi,
 
Thanks for the post - sounds a bit strange - what version of Windows are you using and are you on 32 or 64 bit?
 
Can you check in the registry what you have under HKCR/.txt ? You should have the default value of the key set to something like 'txtfile', then under HKCR/txtfile you should have something like:
 
HKCR/txtfile/shellex/ContextMenuHandlers/{some clsid for the extension}
 
do you have these keys?

QuestionAn Item under a Sub Menu does not trigger event attached to it but triggers event attached to next onememberMember 290279323 Jan '13 - 3:08 
Hello
 
First of all, thanks for this great code. I needed to use your code since windows 8 does not support multilevel item in SendTo which was supported in Windows XP, but I had a problem in certain situation. When using your code, I needed to add a subitem before adding the actual menu items in order to obtain a tree like structure. What I mean was a structure like this:
 
Context Menu->
______________SubMenu->
_______________________Run Menu 1 Job
_______________________Run Menu 2 Job
 
What happens is when I click the first item of 'SubMenu' that is 'Run Menu 1 Job', it does not trigger the event attached to 'Run Menu 1 Job' (itemCountLinesv1), but it triggers the event of the second item that is the event of 'Run Menu 2 Job' (itemCountLinesv2). And when I click the second item of 'SubMenu' that is 'Run Menu 2 Job', it does not trigger its event (itemCountLinesv2). I tried this with up to six items under SubMenu, the pattern does not change, always triggers the next one's event except the last item wich does not trigger anything. I could not resolve this, would you be able to help me (For testing, I used both VS2008,.NET4.0 SP1 in 32 bit XP and VS2012 with first update,.NET4.5 in 64 bit Windows8; no difference).
 
Kind Regards
 
Ozcan
 
<pre lang="cs">//  Create the menu strip.

        protected override ContextMenuStrip CreateMenu()
        {
            //  Create the menu strip.
            var menu = new ContextMenuStrip();
 
            ToolStripMenuItem SubMenu;
 
            SubMenu = new ToolStripMenuItem("SubMenu");
            menu.Items.Add(SubMenu);
            
            var itemCountLinesv1 = new ToolStripMenuItem
            {
                Text = "Run Menu 1 Job",
            };
            itemCountLinesv1.Click += SubMenuItemClickv1;
            SubMenu.DropDownItems.Add(itemCountLinesv1);
 
            var itemCountLinesv2 = new ToolStripMenuItem
            {
                Text = "Run Menu 2 Job",
            };
            itemCountLinesv2.Click += SubMenuItemClickv2;
            SubMenu.DropDownItems.Add(itemCountLinesv2);
			
            return menu;
        }

AnswerRe: An Item under a Sub Menu does not trigger event attached to it but triggers event attached to next onemvpDave Kerr24 Jan '13 - 11:37 
Hi,
 
I believe you have found a bug, I think that what is happening is that the ID used to identify the context menu items when they're created is taking into account the submenu item:
 
CM
-SM (id = 0
-- Item 1 (id = 1)
-- Item 2 (id = 2)
 
and when the system asks for id 1 to be called, the context menu base class is going 'what menu items have I got' and seeing
 
--Item 1 (id = 0)
--Item 2 (id = 1)
 
i.e. its not counting the parent item.
 
I've raised a bug, http://sharpshell.codeplex.com/workitem/1004[^] expect the fix in version 1.4, which will be released by Sunday Smile | :)
 
Thanks for letting me know about this issue, I'll re-post when its resolved!

AnswerIssue ResolvedmvpDave Kerr26 Jan '13 - 3:56 
SharpShell v1.4 resolves this issue:
 
http://sharpshell.codeplex.com/releases[^]
 
Thanks for letting me know about it Smile | :)

GeneralRe: Issue ResolvedmemberMember 290279327 Jan '13 - 22:08 
Thanks for your help. It is working
GeneralRe: Issue ResolvedmvpDave Kerr27 Jan '13 - 23:03 
My pleasure, glad it's working now Smile | :)

QuestionMultiple extensionmemberPascal Ganaye18 Jan '13 - 7:50 
Is it possible to make an extension that work with both folder and files.
For example a context menu : 'zip and email' should be working on both files and folder.
AnswerRe: Multiple extensionmvpDave Kerr24 Jan '13 - 11:31 
Hi,
 
I would have to double check and get back to you, you'd need to decorate the class with:
 
[COMServerAssocation(AssociationType.Folder)]
 
and
 
[COMServerAssociation(AssocicationType.UnknownFiles)]
 
and I think that at the moment, multiple association attributes cannot be used. But I'll get back to you on that one shortly.

GeneralRe: Multiple extensionmemberPascal Ganaye25 Jan '13 - 6:47 
Great
GeneralMy vote of 5memberRugbyLeague17 Jan '13 - 4:05 
Very useful, thank you
GeneralRe: My vote of 5mvpDave Kerr17 Jan '13 - 4:05 
No probs, glad you've found it useful Smile | :)

QuestionVery Nice!memberring_017 Jan '13 - 0:21 
Namaste!
Thanks for the nice little library. I hope this will save some frustration of creating Shell extension in C++!. A Five for sure!
Man having 1 bit brain with parity error

AnswerRe: Very Nice!mvpDave Kerr17 Jan '13 - 1:11 
Namaste and thanks very much!

GeneralMy vote of 5memberEdo Tzumer8 Jan '13 - 20:21 
Shell extensions articles like this one are in dire need,
 
We all want other guys to do the hard work for us Wink | ;)
GeneralRe: My vote of 5mvpDave Kerr8 Jan '13 - 21:54 
Glad you like it - I'm in the same boat, every time I write an extension I don't want to have to write the plumbing again and again!

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 5 Mar 2013
Article Copyright 2013 by Dave Kerr
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid