Ejemplo n.º 1
0
        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.ShgfiIcon |
                                                        FileIcon.ShGetFileInfoConstants.ShgfiAttributes;
                if (large)
                {
                    flags = flags | FileIcon.ShGetFileInfoConstants.ShgfiLargeicon;
                }
                else
                {
                    flags = flags | FileIcon.ShGetFileInfoConstants.ShgfiSmallicon;
                }
                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);
            }
        }
Ejemplo n.º 2
0
 /// <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();
 }