/// <summary>Extracts an icon image from the specified location.</summary> /// <param name="exIcon">The <see cref="IExtractIconA"/> instance.</param> /// <param name="pszFile">A pointer to a null-terminated string that specifies the icon location.</param> /// <param name="nIconIndex">The index of the icon in the file pointed to by pszFile.</param> /// <param name="nIconSize"> /// The desired size of the icon, in pixels. The size specified can be the width or height. The width of an icon always equals /// its height. /// </param> /// <param name="phicon">A pointer to an HICON value that receives the handle to the icon. This parameter may be NULL.</param> /// <returns>Returns S_OK if the function extracted the icon, or S_FALSE if the calling application should extract the icon.</returns> public static HRESULT Extract(this IExtractIconA exIcon, string pszFile, uint nIconIndex, ushort nIconSize, out SafeHICON phicon) { if (exIcon is null) { throw new ArgumentNullException(nameof(exIcon)); } var sz = nIconSize > 16 ? Macros.MAKELONG(nIconSize, 0) : Macros.MAKELONG(0, nIconSize); unsafe { HICON h1 = default; var hr = nIconSize > 16 ? exIcon.Extract(pszFile, nIconIndex, &h1, null, sz) : exIcon.Extract(pszFile, nIconIndex, null, &h1, sz); phicon = h1 == default ? null : new SafeHICON((IntPtr)h1); return(hr); } }
/// <summary>Gets the icon for the item using the specified characteristics.</summary> /// <param name="iei">The IExtractIconA from which to retrieve the icon.</param> /// <param name="imgSz">The width, in pixels, of the icon.</param> /// <param name="hico">The resulting icon handle, on success, or <c>null</c> on failure.</param> /// <returns>The result of function.</returns> public static HRESULT LoadIconFromExtractIcon(IExtractIconA iei, ref uint imgSz, out SafeHICON hico) { var szIconFile = new StringBuilder(Kernel32.MAX_PATH); var hr = iei.GetIconLocation(GetIconLocationFlags.GIL_FORSHELL, szIconFile, szIconFile.Capacity, out var iIdx, out _); if (hr.Succeeded) { if (szIconFile.ToString() != "*") { hr = iei.Extract(szIconFile.ToString(), (uint)iIdx, (ushort)imgSz, out hico); } else { hr = LoadIconFromSystemImageList(iIdx, ref imgSz, out hico); } } else { hico = null; } return(hr); }