コード例 #1
0
ファイル: IconHelper.cs プロジェクト: xknife-erian/nknife
        /// <summary>
        ///     Returns an icon for a given file - indicated by the name parameter.
        /// </summary>
        /// <param name="name">Pathname for file.</param>
        /// <param name="size">Large or small</param>
        /// <param name="linkOverlay">Whether to include the link icon</param>
        /// <returns>System.Drawing.Icon</returns>
        public static Icon GetFileIcon(string name, IconSize size, bool linkOverlay)
        {
            var shfi  = new NKnife.Win.Api.Shell32.Shfileinfo();
            var flags = NKnife.Win.Api.Shell32.SHGFI_ICON | NKnife.Win.Api.Shell32.SHGFI_USE_FILE_ATTRIBUTES;

            if (linkOverlay)
            {
                flags += NKnife.Win.Api.Shell32.SHGFI_LINK_OVERLAY;
            }

            /* Check the size specified for return. */
            if (IconSize.Small == size)
            {
                flags += NKnife.Win.Api.Shell32.SHGFI_SMALL_ICON;
            }
            else
            {
                flags += NKnife.Win.Api.Shell32.SHGFI_LARGE_ICON;
            }

            NKnife.Win.Api.Shell32.SHGetFileInfo(name,
                                                 NKnife.Win.Api.Shell32.FILE_ATTRIBUTE_NORMAL,
                                                 ref shfi,
                                                 (uint)Marshal.SizeOf(shfi),
                                                 flags);

            // Copy (clone) the returned icon to a new object, thus allowing us to clean-up properly
            var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

            Api.User32.DestroyIcon(shfi.hIcon);// Cleanup
            return(icon);
        }
コード例 #2
0
ファイル: IconHelper.cs プロジェクト: xknife-erian/nknife
        /// <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>
        public static Icon GetFolderIcon(IconSize size, FolderType folderType)
        {
            // Need to add size check, although errors generated at present!
            var flags = NKnife.Win.Api.Shell32.SHGFI_ICON | NKnife.Win.Api.Shell32.SHGFI_USE_FILE_ATTRIBUTES;

            if (FolderType.Open == folderType)
            {
                flags += NKnife.Win.Api.Shell32.SHGFI_OPEN_ICON;
            }

            if (IconSize.Small == size)
            {
                flags += NKnife.Win.Api.Shell32.SHGFI_SMALL_ICON;
            }
            else
            {
                flags += NKnife.Win.Api.Shell32.SHGFI_LARGE_ICON;
            }

            // Get the folder icon
            var shfi = new NKnife.Win.Api.Shell32.Shfileinfo();

            NKnife.Win.Api.Shell32.SHGetFileInfo(null,
                                                 NKnife.Win.Api.Shell32.FILE_ATTRIBUTE_DIRECTORY,
                                                 ref shfi,
                                                 (uint)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
            var icon = (Icon)Icon.FromHandle(shfi.hIcon).Clone();

            Api.User32.DestroyIcon(shfi.hIcon); // Cleanup
            return(icon);
        }