c中的图标叠加#
如果它是一个愚蠢的问题,我请你谅解。但我真的需要知道如何使用我在下面发布的代码。我从表格复制了它。我想实现文件夹/文件图标叠加。所以在搜索时,我找到了下面的代码。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
}
请如果有人能解释给我。我有一种情况,我必须为用户在特殊文件夹下创建的文件和文件夹实施图标叠加。
感谢
(编辑:固定REGSVR32到regasm,对不起我的错)
这段代码有三个主要部分: 1)为IShellIconOverlayIdentifier
的互操作签名,由外壳所使用的接口添加图标叠加。 2)MyOverlay,该接口的实现,添加(我猜测)“normalIcon.ico”作为覆盖。
3)(这是重要部分)代码注册和注销MyOverlay作为注册表中的外壳扩展。
我不太熟悉图标叠加的细节,但似乎要启用它,您需要调用Register方法。因为它是标有[ComRegisterFunction]
,我猜你需要做的,从管理权限在命令提示符下,为呼吁
C:\> regasm <path to your DLL>
应该叫注册。同样用regasm /u <path>
来卸载。试一试。
@Averver - 谢谢。我已经注册了DLL,但没有发生任何事情。我用regasm注册它。我可以去regsvr32吗? – 2012-02-15 06:39:59
我试图用regsvr32注册它。我给出的错误如下:“模块”D:\ Test \ IconOverLay_Test \ IconOverLay_Test \ bin \ Release \ IconOverLay_Test.dll“已加载,但没有找到入口点DllRegisterServer。 确保”D:\ Test \ IconOverLay_Test \ IconOverLay_Test \ bin \ Release \ IconOverLay_Test.dll“是一个有效的DLL或OCX文件,然后再试一次。” – 2012-02-15 06:45:32
你是对的,regasm是正确的工具,我的坏。您可以通过检查注册方法中提到的注册表项并在那里看到正确的GUID来确保它已正确注册。 可能有许多原因,为什么它现在不工作。现在您的主机进程是explorer.exe尝试加载您的DLL。 可以explorer.exe找到所有相关的依赖项? 是否可以有另一个使用不同版本的.NET Framework的托管外壳扩展? 您应该尝试使用ProcessExplorer之类的工具来确保您的DLL已加载到explorer.exe,并尝试连接一个调试器。 – 2012-02-15 07:29:53
代码的哪些部分不明白? – 2012-02-15 06:37:36
我只想知道,我怎样才能使用这段代码来实现图标叠加?我的意思是,我有一个特殊的文件夹,如果我在该特殊文件夹中放置任何文件或文件夹,应该在每个文件/文件夹中应用一个图标,就像在SVN中一样。为此,我该如何使用此代码? – 2012-02-15 06:41:08