private void Refresh() { var flags = GetFlags(); if (curFlags == flags && curId == Identifier) { return; } if (hIcon != IntPtr.Zero) { DestroyIcon(hIcon); hIcon = default; } var info = SHSTOCKICONINFO.Default; var hr = SHGetStockIconInfo(curId = Identifier, curFlags = flags, ref info); // If we get an error, return null as the icon requested might not be supported on the current system if (hr == HRESULT.E_INVALIDARG) { throw new InvalidOperationException("Invalid identifier."); } else if (hr.Succeeded) { hIcon = info.hIcon; location = new IconLocation(info.szPath, info.iIcon); systemImageIndex = info.iSysImageIndex; } else { location = null; systemImageIndex = 0; } }
/// <summary> /// Gets the (stock) Icon associated to the specified ID. /// </summary> /// <param name="StockIconID">Icon ID among the defined Stock ones.</param> /// <returns>The (stock) Icon. If no Icon were found, Null is returned.</returns> /// <remarks>WARNING ! Caller is responsible of calling Dispose() on the returned Icon.</remarks> public static Icon GetSystemIcon(SHSTOCKICONID stockIconID, SHGSI iconOptions) { IntPtr iconPointer = GetShellIconPointer(stockIconID, iconOptions); if (iconPointer != IntPtr.Zero) { Icon actualIcon = Icon.FromHandle(iconPointer); Icon iconCopy = (System.Drawing.Icon)actualIcon.Clone(); actualIcon.Dispose(); DestroyIcon(iconPointer); /* * Honestly, I'm unsure of what I'm doing here :-( * If I get rid of either actualIcon or iconCopy, * and don't make a copy of it, I get a 0x0 Icon. * If I don't call DestroyIcon(h), I get a memory leak. * I highly doubt a memory leak won't occur even with the trick above, * but heh! >:-D honestly I don't care since * everything related to Icon retrival in Windows * appears to me a very bad design pattern in the first place. */ return(iconCopy); } else { return(null); } }
/// <summary>Creates a new StockIcon instance with the specified identifer and options.</summary> /// <param name="id">A value that identifies the icon represented by this instance.</param> /// <param name="size">A value that indicates the size of the stock icon.</param> /// <param name="isLinkOverlay">A bool value that indicates whether the icon has a link overlay.</param> /// <param name="isSelected">A bool value that indicates whether the icon is in a selected state.</param> public StockIcon(SHSTOCKICONID id, ShellIconType size = ShellIconType.Large, bool isLinkOverlay = false, bool isSelected = false) { Identifier = id; LinkOverlay = isLinkOverlay; Selected = isSelected; Size = size; }
public static Icon GetSystemIcon(SHSTOCKICONID icon, IconSize size = IconSize.Unspecified) { var info = new SHSTOCKICONINFO(); info.cbSize = (uint)unchecked (Marshal.SizeOf(info)); var flags = SHGSI.SHGSI_ICON; if (size == IconSize.Small) { flags |= SHGSI.SHGSI_SMALLICON; } else if (size == IconSize.Large) { flags |= SHGSI.SHGSI_LARGEICON; } var r = SHGetStockIconInfo(icon, flags, ref info); if (r != 0) { return(null); } return(Icon.FromHandle(info.hIcon)); }
private void SetIcon(MessageBoxImage image) { SHSTOCKICONID iconId = image switch { MessageBoxImage.Error => SHSTOCKICONID.SIID_ERROR, MessageBoxImage.Question => SHSTOCKICONID.SIID_HELP, MessageBoxImage.Warning => SHSTOCKICONID.SIID_WARNING, MessageBoxImage.Information => SHSTOCKICONID.SIID_INFO, _ => 0 }; if (iconId != 0) { SHSTOCKICONINFO sii = new SHSTOCKICONINFO { cbSize = (uint)Marshal.SizeOf(typeof(SHSTOCKICONINFO)) }; Marshal.ThrowExceptionForHR(SHGetStockIconInfo(iconId, SHGSI.SHGSI_ICON, ref sii)); m_iconPtr = sii.hIcon; IconSource = LoadBitmap(System.Drawing.Icon.FromHandle(m_iconPtr).ToBitmap()); IsIconVisible = true; } else { m_iconPtr = IntPtr.Zero; IconSource = null; IsIconVisible = false; } }
public static Bitmap GetStockIcon(SHSTOCKICONID stockIconId, bool largeIcon) { SHSTOCKICONINFO info = new SHSTOCKICONINFO(); info.ZeroMemory(); info.cbSize = (uint)Marshal.SizeOf(info); SHGSI flags = SHGSI.Icon; if (largeIcon) { flags |= SHGSI.LargeIcon; } else { flags |= SHGSI.SmallIcon; } int result = SHGetStockIconInfo(stockIconId, flags, ref info); if (result != 0) { return(null); } Icon icon = Icon.FromHandle(info.hIcon); Bitmap bitmap = icon.ToBitmap(); DestroyIcon(info.hIcon); return(bitmap); }
private static Icon GetCachedIcon(string name, SHSTOCKICONID type, uint size) { if (!_cache.ContainsKey(name)) { _cache.Add(name, GetStockIcon(type, size)); } return(_cache[name]); }
/// <summary> /// Initialize a new instance of the <see cref="StockIcon" /> class /// to the specified stock icon ID, stock icon size, overlay flag and selected flag. /// </summary> /// <param name="stockIconId"></param> /// <param name="size"></param> /// <param name="isLinkOverlay"></param> /// <param name="isSelected"></param> internal StockIcon(SHSTOCKICONID stockIconId, StockIconSize size, bool isLinkOverlay, bool isSelected) { this.Id = stockIconId; this.Size = StockIconSize.Large; this.LinkOverlay = isLinkOverlay; this.Selected = isSelected; this.Size = size; }
public static IntPtr GetIcon(SHSTOCKICONID identifier, SHSTOCKICONFLAGS flags) { SHSTOCKICONINFO info = new SHSTOCKICONINFO(); info.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(SHSTOCKICONINFO))); Marshal.ThrowExceptionForHR(SHGetStockIconInfo(identifier, flags, ref info)); return(info.hIcon); }
/// <summary> /// Gets a Windows stock icon. Useful as an alternative to the SystemIcons class. /// </summary> /// <param name="eIconId">Id to indicate which stock icon to fetch.</param> internal static Icon GetStockIcon(SHSTOCKICONID eIconId) { SHSTOCKICONINFO sii = new SHSTOCKICONINFO { cbSize = (uint)Marshal.SizeOf(typeof(SHSTOCKICONINFO)) }; Marshal.ThrowExceptionForHR(SHGetStockIconInfo(eIconId, SHGSI.SHGSI_ICON, ref sii)); return(Icon.FromHandle(sii.hIcon)); }
private static Icon Load(SHSTOCKICONID stockIconId, SHGSI gsi) { SHSTOCKICONINFO iconInfo = new SHSTOCKICONINFO { cbSize = SHSTOCKICONINFO_SIZE, }; SHGetStockIconInfo(stockIconId, SHGSI.SHGSI_ICON | gsi, ref iconInfo); return(Icon.FromHandle(iconInfo.hIcon)); }
/// <summary> /// Gets the Pointer to the (stock) Icon associated to the specified ID. /// </summary> /// <param name="StockIconID">Icon ID among the defined Stock ones.</param> /// <returns>The Pointer to the retrieved Icon. If no Icon were found, an empty Pointer is returned.</returns> private static IntPtr GetShellIconPointer(SHSTOCKICONID StockIconID, SHGSI IconOptions) { SHSTOCKICONINFO StkIconInfo = new SHSTOCKICONINFO(); StkIconInfo.cbSize = Convert.ToUInt32(Marshal.SizeOf(typeof(SHSTOCKICONINFO))); if (SHGetStockIconInfo(StockIconID, IconOptions, StkIconInfo) == 0) { return(StkIconInfo.hIcon); } return(IntPtr.Zero); }
public static System.Drawing.Icon GetSmallIcon(SHSTOCKICONID id) { SHSTOCKICONINFO shinfo = new SHSTOCKICONINFO(); shinfo.cbSize = (UInt32)Marshal.SizeOf(shinfo); SHGetStockIconInfo(id, SHGSI.SHGSI_ICON | SHGSI.SHGSI_SMALLICON, ref shinfo); Icon ret = Icon.FromHandle(shinfo.hIcon); //DestroyIcon(shinfo.hIcon); return(ret); }
public static Icon GetStockIcon(SHSTOCKICONID id, SHGSI flags) { SHSTOCKICONINFO info = new SHSTOCKICONINFO(); info.cbSize = (uint)Marshal.SizeOf(typeof(SHSTOCKICONINFO)); if (SHGetStockIconInfo(id, flags, ref info) == 0) { return(Icon.FromHandle(info.hIcon)); } return(null); }
public static Icon GetStockIcon(SHSTOCKICONID iconId, IconSize iconSize) { var shsii = new SHSTOCKICONINFO(); shsii.cbSize = (uint)Marshal.SizeOf(shsii); SHGetStockIconInfo(iconId, SHGSI.SHGSI_ICON | (iconSize == IconSize.Small ? SHGSI.SHGSI_SMALLICON : SHGSI.SHGSI_LARGEICON), ref shsii); var icon = (Icon)Icon.FromHandle(shsii.hIcon).Clone(); DestroyIcon(shsii.hIcon); return(icon); }
private static Icon GetStockIcon(SHSTOCKICONID type, uint size) { var info = new SHSTOCKICONINFO(); info.cbSize = (uint)Marshal.SizeOf(info); SHGetStockIconInfo((uint)type, SHGFI_ICON | size, ref info); var icon = (Icon)Icon.FromHandle(info.hIcon).Clone(); // Get a copy that doesn't use the original handle DestroyIcon(info.hIcon); // Clean up native icon to prevent resource leak return(icon); }
/// <summary> /// Gets the (stock) Icon associated to the specified ID. /// </summary> /// <param name="StockIconID">Icon ID among the defined Stock ones.</param> /// <returns>The (stock) Icon. If no Icon were found, Null is returned.</returns> /// <remarks>WARNING ! Caller is responsible of calling Dispose() on the returned Icon.</remarks> public static Icon GetSystemIcon(SHSTOCKICONID stockIconID, SHGSI iconOptions) { IntPtr iconPointer = GetShellIconPointer(stockIconID, iconOptions); if (iconPointer != IntPtr.Zero) { Icon actualIcon = Icon.FromHandle(iconPointer); Icon iconCopy = (System.Drawing.Icon)actualIcon.Clone(); actualIcon.Dispose(); Win32.DestroyIcon(iconPointer); return(iconCopy); } else { return(null); } }
public static ImageSource GetSystemIcon(SHSTOCKICONID stockIconID, bool small) { //if (IsRunningOnNix() || Environment.OSVersion.Version < Version.Parse("6.0")) //{ // switch (stockIconID) // { // case SHSTOCKICONID.SIID_DOCNOASSOC: return BuiltinResources.SIID_DOCNOASSOC; // case SHSTOCKICONID.SIID_FOLDERBACK: return BuiltinResources.SIID_FOLDERBACK; // case SHSTOCKICONID.SIID_FOLDEROPEN: return BuiltinResources.SIID_FOLDEROPEN; // default: return null; // } //} //else { var flags = SHGSI.SHGSI_ICON; if (small) { flags |= SHGSI.SHGSI_SMALLICON; } else { flags |= SHGSI.SHGSI_LARGEICON; } SHSTOCKICONINFO stkIconInfo = new SHSTOCKICONINFO(); stkIconInfo.cbSize = (uint)(Marshal.SizeOf(typeof(SHSTOCKICONINFO))); IntPtr iconPointer = Win32.SHGetStockIconInfo(stockIconID, flags, ref stkIconInfo) == 0 ? stkIconInfo.hIcon : IntPtr.Zero; if (iconPointer != IntPtr.Zero) { using (var icon = Icon.FromHandle(iconPointer)) { var rawIcon = (Icon)icon.Clone(); var imageSource = System.Windows.Interop.Imaging.CreateBitmapSourceFromHIcon(rawIcon.Handle, new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions()); return(imageSource); } } else { return(null); } } }
public static extern void SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
internal static extern Int32 SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
private static IntPtr GetShellIconPointer(SHSTOCKICONID StockIconID, SHGSI IconOptions) { return(IntPtr.Zero); }
public static extern int SHGetStockIconInfo(SHSTOCKICONID siid, uint uFlags, ref SHSTOCKICONINFO psii);
public static extern HRESULT SHGetStockIconInfo(SHSTOCKICONID siid, uint uFlags, void *psii);
/// <summary> /// Get the <see cref="StockIcon" />. /// </summary> /// <param name="stockIconId">Stock icon ID.</param> /// <returns></returns> private static StockIcon GetStockIcon(SHSTOCKICONID stockIconId) { return(new StockIcon(stockIconId, DefaultSize, DefaultLinkOverlay, DefaultSelectedState)); }
internal static extern HRESULT SHGetStockIconInfo( SHSTOCKICONID identifier, UInt32 flags, ref SHSTOCKICONINFO info);
private static extern int SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
public static extern int SHGetStockIconInfo(SHSTOCKICONID siid, SHSTOCKICONFLAGS uFlags, ref SHSTOCKICONINFO info);
public static extern Int32 SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
private static extern HRESULT SHGetStockIconInfo(SHSTOCKICONID siid, SHGetStockIconInfoFlags uFlags, ref SHSTOCKICONINFO psii);