Ejemplo n.º 1
0
        public static FileIcon FromFile(string FileName, SHGetFileInfoConstants Flags = Flags_Default)
        {
            var ret = default(FileIcon);

            if (Win32.SHGetFileInfo(FileName, 0, (uint)(Flags), out var shfi) == IntPtr.Zero)
            {
                var ErrorNumber  = Win32.GetLastError();
                var ErrorMessage = Win32.FormatMessage(FormatMessageFlags.FROM_SYSTEM | FormatMessageFlags.IGNORE_INSERTS, IntPtr.Zero, ErrorNumber, 0, IntPtr.Zero);

                var ExceptionMessage = $@"Error #{ErrorNumber}: {ErrorMessage}";
                throw new Exception(ExceptionMessage);
            }
            else
            {
                ret = new FileIcon()
                {
                    TypeName    = shfi.szTypeName,
                    DisplayName = shfi.szDisplayName,
                    ShellIcon   = shfi.hIcon == IntPtr.Zero ? null : System.Drawing.Icon.FromHandle(shfi.hIcon)
                };
            }

            return(ret);
        }
Ejemplo n.º 2
0
        Icon getIcon(bool large)
        {
            // Get icon index and path:
            var iconPath = new StringBuilder(Win32.MAX_PATH, Win32.MAX_PATH);

            Wrapped.GetIconLocation(iconPath, iconPath.Capacity, out var iconIndex);

            var 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:
                var flags =
                    SHGetFileInfoConstants.SHGFI_ICON |
                    SHGetFileInfoConstants.SHGFI_ATTRIBUTES;
                if (large)
                {
                    flags = flags | SHGetFileInfoConstants.SHGFI_LARGEICON;
                }
                else
                {
                    flags = flags | SHGetFileInfoConstants.SHGFI_SMALLICON;
                }
                var fileIcon = FileIcon.FromFile(Target, flags);
                return(fileIcon.ShellIcon);
            }
            else
            {
                // Use ExtractIconEx to get the icon:
                var hIconEx = new IntPtr[1] {
                    IntPtr.Zero
                };
                var iconCount = 0;
                if (large)
                {
                    iconCount = Win32.ExtractIconEx(
                        iconFile,
                        iconIndex,
                        hIconEx,
                        null,
                        1);
                }
                else
                {
                    iconCount = Win32.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);
            }
        }