2012-02-15 66 views
0

如果它是一個愚蠢的問題,我請你諒解。但我真的需要知道如何使用我在下面發佈的代碼。我從表格複製了它。我想實現文件夾/文件圖標疊加。所以在搜索時,我找到了下面的代碼。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 
    } 

請如果有人能解釋給我。我有一種情況,我必須爲用戶在特殊文件夾下創建的文件和文件夾實施圖標疊加。

感謝

+0

代碼的哪些部分不明白? – 2012-02-15 06:37:36

+0

我只想知道,我怎樣才能使用這段代碼來實現圖標疊加?我的意思是,我有一個特殊的文件夾,如果我在該特殊文件夾中放置任何文件或文件夾,應該在每個文件/文件夾中應用一個圖標,就像在SVN中一樣。爲此,我該如何使用此代碼? – 2012-02-15 06:41:08

回答

0

(編輯:固定REGSVR32到regasm,對不起我的錯)

這段代碼有三個主要部分: 1)爲IShellIconOverlayIdentifier的互操作簽名,由外殼所使用的接口添加圖標疊加。 2)MyOverlay,該接口的實現,添加(我猜測)「normalIcon.ico」作爲覆蓋。

3)(這是重要部分)代碼註冊和註銷MyOverlay作爲註冊表中的外殼擴展。

我不太熟悉圖標疊加的細節,但似乎要啓用它,您需要調用Register方法。因爲它是標有[ComRegisterFunction],我猜你需要做的,從管理權限在命令提示符下,爲呼籲

C:\> regasm <path to your DLL> 

應該叫註冊。同樣用regasm /u <path>來卸載。試一試。

+0

@Averver - 謝謝。我已經註冊了DLL,但沒有發生任何事情。我用regasm註冊它。我可以去regsvr32嗎? – 2012-02-15 06:39:59

+0

我試圖用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

+1

你是對的,regasm是正確的工具,我的壞。您可以通過檢查註冊方法中提到的註冊表項並在那裏看到正確的GUID來確保它已正確註冊。 可能有許多原因,爲什麼它現在不工作。現在您的主機進程是explorer.exe嘗試加載您的DLL。 可以explorer.exe找到所有相關的依賴項? 是否可以有另一個使用不同版本的.NET Framework的託管外殼擴展? 您應該嘗試使用ProcessExplorer之類的工具來確保您的DLL已加載到explorer.exe,並嘗試連接一個調試器。 – 2012-02-15 07:29:53