Click here to Skip to main content
15,896,606 members
Articles / Desktop Programming / Win32

.NET Shell Extensions - Shell Info Tip Handlers

Rate me:
Please Sign up or sign in to vote.
4.90/5 (16 votes)
5 Apr 2013CPOL7 min read 54.6K   2K   44  
Rapidly create Shell Info Tip Extensions using .NET
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Windows.Forms;
using Apex.WinForms.Interop;
using Apex.WinForms.Shell;
using SharpShell;
using SharpShell.Attributes;
using SharpShell.Interop;
using SharpShell.ServerRegistration;
using SharpShell.SharpContextMenu;
using SharpShell.SharpIconHandler;
using SharpShell.SharpInfoTipHandler;

namespace ServerManager.TestShell
{
    /// <summary>
    /// The TestShellForm is a simple form that can be used to test SharpShell
    /// COM servers.
    /// </summary>
    public partial class TestShellForm : Form
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="TestShellForm" /> class.
        /// </summary>
        public TestShellForm()
        {
            InitializeComponent();

            lazyBoldFont = new Lazy<Font>(() => new Font(Font, FontStyle.Bold));

            shellTreeView.OnShellItemAdded += new TreeViewEventHandler(shellTreeView_OnShellItemAdded);
            shellListView.OnShellItemAdded += new Apex.WinForms.Controls.ListViewItemEventHandler(shellListView_OnShellItemAdded);

            //  Create the ordered view menu items.
            orderedViewMenuItems.Add(largeIconsToolStripMenuItem);
            orderedViewMenuItems.Add(toolStripMenuItemSmallIcons);
            orderedViewMenuItems.Add(listToolStripMenuItem);
            orderedViewMenuItems.Add(detailsToolStripMenuItem);
            orderedViewMenuItems.Add(tileToolStripMenuItem);

            shellListView.Columns.Add(new ColumnHeader {Text = "Name"});
        }

        void shellTreeView_OnShellItemAdded(object sender, TreeViewEventArgs e)
        {
            /*var shellItem = shellTreeView.GetShellItem(e.Node);
            if (IsServerAssociatedWithShellItem(TestIconHandler, shellItem))
            {
                DoTestIconHandler(e.Node);
            }*/
        }

        void shellListView_OnShellItemAdded(object sender, Apex.WinForms.Controls.ListViewItemEventArgs args)
        {
            var shellItem = shellListView.GetShellItem(args.Item);

            //  If the icon handler is associated with the the item, test it.
            if (IsServerAssociatedWithShellItem(TestIconHandler, shellItem))
                DoTestIconHandler(args.Item);

            //  If the info tip handler is associated with the item, test it.
            if (IsServerAssociatedWithShellItem(TestInfoTipHandler, shellItem))
                DoTestInfoTipHandler(args.Item);
        }

        private void shellTreeView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                //  Get the hit test info.
                var hitTestInfo = shellTreeView.HitTest(new Point(e.X, e.Y));

                //  If we're not hit a node, bail.
                if (hitTestInfo.Node == null)
                    return;

                //  Get the point in screen coords.
                var screenPoint = shellTreeView.PointToScreen(new Point(e.X, e.Y));

                //  Get the shell item.
                var shellItem = shellTreeView.GetShellItem(hitTestInfo.Node);

