Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

.NET Shell Extensions - Shell Drop Handlers

, 19 Jan 2013
Rapidly create Shell Drop Handler Extensions using .NET
SharpShell_Core_Library.zip
SharpShell_Source_Code.zip
SharpShell Source Code
Dependencies
Apex
Apex.WinForms.dll
SharpShell
packages
Apex.1.5.0
Apex.1.5.0.nupkg
lib
net35
Apex.dll
net40
Apex.dll
sl30
Apex.Silverlight.dll
sl3-wp
Apex.WP7.dll
sl40
Apex.Silverlight4.dll
sl4-wp71
Apex.WP7.dll
sl50
Apex.Silverlight.dll
NUnit.2.6.2
lib
nunit.framework.dll
NUnit.2.6.2.nupkg
Samples
ContextMenu
CountLinesExtension
CountLinesExtension.csproj.vspscc
Key.snk
Properties
Resources
CountLines.bmp
DropHandler
XsdDropHandler
ExampleFiles
Key.snk
Properties
XsdDropHandler.csproj.vspscc
IconHandler
DllIconHandler
DllIconHandler.csproj.vspscc
Key.snk
Properties
Resources
ManagedDll.ico
NativeDll.ico
IconOverlayHandler
ReadOnlyFileIconOverlayHandler
Key.snk
Properties
ReadOnlyFileIconOverlayHandler.csproj.vspscc
Resources
ReadOnly.ico
InfoTipHandler
FolderInfoTipHandler
FolderInfoTipHandler.csproj.vspscc
Key.snk
Properties
PropertySheet
DrivePropertySheet
DrivePropertySheet.csproj.vspscc
Key.snk
Properties
TextFilePropertySheet
Key.snk
Properties
TextFilePropertySheet.csproj.vspscc
SharpShell
Attributes
Diagnostics
Extensions
Interop
Key.snk
NativeBridge
SharpShellNativeBridge32.dll
SharpShellNativeBridge64.dll
Properties
ServerRegistration
SharpContextMenu
SharpDropHandler
SharpIconHandler
SharpIconOverlayHandler
SharpInfoTipHandler
SharpPropertySheet
SharpShell.csproj.vspscc
SharpShellNativeBridge
SharpShellNativeBridge.vcxproj.filters
SharpShellNativeBridge.vcxproj.user
SharpShellNativeBridge.vcxproj.vspscc
Tools
ServerInspector
Properties
Settings.settings
ServerInspector.csproj.vspscc
Servers
ServerManager
About
LogView
Properties
Settings.settings
Resources
ChangeYourView.png
ContextMenu.png
Icon.png
openHS.png
PlayHS.png
PropertySheet.png
View_Details.png
View_LargeIcons.png
View_List.png
View_SmallIcons.png
View_Tiles.png
ServerDetails
ServerManager.csproj.vspscc
SharpShell.ico
TestShell
ShellExtensionManager
Properties
Settings.settings
ShellExtensionManager.csproj.vspscc
SharpShell_Tools.zip
SharpShell Tools
Apex.dll
Apex.WinForms.dll
ServerInspector.exe
ServerManager.exe
SharpShell.dll
System.Windows.Interactivity.dll
using System;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Text;
using SharpShell.Attributes;
using SharpShell.Interop;

namespace SharpShell.SharpIconHandler
{
    /// <summary>
    /// The SharpIconHandler is the base class for SharpShell servers that provide
    /// custom Icon Handlers.
    /// </summary>
    [ServerType(ServerType.ShellIconHandler)]
    public abstract class SharpIconHandler : PersistFileServer, IExtractIcon
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SharpIconHandler"/> class.
        /// </summary>
        protected SharpIconHandler()
        {
            //  By default, we WON'T cache icons.
            DontCacheIcons = true;
        }

       #region Implementation of IExtractIcon

