Пример #1
0
        /// <summary>
        /// Obtains system folder icon</summary>
        /// <param name="size">Specifies large or small icons</param>
        /// <param name="folderType">Specifies open or closed FolderType</param>
        /// <returns>System folder icon</returns>
        public static 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();

            // Flag SHGFI_USEFILEATTRIBUTES prevents SHGetFileInfo() from attempting
            // to access the path specified by the first argument.  However, passing
            // some sort of non-null string is still required, or the call will fail.
            Shell32.SHGetFileInfo("C:\\Windows",
                                  Shell32.FILE_ATTRIBUTE_DIRECTORY,
                                  ref shfi,
                                  (uint)System.Runtime.InteropServices.Marshal.SizeOf(shfi),
                                  flags);

            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);
        }