Exemple #1
0
        /// <summary>
        /// Gets the index in the system image list of the icon representing
        /// the item.
        /// </summary>
        ///
        /// <param name="type">
        /// The type of icon to retrieve.
        /// </param>
        ///
        /// <param name="flags">
        /// Flags detailing additional information to be conveyed by the icon.
        /// </param>
        ///
        /// <returns></returns>
        public int GetSystemImageListIndex(ShellIconType type, ShellIconFlags flags)
        {
            SHFILEINFO info = new SHFILEINFO();

            // Patch FAB : Remove SHGFI.ICON
            // https://sourceforge.net/p/gong-shell/patches/2/
            //To eliminate GDI object leaks
            //(which prevent diplaying folder trees with more than a few thousand subfolders),

            /*
             * IntPtr result = Shell32.SHGetFileInfo(Pidl, 0, out info,
             *  Marshal.SizeOf(info),
             *  SHGFI.ICON | SHGFI.SYSICONINDEX | SHGFI.OVERLAYINDEX | SHGFI.PIDL |
             *  (SHGFI)type | (SHGFI)flags);
             */
            IntPtr result = Shell32.SHGetFileInfo(Pidl, 0, out info,
                                                  Marshal.SizeOf(info),
                                                  SHGFI.SYSICONINDEX | SHGFI.OVERLAYINDEX | SHGFI.PIDL |
                                                  (SHGFI)type | (SHGFI)flags);


            if (result == IntPtr.Zero)
            {
                throw new Exception("Error retreiving shell folder icon");
            }

            return(info.iIcon);
        }
Exemple #2
0
 /// <summary>Creates a new StockIcon instance with the specified identifer and options.</summary>
 /// <param name="id">A value that identifies the icon represented by this instance.</param>
 /// <param name="size">A value that indicates the size of the stock icon.</param>
 /// <param name="isLinkOverlay">A bool value that indicates whether the icon has a link overlay.</param>
 /// <param name="isSelected">A bool value that indicates whether the icon is in a selected state.</param>
 public StockIcon(SHSTOCKICONID id, ShellIconType size = ShellIconType.Large, bool isLinkOverlay = false, bool isSelected = false)
 {
     Identifier  = id;
     LinkOverlay = isLinkOverlay;
     Selected    = isSelected;
     Size        = size;
 }
Exemple #3
0
        public int AddShellIcon(string path, ShellIconType flags)
        {
            var info = new NativeMethods.SHFILEINFO();

            try
            {
                var imageList = NativeMethods.SHGetFileInfo(
                    path, 0, ref info, (uint)Marshal.SizeOf(info),
                    ((uint)flags | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_SYSICONINDEX)
                    & ~NativeMethods.SHGFI_ICON
                );

                if (imageList == IntPtr.Zero)
                    return -1;

                if (imageList != _handle)
                    throw new ArgumentException("Could not get image", "path");

                return info.iIcon;
            }
            finally
            {
                if (info.hIcon != IntPtr.Zero)
                    NativeMethods.DestroyIcon(info.hIcon);
            }
        }
Exemple #4
0
        public int AddShellIcon(string path, ShellIconType flags)
        {
            var info = new NativeMethods.SHFILEINFO();

            try
            {
                var imageList = NativeMethods.SHGetFileInfo(
                    path, 0, ref info, (uint)Marshal.SizeOf(info),
                    ((uint)flags | NativeMethods.SHGFI_SMALLICON | NativeMethods.SHGFI_SYSICONINDEX)
                    & ~NativeMethods.SHGFI_ICON
                    );

                if (imageList == IntPtr.Zero)
                {
                    return(-1);
                }

                if (imageList != _handle)
                {
                    throw new ArgumentException("Could not get image", "path");
                }

                return(info.iIcon);
            }
            finally
            {
                if (info.hIcon != IntPtr.Zero)
                {
                    NativeMethods.DestroyIcon(info.hIcon);
                }
            }
        }