        /// <summary>
        /// Gets the location and index of an icon.
        /// </summary>
        /// <param name="uFlags">One or more of the following values. This parameter can also be NULL.</param>
        /// <param name="szIconFile">A pointer to a buffer that receives the icon location. The icon location is a null-terminated string that identifies the file that contains the icon.</param>
        /// <param name="cchMax">The size of the buffer, in characters, pointed to by pszIconFile.</param>
        /// <param name="piIndex">A pointer to an int that receives the index of the icon in the file pointed to by pszIconFile.</param>
        /// <param name="pwFlags">A pointer to a UINT value that receives zero or a combination of the following value</param>
        /// <returns></returns>
        int IExtractIcon.GetIconLocation(GILInFlags uFlags, StringBuilder szIconFile, int cchMax, out int piIndex, out GILOutFlags pwFlags)
        {
            //  Log this key event.
            Log(string.Format("Getting icon location icon for {0}", SelectedItemPath));

            //  We're always going to return by handle.
            //  This will cause the 'Extract' function to be called.
            pwFlags = GILOutFlags.GIL_NOTFILENAME;
            if(DontCacheIcons)
                pwFlags |= GILOutFlags.GIL_DONTCACHE;

            //  No need for an index.
            piIndex = 0;

            //  Return success.
            return WinError.S_OK;
        }

        /// <summary>
        /// Extracts an icon image from the specified location.
        /// </summary>
        /// <param name="pszFile">A pointer to a null-terminated string that specifies the icon location.</param>
        /// <param name="nIconIndex">The index of the icon in the file pointed to by pszFile.</param>
        /// <param name="phiconLarge">A pointer to an HICON value that receives the handle to the large icon. This parameter may be NULL.</param>
        /// <param name="phiconSmall">A pointer to an HICON value that receives the handle to the small icon. This parameter may be NULL.</param>
        /// <param name="nIconSize">The desired size of the icon, in pixels. The low word contains the size of the large icon, and the high word contains the size of the small icon. The size specified can be the width or height. The width of an icon always equals its height.</param>
        /// <returns>
        /// Returns S_OK if the function extracted the icon, or S_FALSE if the calling application should extract the icon.
        /// </returns>
        int IExtractIcon.Extract(string pszFile, uint nIconIndex, out IntPtr phiconLarge, out IntPtr phiconSmall, uint nIconSize)
        {
            //  Log this key event.
            Log(string.Format("Extracting icon for {0}", SelectedItemPath));

            //  Get the small and large icon sizes.
            var iconSizeSmall = (uint)User32.HighWord((int)nIconSize);
            var iconSizeLarge = (uint)User32.LowWord((int)nIconSize);

            //  Default the icons to null.
            phiconLarge = IntPtr.Zero;
            phiconSmall = IntPtr.Zero;
            
            try
            {
                //  Set the large and small icons.
                phiconLarge = GetIcon(false, iconSizeLarge).Handle;
                phiconSmall = GetIcon(true, iconSizeSmall).Handle;
            }
            catch (Exception exception)
            {
                //  Log the exception.
                LogError("An exception occured extracting icons.", exception);
            }
            
            //  Return succes.
            return WinError.S_OK;
        }

        #endregion

        /// <summary>
        /// Gets an icon of a specific size from a set of icons.
        /// </summary>
        /// <param name="icon">The icon.</param>
        /// <param name="size">The size required.</param>
        /// <returns>The icon that is cloest to the size provided.</returns>
        protected static Icon GetIconSpecificSize(Icon icon, Size size)
        {
            //  Create a memory stream.
            using (var memoryStream = new MemoryStream())
            {
                //  Save the icon to the stream, then seek to the origin.
                icon.Save(memoryStream);
                memoryStream.Position = 0;

                //  Load the icon from the stream, but specify the size.
                return new Icon(memoryStream, size);
            }
        }
        
        /// <summary>
        /// Gets the icon.
        /// </summary>
        /// <param name="smallIcon">if set to <c>true</c> provide a small icon.</param>
        /// <param name="iconSize">Size of the icon.</param>
        /// <returns>The icon for the file.</returns>
        protected abstract Icon GetIcon(bool smallIcon, uint iconSize);

        /// <summary>
        /// Gets or sets a value indicating whether to force icons from this handler 
        /// to not be cached by the shell.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if icons mustn't be cached; otherwise, <c>false</c>.
        /// </value>
        public bool DontCacheIcons { get; protected set; }
    }
}

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)

Share

About the Author

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

| Advertise | Privacy | Mobile
Web04 | 2.8.140902.1 | Last Updated 19 Jan 2013
Article Copyright 2013 by Dave Kerr
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid