/// <summary> /// Generates the Icon which is used for folders /// </summary> /// <param name="_iconSize"></param> /// <param name="_folderType"></param> /// <returns></returns> private static System.Drawing.Icon GetFolderIcon(IconSize _iconSize, FolderType _folderType) { // Need to add size check, although errors generated at present! API.SHGFI flags = API.SHGFI.ICON | API.SHGFI.USEFILEATTRIBUTES; if (FolderType.Open == _folderType) { flags |= API.SHGFI.OPENICON; } switch (_iconSize) { case IconSize.Small: flags |= API.SHGFI.SMALLICON; break; case IconSize.Large: flags |= API.SHGFI.LARGEICON; break; } // Get the folder icon var shfi = new API.SHFILEINFO(); var res = API.SHGetFileInfo(@"C:\Windows", API.FileAttributes.ATTRIBUTE_DIRECTORY, ref shfi, (uint)Marshal.SizeOf(shfi), flags); if (res == IntPtr.Zero) { throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); } // Load the icon from an HICON handle System.Drawing.Icon.FromHandle(shfi.hIcon); // Now clone the icon, so that it can be successfully used var icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone(); // Cleanup API.DestroyIcon(shfi.hIcon); return(icon); }
/// <summary> /// Generates the Icon which is used for folders /// </summary> /// <param name="_iconSize"></param> /// <param name="_folderType"></param> /// <returns></returns> private static System.Drawing.Icon GetFolderIcon(IconSize _iconSize, FolderType _folderType) { // Need to add size check, although errors generated at present! API.SHGFI flags = API.SHGFI.ICON | API.SHGFI.USEFILEATTRIBUTES; if (FolderType.Open == _folderType) { flags |= API.SHGFI.OPENICON; } switch (_iconSize) { case IconSize.Small: flags |= API.SHGFI.SMALLICON; break; case IconSize.Large: flags |= API.SHGFI.LARGEICON; break; } // Get the folder icon var shfi = new API.SHFILEINFO(); var res = API.SHGetFileInfo(@"C:\Windows", API.FileAttributes.ATTRIBUTE_DIRECTORY, ref shfi, (uint)Marshal.SizeOf(shfi), flags); if (res == IntPtr.Zero) throw Marshal.GetExceptionForHR(Marshal.GetHRForLastWin32Error()); // Load the icon from an HICON handle System.Drawing.Icon.FromHandle(shfi.hIcon); // Now clone the icon, so that it can be successfully used var icon = (System.Drawing.Icon)System.Drawing.Icon.FromHandle(shfi.hIcon).Clone(); // Cleanup API.DestroyIcon(shfi.hIcon); return icon; }