使用c#为图标处理程序创建Shell扩展处理程序

时间:2013-06-10 11:27:44

标签: c# shell com interface

我尝试实现IPersisFile和IExtractIcon接口来设置文件夹的自定义图标。这是我的实施:

  [ComImport(), InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
[Guid("000214FA-0000-0000-C000-000000000046")]
public interface IExtractIcon
{
    [PreserveSig]
    uint GetIconLocation(
        uint uFlags,
        /*[MarshalAs(UnmanagedType.LPTStr)]*/
        StringBuilder pszIconFile,
        uint cchMax,
        ref int piIndex,
        ref uint pwFlags);

    [PreserveSig]
    uint Extract(
        string pzsFile,
        uint nIconIndex,
        ref IntPtr phiconLarge,
        ref IntPtr phiconSmall,
        uint nIconSize);



}

/// <summary>
/// 
/// </summary>
enum UFlags : uint
{
    GIL_ASYNC =0x0020,
    GIL_DEFAULTICON =0x0040,
    GIL_FORSHELL =0x0002,
    GIL_FORSHORTCUT =0x0080,
    GIL_OPENICON =0x0001,
    GIL_CHECKSHIELD =0x0200
}

/// <summary>
/// 
/// </summary>
enum PWFlags : uint
{
    GIL_DONTCACHE =0x0010,
    GIL_NOTFILENAME =0x0008,
    GIL_PERCLASS =0x0004,
    GIL_PERINSTANCE =0x0002,
    GIL_SIMULATEDOC =0x0001,
    GIL_SHIELD =0x0200,
    GIL_FORCENOSHIELD =0x0400
}

这是代码

    [ClassInterface(ClassInterfaceType.None)]
[Guid("096F8A92-49E2-451B-8CB3-9BFD19C286FD"), ComVisible(true)]
public class IconHandler : IPersistFile, IExtractIcon
{
    #region fileds
    Guid t = new Guid("096F8A92-49E2-451B-8CB3-9BFD19C286FD");
    string FileName;
    /// <summary>
    /// Хранит путь к иконке 
    /// </summary>
    string IconLocation = @"C:\Users\root\Desktop\Tosafe2\Sync2.png";
    /// <summary>
    /// Установочный флаг
    /// </summary>
    string SmalIconLocation = @"C:\Users\root\Desktop\Tosafe2\Sync3.png";
    bool Flag;
    public IconHandler() { }
    #endregion

    #region registartion Methods

    [ComRegisterFunction]
    public static void Register(Type t)
    {
        try
        {
            NativeMethods.Register(t.GUID, "Directory", "TosafeIconExtention.IconHandler Class");
        }
        catch (Exception exe)
        {
            throw;
        }
    }

    [ComUnregisterFunction]
    public static void Unregister(Type t)
    {
        try
        {
            NativeMethods.Unregister(t.GUID, "Directory");
        }
        catch (Exception exe) { throw; }
    }


    #endregion

    #region implementation methods of  IPersistFile interface

    public void GetClassID(out Guid pClassID)
    {
        throw new NotImplementedException();
    }

    public void GetCurFile(out string ppszFileName)
    {
        throw new NotImplementedException();
    }

    public int IsDirty()
    {
        throw new NotImplementedException();
    }

    public void Load(string pszFileName, int dwMode)
    {
        FileName = pszFileName;
    }

    public void Save(string pszFileName, bool fRemember)
    {
        throw new NotImplementedException();
    }

    public void SaveCompleted(string pszFileName)
    {
        throw new NotImplementedException();
    }

    #endregion

    #region implementation methods of IExtractIcon interface

    public uint GetIconLocation(
        uint uFlags,
        /*[MarshalAs(UnmanagedType.LPTStr)]*/
        StringBuilder pszIconFile,
        uint cchMax,
        ref int piIndex,
        ref uint pwFlags)
    {
        try
        {
            string path = @"C:\Users\root\Desktop\3";
            if (FileName.Contains(path))
                Flag = true;
            else
                Flag = false;

            pwFlags = (uint)PWFlags.GIL_NOTFILENAME | (uint)PWFlags.GIL_DONTCACHE;
            return NativeMethods.S_OK;
        }
        catch (Exception exe)
        {
            return NativeMethods.S_FALSE;
        }
    }

    public uint Extract(
        string pzsFile,
        uint nIconIndex,
        ref IntPtr phiconLarge,
        ref IntPtr phiconSmall,
        uint nIconSize)
    {
        if (Flag)
        {
            if (phiconLarge != IntPtr.Zero)
            {
                Bitmap b = (Bitmap)Image.FromFile(IconLocation);
                b.MakeTransparent(b.GetPixel(0, 0));
                phiconLarge = b.GetHbitmap();
            }
            if (phiconSmall != IntPtr.Zero)
            {
                Bitmap b = (Bitmap)Image.FromFile(SmalIconLocation);
                b.MakeTransparent(b.GetPixel(0, 0));
                phiconLarge = b.GetHbitmap();
            }
            return NativeMethods.S_OK;
        }
        else
        {
            return NativeMethods.S_FALSE;
        }
    }





    #endregion
}

问题是没有调用IPersistFile :: Load方法,IExtractIcon接口的方法也是如此。请帮我解决这个问题。

0 个答案:

没有答案