Exemple #5
0
        /// <summary>Gets the Shell icon for the given file name or extension.</summary>
        /// <param name="fileNameOrExtension">The file name or extension .</param>
        /// <param name="iconType">Flags to specify the type of the icon to retrieve. This uses the <see cref="SHGetFileInfo(string, System.IO.FileAttributes, ref SHFILEINFO, int, SHGFI)"/> method and can only retrieve small or large icons.</param>
        /// <returns>An <see cref="Icon"/> instance if found; otherwise <see langword="null"/>.</returns>
        public static Icon GetFileIcon(string fileNameOrExtension, ShellIconType iconType = ShellIconType.Large)
        {
            var shfi = new SHFILEINFO();
            var ret  = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, SHFILEINFO.Size, SHGFI.SHGFI_USEFILEATTRIBUTES | SHGFI.SHGFI_ICON | (SHGFI)iconType);

            if (ret == IntPtr.Zero)
            {
                ret = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, SHFILEINFO.Size, SHGFI.SHGFI_ICON | (SHGFI)iconType);
            }
            return(ret == IntPtr.Zero ? null : shfi.hIcon.ToIcon());
        }
Exemple #6
0
        /// <summary>Gets the Shell icon for the given file name or extension.</summary>
        /// <param name="fileNameOrExtension">The file name or extension .</param>
        /// <param name="iconType">Flags to specify the type of the icon to retrieve. This uses the <see cref="SHGetFileInfo(string, System.IO.FileAttributes, ref SHFILEINFO, int, SHGFI)"/> method and can only retrieve small or large icons.</param>
        /// <returns>An <see cref="Icon"/> instance if found; otherwise <see langword="null"/>.</returns>
        public static Icon GetFileIcon(string fileNameOrExtension, ShellIconType iconType = ShellIconType.Large)
        {
            const SHGFI baseFlags = SHGFI.SHGFI_USEFILEATTRIBUTES | SHGFI.SHGFI_ICON;
            var         shfi      = new SHFILEINFO();
            var         ret       = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, shfiSz, baseFlags | (SHGFI)iconType);

            if (ret == IntPtr.Zero)
            {
                ret = SHGetFileInfo(fileNameOrExtension, 0, ref shfi, shfiSz, SHGFI.SHGFI_ICON | (SHGFI)iconType);
            }
            return(ret == IntPtr.Zero ? null : IconLocation.GetClonedIcon(shfi.hIcon));
        }
Exemple #7
0
        /// <summary>
        /// Gets the icon defined by the set of flags provided.
        /// </summary>
        /// <param name="iconType">Flags to specify type of the icon.</param>
        /// <returns><see cref="Icon"/> if successful; <c>null</c> otherwise.</returns>
        public Icon GetIcon(ShellIconType iconType = ShellIconType.Large)
        {
            const SHGFI baseFlags = SHGFI.SHGFI_ICON | SHGFI.SHGFI_PIDL;
            var         shfi      = new SHFILEINFO();
            var         ret       = SHGetFileInfo(PIDL, 0, ref shfi, SHFILEINFO.Size, baseFlags | SHGFI.SHGFI_USEFILEATTRIBUTES | (SHGFI)iconType);

            if (ret == IntPtr.Zero)
            {
                ret = SHGetFileInfo(PIDL, 0, ref shfi, SHFILEINFO.Size, baseFlags | (SHGFI)iconType);
            }
            return(ret == IntPtr.Zero ? null : GetClonedIcon(shfi.hIcon));
        }
Exemple #8
0
        /// <summary>
        /// Gets the index in the system image list of the icon representing
        /// the item.
        /// </summary>
        ///
        /// <param name="pidl">A pointer to a null-terminated string of maximum length MAX_PATH that contains the path and file name. Both absolute and relative paths are valid.</param>
        /// <param name="type">
        /// The type of icon to retrieve.
        /// </param>
        ///
        /// <param name="flags">
        /// Flags detailing additional information to be conveyed by the icon.
        /// </param>
        ///
        /// <returns></returns>
        public static int GetSystemImageListIndex(IntPtr pidl, ShellIconType type, ShellIconFlags flags)
        {
            var    info   = new SHFILEINFO();
            IntPtr result = Shell32.SHGetFileInfo(pidl, 0, out info, Marshal.SizeOf(info), SHGFI.Icon | SHGFI.SysIconIndex | SHGFI.OverlayIndex | SHGFI.PIDL | (SHGFI)type | (SHGFI)flags);

            if (result == IntPtr.Zero)
            {
                throw new Exception("Error retrieving shell folder icon");
            }

            User32.DestroyIcon(info.hIcon);
            return(info.iIcon);
        }
