c#中的图标叠加

时间:2012-02-15 06:25:49

标签: c#

请原谅,如果这是一个愚蠢的问题。但我真的需要知道如何使用我在下面发布的代码。我从表格中复制了它。我想实现文件夹/文件图标覆盖。所以在搜索时,我找到了下面的代码。

public sealed class ShellInterop
    {

        private ShellInterop()
        {
        }
        [DllImport("shell32.dll")]
        public static extern void SHChangeNotify(int eventID, uint flags, IntPtr item1, IntPtr item2);

    }

    [ComVisible(false)]
    [ComImport]
    [Guid("0C6C4200-C589-11D0-999A-00C04FD655E1")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IShellIconOverlayIdentifier
    {

        [PreserveSig]
        int IsMemberOf([MarshalAs(UnmanagedType.LPWStr)]string path, uint attributes);

        [PreserveSig]
        int GetOverlayInfo(
            IntPtr iconFileBuffer,
            int iconFileBufferSize,
            out int iconIndex,
            out uint flags);

        [PreserveSig]
        int GetPriority(out int priority);
    }

    [ComVisible(true)]
    [Guid("B8FA9E43-38E6-4654-8A13-FF905AD22CE5")]
    public class MyIconOverlay : IShellIconOverlayIdentifier
    {

        #region IShellIconOverlayIdentifier Members
        public int IsMemberOf(string path, uint attributes)
        {
            //Show everything with icon overlay
            return 0; // S_OK
        }

        public int GetOverlayInfo(IntPtr iconFileBuffer, int
        iconFileBufferSize,
        out int iconIndex, out uint flags)
        {

            System.Diagnostics.Debug.WriteLine(string.Format("GetOverlayInfo::{0}", iconFileBuffer));

            System.Diagnostics.Debug.WriteLine(string.Format("GetOverlayInfo::{0}", iconFileBufferSize));
            string fname = @"c:\NormalIcon.ico";

            int bytesCount = System.Text.Encoding.Unicode.GetByteCount(fname);

            System.Diagnostics.Debug.WriteLine(string.Format("GetOverlayInfo::{0}", bytesCount));

            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(fname);

            if (bytes.Length + 2 < iconFileBufferSize)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    Marshal.WriteByte(iconFileBuffer, i, bytes[i]);
                }
                //write the "\0\0"
                Marshal.WriteByte(iconFileBuffer, bytes.Length, 0);
                Marshal.WriteByte(iconFileBuffer, bytes.Length + 1, 0);
            }

            iconIndex = 0;
            flags = 1; // ISIOI_ICONFILE
            return 0; // S_OK
        }


        public int GetPriority(out int priority)
        {
            priority = 0; // 0-100 (0 is highest priority)
            return 0; // S_OK
        }
        #endregion

        #region Registry
        [ComRegisterFunction]
        public static void Register(Type t)
        {
            RegistryKey rk = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\_"
            + t.Name);
            rk.SetValue(string.Empty, t.GUID.ToString("B").ToUpper());
            rk.Close();
            ShellInterop.SHChangeNotify(0x08000000, 0, IntPtr.Zero, IntPtr.Zero);
        }

        [ComUnregisterFunction]
        public static void Unregister(Type t)
        {
            Registry.LocalMachine.DeleteSubKeyTree(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\ShellIconOverlayIdentifiers\_"
            + t.Name);
            ShellInterop.SHChangeNotify(0x08000000, 0, IntPtr.Zero, IntPtr.Zero);
        }
        #endregion
    }

如果有人能向我解释,请。我有一种情况,我必须为用户在特殊文件夹下创建的文件和文件夹实现图标叠加。

由于

1 个答案:

答案 0 :(得分:0)

(编辑:修改regsvr32到regasm,抱歉我的错误)

此代码有三个主要部分: 1)IShellIconOverlayIdentifier的互操作签名,shell是用来添加图标叠加的接口。 2)MyOverlay,该接口的一个实现,添加(我猜)“normalIcon.ico”作为叠加。

3)(这是重要的部分)代码,用于在注册表中注册和注销MyOverlay作为shell扩展。

我不太熟悉图标叠加的细节,但似乎要启用此功能,您需要调用Register方法。由于它标有[ComRegisterFunction],我猜你所要做的就是从具有管理员权限的命令提示符调用

C:\> regasm <path to your DLL>

应该叫Register。同样要卸载regasm /u <path>。试一试。