Introduction

Shell Context Menus are Shell Extensions that add to the context menu opened when the user right clicks on a Shell Item. A Shell Item might be a file, folder, drive, network share or so on.

Shell Context Menus are fully supported in SharpShell. This section describes how to implement them.

Creating Shell Context Menus

Create a C# class library project. Ensure the class library is signed with a strong name. Add a reference to 'SharpShell' (the SharpShell core library is available from the Downloads page, or can be found on Nuget with a search for 'SharpShell'). If the library has been referenced manually, also add references to:

  • System.Drawing
  • System.Windows.Forms


If the library has been installed with Nuget, these references will have been added automatically.

Create a class that derives from SharpContextMenu. Ensure the class has the 'ComVisible' attribute set to true. Also add a 'COMServerAssociation' attribute to specify what types of shell item this context menu should be available for. You can find full documentation on this attribute on the page COM Server Associations.

At this stage, you should have a class that looks like this:

[ComVisible(true)]
[COMServerAssociation(AssociationType.ClassOfExtension, ".txt")]
public class ExampleShellExtension : SharpContextMenu
{
} 



Finally, there are two override that must be implemented.

CanShowMenu

This function is called to determine whether the shell extension should be visible for a set of items. The items selected by the user are stored in the property SelectedItemPaths.

protected override bool CanShowMenu()
{
    //  Depending on the files in 'SelectedItemPaths' you might not show the menu.
    return true;
}



CreateMenu

This function is called to actually create the context menu strip to add to the shell context menu. An example is below:

protected override ContextMenuStrip CreateMenu()
        {
            //  Create the menu strip.
            var menu = new ContextMenuStrip();

            //  Create an item.
            var itemCountLines = new ToolStripMenuItem
                                     {
                                         Text = "Do something ...",
                                         Image = Properties.Resources.CountLines
                                     };

            //  Add a handler for the click event.
            itemCountLines.Click += (sender, args) => MessageBox.Show("Do something");

            //  Add the item to the context menu.
            menu.Items.Add(itemCountLines);

            //  Return the menu.
            return menu;
        }

Icons

As a note, if the source of a context menu item's icon is a file format that supports transparency, such as *.png, then the icon itself will render correctly with transparency, as the screenshot below shows (left side with the mouse out, right side with the mouse over):

 

Last edited Feb 11, 2013 at 4:44 PM by dwmkerr, version 2

Comments

No comments yet.