Example #1
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);
        }
    }
Example #2
0
        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;
            }
        }
Example #3
0
        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);
        }
Example #4
0
        private static Icon GetStockIcon(int id, SHGSI flags)
        {
            SHSTOCKICONINFO info = new SHSTOCKICONINFO();
            info.cbSize = Marshal.SizeOf(typeof(SHSTOCKICONINFO));
            if (SHGetStockIconInfo(id, flags, ref info) == 0)
                return Icon.FromHandle(info.hIcon);

            return null;
        }
Example #5
0
        public static Image GetShieldImage(SHGSI flags)
        {
            using (Icon icon = GetShieldIcon(flags))
            {
                if (icon == null)
                    return null;

                return icon.ToBitmap();
            }
        }
Example #6
0
    /// <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);
    }
Example #7
0
        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));
        }
Example #8
0
        public static Image GetShieldImage(SHGSI flags)
        {
            using (var icon = GetShieldIcon(flags))
            {
                if (icon == null)
                {
                    return(null);
                }

                return(icon.ToBitmap());
            }
        }
Example #9
0
        private static Icon GetStockIcon(int id, SHGSI flags)
        {
            var info = new SHSTOCKICONINFO();

            info.cbSize = Marshal.SizeOf(typeof(SHSTOCKICONINFO));
            if (SHGetStockIconInfo(id, flags, ref info) == 0)
            {
                return(Icon.FromHandle(info.hIcon));
            }

            return(null);
        }
Example #10
0
        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);
        }
Example #11
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();
                Win32.DestroyIcon(iconPointer);
                return(iconCopy);
            }
            else
            {
                return(null);
            }
        }
Example #12
0
 public static extern Int32 SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
Example #13
0
 public static extern int SHGetStockIconInfo(SIID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
Example #14
0
 public static Icon GetShieldIcon(SHGSI flags)
 {
     return GetStockIcon(SHIELD, flags);
 }
Example #15
0
 internal static extern Int32 SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
Example #16
0
 private static IntPtr GetShellIconPointer(SHSTOCKICONID StockIconID, SHGSI IconOptions)
 {
     return(IntPtr.Zero);
 }
Example #17
0
 private static extern int SHGetStockIconInfo(int siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
Example #18
0
 private static extern int SHGetStockIconInfo(int siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
Example #19
0
 public static Icon GetShieldIcon(SHGSI flags)
 {
     return(GetStockIcon(SHIELD, flags));
 }
Example #20
0
 public static extern Int32 SHGetStockIconInfo(int siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);
 public static extern HRESULT SHGetStockIconInfo(SHSTOCKICONID siid, SHGSI uFlags, ref SHSTOCKICONINFO psii);