2017-11-27 13:11:20 -05:00
using System ;
using System.Drawing ;
2018-09-22 00:35:34 -04:00
using Vanara.PInvoke ;
2017-11-27 13:11:20 -05:00
using static Vanara . PInvoke . ComCtl32 ;
using static Vanara . PInvoke . Shell32 ;
2019-08-27 18:03:21 -04:00
using static Vanara . PInvoke . User32 ;
2017-11-27 13:11:20 -05:00
namespace Vanara.Windows.Shell
{
2018-04-05 15:25:38 -04:00
/// <summary>Used to determine the size of the icon returned by <see cref="ShellImageList.GetSystemIcon"/>.</summary>
public enum ShellImageSize
{
/// <summary>
/// The image size is normally 32x32 pixels. However, if the Use large icons option is selected from the Effects section of the Appearance tab in
/// Display Properties, the image is 48x48 pixels.
/// </summary>
Large = SHIL . SHIL_LARGE ,
/// <summary>The image is the Shell standard small icon size of 16x16, but the size can be customized by the user.</summary>
Small = SHIL . SHIL_SMALL ,
/// <summary>The image is the Shell standard extra-large icon size. This is typically 48x48, but the size can be customized by the user.</summary>
ExtraLarge = SHIL . SHIL_EXTRALARGE ,
/// <summary>Windows Vista and later. The image is normally 256x256 pixels.</summary>
Jumbo = SHIL . SHIL_JUMBO
}
2018-09-22 00:35:34 -04:00
/ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Developer Note : Keep these methods synchronized with identical methods in Vanara . Windows . Forms : : Vanara . Extensions . IconExtension
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /
2018-04-05 15:25:38 -04:00
/// <summary>Represents the System Image List holding images for all shell icons.</summary>
2017-11-27 13:11:20 -05:00
public static class ShellImageList
{
2018-04-05 15:25:38 -04:00
/// <summary>Gets the system icon for the given file name or extension.</summary>
/// <param name="fileNameOrExtension">The file name or extension.</param>
/// <param name="iconSize">Size of the icon.</param>
/// <returns>An <see cref="Icon"/> instance if found; otherwise <see langword="null"/>.</returns>
public static Icon GetSystemIcon ( string fileNameOrExtension , ShellImageSize iconSize = ShellImageSize . Large )
2017-11-27 13:11:20 -05:00
{
2018-04-05 15:25:38 -04:00
var shfi = new SHFILEINFO ( ) ;
2018-09-22 00:35:34 -04:00
var hImageList = ( HIMAGELIST ) SHGetFileInfo ( fileNameOrExtension , 0 , ref shfi , SHFILEINFO . Size , SHGFI . SHGFI_SYSICONINDEX | ( iconSize = = ShellImageSize . Small ? SHGFI . SHGFI_SMALLICON : 0 ) ) ;
if ( hImageList . IsNull ) return null ;
2018-04-05 15:25:38 -04:00
if ( iconSize < = ShellImageSize . Small )
2018-09-22 00:35:34 -04:00
return ImageList_GetIcon ( hImageList , shfi . iIcon , IMAGELISTDRAWFLAGS . ILD_TRANSPARENT ) . ToIcon ( ) ;
2018-04-05 15:25:38 -04:00
SHGetImageList ( ( SHIL ) iconSize , typeof ( IImageList ) . GUID , out var il ) . ThrowIfFailed ( ) ;
2018-09-22 00:35:34 -04:00
return il . GetIcon ( shfi . iIcon , IMAGELISTDRAWFLAGS . ILD_TRANSPARENT ) . ToIcon ( ) ;
2017-11-27 13:11:20 -05:00
}
2018-04-05 15:25:38 -04:00
/// <summary>Gets the Shell icon for the given file name or extension.</summary>
/// <param name="fileNameOrExtension">The file name or extension .</param>
2018-11-28 14:33:55 -05:00
/// <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>
2018-04-05 15:25:38 -04:00
/// <returns>An <see cref="Icon"/> instance if found; otherwise <see langword="null"/>.</returns>
public static Icon GetFileIcon ( string fileNameOrExtension , ShellIconType iconType = ShellIconType . Large )
2017-11-27 13:11:20 -05:00
{
2018-04-05 15:25:38 -04:00
var shfi = new SHFILEINFO ( ) ;
2018-09-22 00:35:34 -04:00
var ret = SHGetFileInfo ( fileNameOrExtension , 0 , ref shfi , SHFILEINFO . Size , SHGFI . SHGFI_USEFILEATTRIBUTES | SHGFI . SHGFI_ICON | ( SHGFI ) iconType ) ;
2018-04-05 15:25:38 -04:00
if ( ret = = IntPtr . Zero )
2018-09-22 00:35:34 -04:00
ret = SHGetFileInfo ( fileNameOrExtension , 0 , ref shfi , SHFILEINFO . Size , SHGFI . SHGFI_ICON | ( SHGFI ) iconType ) ;
2018-11-19 23:18:50 -05:00
return ret = = IntPtr . Zero ? null : shfi . hIcon . ToIcon ( ) ;
2017-11-27 13:11:20 -05:00
}
}
}