private Icon getIcon(bool large) { // Get icon index and path: int iconIndex = 0; StringBuilder iconPath = new StringBuilder(260, 260); if (linkA == null) { linkW.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } else { linkA.GetIconLocation(iconPath, iconPath.Capacity, out iconIndex); } string iconFile = iconPath.ToString(); // If there are no details set for the icon, then we must use // the shell to get the icon for the target: if (iconFile.Length == 0) { // Use the FileIcon object to get the icon: FileIcon.SHGetFileInfoConstants flags = FileIcon.SHGetFileInfoConstants.SHGFI_ICON | FileIcon.SHGetFileInfoConstants.SHGFI_ATTRIBUTES; if (large) { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_LARGEICON; } else { flags = flags | FileIcon.SHGetFileInfoConstants.SHGFI_SMALLICON; } FileIcon fileIcon = new FileIcon(Target, flags); return(fileIcon.ShellIcon); } else { // Use ExtractIconEx to get the icon: IntPtr[] hIconEx = new IntPtr[1] { IntPtr.Zero }; int iconCount = 0; if (large) { iconCount = UnManagedMethods.ExtractIconEx(iconFile, iconIndex, hIconEx, null, 1); } else { iconCount = UnManagedMethods.ExtractIconEx(iconFile, iconIndex, null, hIconEx, 1); } // If success then return as a GDI+ object Icon icon = null; if (hIconEx[0] != IntPtr.Zero) { icon = Icon.FromHandle(hIconEx[0]); //UnManagedMethods.DestroyIcon(hIconEx[0]); } return(icon); } }
private Icon getIcon(bool large) { int piIcon = 0; StringBuilder pszIconPath = new StringBuilder(260, 260); if (this.linkA == null) { this.linkW.GetIconLocation(pszIconPath, pszIconPath.Capacity, out piIcon); } else { this.linkA.GetIconLocation(pszIconPath, pszIconPath.Capacity, out piIcon); } string lpszFile = pszIconPath.ToString(); if (lpszFile.Length == 0) { FileIcon.SHGetFileInfoConstants flags = FileIcon.SHGetFileInfoConstants.SHGFI_ATTRIBUTES | FileIcon.SHGetFileInfoConstants.SHGFI_ICON; flags = !large ? (flags | FileIcon.SHGetFileInfoConstants.SHGFI_SMALLICON) : (flags | FileIcon.SHGetFileInfoConstants.SHGFI_LARGEICON); return(new FileIcon(this.Target, flags).ShellIcon); } IntPtr[] phIconLarge = new IntPtr[] { IntPtr.Zero }; if (large) { UnManagedMethods.ExtractIconEx(lpszFile, piIcon, phIconLarge, null, 1); } else { UnManagedMethods.ExtractIconEx(lpszFile, piIcon, null, phIconLarge, 1); } Icon icon = null; if (phIconLarge[0] != IntPtr.Zero) { icon = Icon.FromHandle(phIconLarge[0]); } return(icon); }
/// <summary> /// Constructs a new instance of the FileIcon class /// and retrieves the information specified in the /// flags. /// </summary> /// <param name="fileName">The filename to get information /// for</param> /// <param name="flags">The flags to use when extracting the /// icon and other shell information.</param> public FileIcon(string fileName, FileIcon.SHGetFileInfoConstants flags) { this.fileName = fileName; this.flags = flags; GetInfo(); }
public FileIcon(bool folder, string fileName, FileIcon.SHGetFileInfoConstants flags) { this.fileName = fileName; this.flags = flags; GetInfo(folder); }