Exemple #9
0
        /// <summary>
        /// Gets the index in the system image list of the icon representing
        /// the item.
        /// </summary>
        ///
        /// <param name="type">
        /// The type of icon to retrieve.
        /// </param>
        ///
        /// <param name="flags">
        /// Flags detailing additional information to be conveyed by the icon.
        /// </param>
        ///
        /// <returns></returns>
        public int GetSystemImageListIndex(ShellIconType type, ShellIconFlags flags)
        {
            SHFILEINFO info   = new SHFILEINFO();
            IntPtr     result = Shell32.SHGetFileInfo(Pidl, 0, out info,
                                                      Marshal.SizeOf(info),
                                                      SHGFI.ICON | SHGFI.SYSICONINDEX | SHGFI.OVERLAYINDEX | SHGFI.PIDL |
                                                      (SHGFI)type | (SHGFI)flags);

            if (result == IntPtr.Zero)
            {
                throw new Exception("Error retreiving shell folder icon");
            }

            return(info.iIcon);
        }
        public int GetSystemImageListIndex(IntPtr pidl, ShellIconType type, ShellIconFlags flags)
        {
            var    options  = SHGetFileInfoOptions.Icon | SHGetFileInfoOptions.SysIconIndex | SHGetFileInfoOptions.OverlayIndex | SHGetFileInfoOptions.Pidl | SHGetFileInfoOptions.AddOverlays | (SHGetFileInfoOptions)type | (SHGetFileInfoOptions)flags;
            var    shfi     = new SHFILEINFO();
            var    shfiSize = Marshal.SizeOf(shfi.GetType());
            IntPtr retVal   = Win32Api.SHGetFileInfo(pidl, FileAttributes.None, ref shfi, shfiSize, options);

            if (shfi.hIcon != IntPtr.Zero)
            {
                Win32Api.DestroyIcon(shfi.hIcon);
            }

            if (retVal.Equals(IntPtr.Zero))
            {
                return(0);
            }
            else
            {
                return(shfi.iIcon);
            }
        }
Exemple #11
0
 /// <summary>Gets the icon defined by the set of flags provided.</summary>
 /// <param name="iconType">Flags to specify type of the icon.</param>
 /// <returns><see cref="Icon"/> if successful; <c>null</c> otherwise.</returns>
 public Icon GetIcon(ShellIconType iconType = ShellIconType.Large) => ShellImageList.GetFileIcon(FullPath, iconType);
Exemple #12
0
 /// <summary>
 /// Gets the index in the system image list of the icon representing
 /// the item.
 /// </summary>
 ///
 /// <param name="type">
 /// The type of icon to retrieve.
 /// </param>
 ///
 /// <param name="flags">
 /// Flags detailing additional information to be conveyed by the icon.
 /// </param>
 ///
 /// <returns></returns>
 public int GetSystemImageListIndex(ShellIconType type, ShellIconFlags flags) => GetSystemImageListIndex(Pidl, type, flags);
    public int GetSystemImageListIndex(IntPtr pidl, ShellIconType type, ShellIconFlags flags) {
      var info = new SHFILEINFO();
      IntPtr result = Shell32.SHGetFileInfo(pidl, 0, out info, Marshal.SizeOf(info),
                          SHGFI.Icon | SHGFI.SysIconIndex | SHGFI.OverlayIndex | SHGFI.PIDL | (SHGFI)type | (SHGFI)flags);

      if (result == IntPtr.Zero) {
        throw new Exception("Error retrieving shell folder icon");
      }

      User32.DestroyIcon(info.hIcon);
      return info.iIcon;
    }