                //  Test it.
                DoTestMenu(shellItem, screenPoint.X, screenPoint.Y);
            }
        }

        private void shellListView1_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                //  Get the hit test info.
                var hitTestInfo = shellListView.HitTest(new Point(e.X, e.Y));

                //  If we're not hit a node, bail.
                if (hitTestInfo.Item == null)
                    return;

                //  Get the point in screen coords.
                var screenPoint = shellListView.PointToScreen(new Point(e.X, e.Y));

                //  Get the shell item.
                var shellItem = shellListView.GetShellItem(hitTestInfo.Item);

                //  Test it.
                DoTestMenu(shellItem, screenPoint.X, screenPoint.Y);
            }
        }

        /// <summary>
        /// Tests the context menu.
        /// </summary>
        /// <param name="item">The item.</param>
        /// <param name="x">The x.</param>
        /// <param name="y">The y.</param>
        private void DoTestMenu(ShellItem item, int x, int y)
        {
            //  If we don't have a context menu, we can bail now.
            if (TestContextMenu == null)
                return;

            //  Get the interfaces we need to test with.
            var shellExtInitInterface = (IShellExtInit) TestContextMenu;
            var contextMenuInterface = (IContextMenu) TestContextMenu;
            
            //  Try init first.
            try
            {
                //  Create the file paths.
                var filePaths = new StringCollection {item.Path};

                //  Create the data object from the file paths.
                var dataObject = new DataObject();
                dataObject.SetFileDropList(filePaths);

                //  Get the IUnknown COM interface address. Jesus .NET makes this easy.
                var dataObjectInterfacePointer = Marshal.GetIUnknownForObject(dataObject);

                //  Pass the data to the shell extension, attempt to initialise it.
                //  We must provide the data object as well as the parent folder PIDL.
                shellExtInitInterface.Initialize(item.ParentItem.PIDL, dataObjectInterfacePointer, IntPtr.Zero);
            }
            catch (Exception)
            {
                //  Not supported for the file
                return;
            }

            //  Create a native menu.
            var menuHandle = CreatePopupMenu();

            //  Build the menu.
            contextMenuInterface.QueryContextMenu(menuHandle, 0, 0, 1, 0);

            //  Track the menu.
            TrackPopupMenu(menuHandle,
                           0, x, y, 0, Handle, IntPtr.Zero);
        }

        /// <summary>
        /// The windows message pump.
        /// </summary>
        /// <param name="m">The Windows <see cref="T:System.Windows.Forms.Message" /> to process.</param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);

            //  Do we have a comamnd and a shell context menu we're testing?
            if (m.Msg == WM_COMMAND && TestContextMenu != null)
            {
                var loword = LowWord(m.WParam.ToInt32());
                var hiword = HighWord(m.WParam.ToInt32());

                //  If the hiword is 0 it's a menu command.
                if (hiword == 0)
                {
                    //  Create command info.
                    var commandInfo = new CMINVOKECOMMANDINFO();
                    commandInfo.cbSize = (uint) Marshal.SizeOf(commandInfo);
                    commandInfo.verb = new IntPtr(loword);

                    //  Get a pointer to the structure.
                    var commandInfoPointer = Marshal.AllocHGlobal(Marshal.SizeOf(commandInfo));
                    Marshal.StructureToPtr(commandInfo, commandInfoPointer, false);

                    ((IContextMenu) TestContextMenu).InvokeCommand(commandInfoPointer);
                }
            }
        }

        public static int HighWord(int number)
        {
            return ((number & 0x80000000) == 0x80000000) ?
                                                             (number >> 16) : ((number >> 16) & 0xffff);
        }

        public static int LowWord(int number)
        {
            return number & 0xffff;
        }

        private void shellListView_SelectedIndexChanged(object sender, EventArgs e)
        {
            var shellItem = shellListView.SelectedItems.Count > 0 ? shellListView.GetShellItem(shellListView.SelectedItems[0]) : null;

            if (shellItem != null)
                toolStripStatusLabelAttributes.Text = "Attributes: " + shellItem.Attributes.ToString();
        }

        private void DoTestIconHandler(ListViewItem item)
        {
            //  Get the shell item.
            try
            {
                var shellItem = shellListView.GetShellItem(item);

                IntPtr iconSmall, iconLarge;
                GetIconHandlerIcons(TestIconHandler, shellItem.Path, out iconSmall, out iconLarge);

                //  We're testing the item, so make it bold.
                item.Font = lazyBoldFont.Value;

                //  Add the icons.
                var largeIcon = Icon.FromHandle(iconLarge);
                var smallIcon = Icon.FromHandle(iconSmall);
                var newIndex = shellListView.LargeImageList.Images.Count;
                shellListView.LargeImageList.Images.Add(largeIcon);
                shellListView.SmallImageList.Images.Add(smallIcon);

                //  Set the icon.
                item.ImageIndex = newIndex;
            }
            catch (Exception)
            {
            }
        }
        
        private void GetIconHandlerIcons(SharpIconHandler iconHandler, string path, out IntPtr iconSmall, out IntPtr iconLarge)
        {
            //  Test the persist file.
            var persistFile = (IPersistFile)iconHandler;
            persistFile.Load(path, 0);

            //  Test the icon handler.
            var extractIcon = (IExtractIcon)iconHandler;
            var size = 32 + (16 << 16);
            extractIcon.Extract(path, 0, out iconLarge, out iconSmall, (uint)size);
        }

        private void DoTestInfoTipHandler(ListViewItem item)
        {
            if (TestInfoTipHandler == null)
                return;

            //  Get the shell item.
            try
            {
                var shellItem = shellListView.GetShellItem(item);

                //  Initialise the icon handler.
                var persistFileInterface = (IPersistFile)TestInfoTipHandler;
                persistFileInterface.Load(shellItem.Path, 0);

                //  Get the info tip.
                var queryInfoInterface = (IQueryInfo)TestInfoTipHandler;
                string infoTip;
                queryInfoInterface.GetInfoTip(QITIPF.QITIPF_DEFAULT, out infoTip);

                //  Set the tooltip.
                item.Font = lazyBoldFont.Value;
                item.ToolTipText = infoTip;
            }
            catch (Exception)
            {
            }
        }

        /// <summary>
        /// Determines whether a server is associated with a shell item.
        /// </summary>
        /// <param name="server">The server.</param>
        /// <param name="shellItem">The shell item.</param>
        /// <returns>
        ///   <c>true</c> if a server is associated with the shell item; otherwise, <c>false</c>.
        /// </returns>
        private bool IsServerAssociatedWithShellItem(ISharpShellServer server, ShellItem shellItem)
        {
            //  If we don't have the server, bail.
            if (server == null)
                return false;

            //  Get the associations.
            var associationType = COMServerAssociationAttribute.GetAssociationType(server.GetType());
            var associations = COMServerAssociationAttribute.GetAssociations(server.GetType());

            //  TODO: This is potentially a very useful check - maybe it should be moved into the
            //  COMServerAssociationAttribute class so that it can be reused.

            //  Based on the assocation type, we can check the shell item.
            switch (associationType)
            {
                case AssociationType.FileExtension:

                    //  TODO must be tested.
                    //  File extensions are easy to check.
                    if (shellItem.Attributes.HasFlag(SFGAOF.SFGAO_FILESYSTEM))
                    {
                        return
                            associations.Any(
                                a =>
                                string.Compare(Path.GetExtension(shellItem.DisplayName), "." + a,
                                               StringComparison.OrdinalIgnoreCase) == 0);
                    }

                    break;

                case AssociationType.ClassOfExtension:

                    //  TODO must be tested.
                    if (shellItem.Attributes.HasFlag(SFGAOF.SFGAO_FILESYSTEM))
                    {
                        //  Get our class.
                        var fileClass = ServerRegistrationManager.GetClassForExtension(Path.GetExtension(shellItem.DisplayName));

                        //  Do we match it?
                        return associations.Any(a => string.Compare(fileClass, ServerRegistrationManager.GetClassForExtension(a), StringComparison.InvariantCultureIgnoreCase) == 0);
                    }

                    break;

                case AssociationType.Class:
                    //  TODO must be tested.
                    break;

                case AssociationType.AllFiles:

                    //  TODO must be tested.
                    return shellItem.Attributes.HasFlag(SFGAOF.SFGAO_FILESYSTEM) && shellItem.IsFolder == false;

                case AssociationType.Directory:

                    //  Directories are filesystem, not streams, and folder.
                    return shellItem.Attributes.HasFlag(SFGAOF.SFGAO_FILESYSTEM) && !shellItem.Attributes.HasFlag(SFGAOF.SFGAO_STREAM) && shellItem.IsFolder;

                case AssociationType.Drive:

                    //  TODO must be tested.
                    return shellItem.Attributes.HasFlag(SFGAOF.SFGAO_STORAGEANCESTOR);

                case AssociationType.UnknownFiles:
                    //  TODO must be tested.
                    break;
                default:
                    throw new ArgumentOutOfRangeException();
            }

            return false;
        }

        private void toolStripMenuItemSmallIcons_Click(object sender, EventArgs e)
        {
            shellListView.View = View.SmallIcon;
            SetViewIndex(1);
        }

        private void largeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shellListView.View = View.LargeIcon;
            SetViewIndex(0);
        }

        private void detailsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shellListView.View = View.Details;
            SetViewIndex(3);
        }

        private void tileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shellListView.View = View.Tile;
            SetViewIndex(4);
        }

        private void listToolStripMenuItem_Click(object sender, EventArgs e)
        {
            shellListView.View = View.List;
            SetViewIndex(2);
        }

        /// <summary>
        /// The view menu items, in order.
        /// </summary>
        private List<ToolStripMenuItem> orderedViewMenuItems = new List<ToolStripMenuItem>();

        private readonly Lazy<Font> lazyBoldFont;

        private const uint WM_CONTEXTMENU = 0x007B;
        private const uint WM_COMMAND = 0x0111;


        [DllImport("User32.dll")]
        internal static extern IntPtr CreateMenu();

        [DllImport("User32.dll")]
        internal static extern IntPtr CreatePopupMenu();

        [DllImport("User32.dll")]
        internal static extern bool TrackPopupMenu(IntPtr hMenu,
                                                   uint uFlags, int x, int y, int nReserved, IntPtr hWnd, IntPtr pRect);

        [DllImport("User32.dll")]
        internal static extern int GetMenuItemCount(IntPtr hMenu);

        /// <summary>
        /// Gets or sets the test server.
        /// </summary>
        /// <value>
        /// The test server.
        /// </value>
        public ISharpShellServer TestServer { get; set; }

        /// <summary>
        /// Gets or sets the test context menu.
        /// </summary>
        /// <value>
        /// The test context menu.
        /// </value>
        public SharpContextMenu TestContextMenu { get { return TestServer as SharpContextMenu; } }

        /// <summary>
        /// Gets or sets the test icon handler.
        /// </summary>
        /// <value>
        /// The test icon handler.
        /// </value>
        public SharpIconHandler TestIconHandler { get { return TestServer as SharpIconHandler; } }

        /// <summary>
        /// Gets or sets the test info tip handler.
        /// </summary>
        /// <value>
        /// The test info tip handler.
        /// </value>
        public SharpInfoTipHandler TestInfoTipHandler { get { return TestServer as SharpInfoTipHandler; } }

        private void toolStripSplitButtonChangeYourView_ButtonClick(object sender, EventArgs e)
        {
           //  Update it.
            currentViewIndex++;
            if (currentViewIndex >= orderedViewMenuItems.Count)
                currentViewIndex = 0;

            SetViewIndex(currentViewIndex);
            orderedViewMenuItems[currentViewIndex].PerformClick();
        }

        private void SetViewIndex(int index)
        {
            currentViewIndex = index;

            //  Get the menu item to change to.
            var newItem = orderedViewMenuItems[currentViewIndex];

            //  Set the icon.
            toolStripSplitButtonChangeYourView.Image = newItem.Image;
        }

        private int currentViewIndex = 0;
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Software Developer
United Kingdom United Kingdom
Follow my blog at www.dwmkerr.com and find out about my charity at www.childrenshomesnepal.org.

Comments and Discussions