Example #1
0
        /// <summary>
        /// Used to access system folder icons.
        /// </summary>
        /// <param name="size">Specify large or small icons.</param>
        /// <param name="folderType">Specify open or closed FolderType.</param>
        /// <returns>System.Drawing.Icon</returns>
        private Icon GetFolderIcon(IconSize size, FolderType folderType)
        {
            // Need to add size check, although errors generated at present!
            uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;

            if (FolderType.Open == folderType)
            {
                flags += Shell32.SHGFI_OPENICON;
            }

            if (IconSize.Small == size)
            {
                flags += Shell32.SHGFI_SMALLICON;
            }
            else
            {
                flags += Shell32.SHGFI_LARGEICON;
            }

            // Get the folder icon
            Shell32.SHFILEINFO shfi     = new Shell32.SHFILEINFO();
            IntPtr             hSuccess = Shell32.SHGetFileInfo("_unknown",
                                                                Shell32.FILE_ATTRIBUTE_DIRECTORY,
                                                                ref shfi,
                                                                (uint)Marshal.SizeOf(shfi),
                                                                flags);

            if (hSuccess != IntPtr.Zero)
            {
                Icon.FromHandle(shfi.hIcon); // Load the icon from an HICON handle

                // Now clone the icon, so that it can be successfully stored in an ImageList
                Icon icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

                User32.DestroyIcon(shfi.hIcon); // Cleanup
                return(icon);
            }
            return(null);
        }
Example #2
0
        private Icon GetFileIcon(string name, bool isFolder, IconSize size, bool linkOverlay)
        {
            //by extension
            string key = Utils.GetSafeExtension(name);

            if (isFolder)
            {
                key += "-folder";
            }
            if (linkOverlay)
            {
                key += "-overlay";
            }
            int  s    = size == IconSize.Small ? 16 : 32;
            Icon icon = _iconCache.Get(key, s);

            if (null == icon)
            {
                Shell32.SHFILEINFO shfi = new Shell32.SHFILEINFO();
                uint flags = Shell32.SHGFI_ICON | Shell32.SHGFI_USEFILEATTRIBUTES;

                if (linkOverlay)
                {
                    flags += Shell32.SHGFI_LINKOVERLAY;
                }

                /* Check the size specified for return. */
                if (IconSize.Small == size)
                {
                    flags += Shell32.SHGFI_SMALLICON;
                }
                else
                {
                    flags += Shell32.SHGFI_LARGEICON;
                }

                uint fileAttributes = 0;
                if (isFolder)
                {
                    fileAttributes = Shell32.FILE_ATTRIBUTE_DIRECTORY;
                }
                else
                {
                    fileAttributes = Shell32.FILE_ATTRIBUTE_NORMAL;
                }

                IntPtr hSuccess = Shell32.SHGetFileInfo(name,
                                                        fileAttributes,
                                                        ref shfi,
                                                        (uint)Marshal.SizeOf(shfi),
                                                        flags);
                if (hSuccess != IntPtr.Zero)
                {
                    // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
                    icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();
                    _iconCache.Put(key, icon, s);
                    // Release icon handle
                    User32.DestroyIcon(shfi.hIcon);
                }
            }
            return(icon);
        }