Example #1
0
        private IntPtr GetHIcon()
        {
            // Create our internal flag to pass to the native method
            StockIconsNativeMethods.StockIconOptions flags = StockIconsNativeMethods.StockIconOptions.Handle;

            // Based on the current settings, update the flags
            if (CurrentSize == StockIconSize.Small)
            {
                flags |= StockIconsNativeMethods.StockIconOptions.Small;
            }
            else if (CurrentSize == StockIconSize.ShellSize)
            {
                flags |= StockIconsNativeMethods.StockIconOptions.ShellSize;
            }
            else
            {
                flags |= StockIconsNativeMethods.StockIconOptions.Large;  // default
            }

            if (Selected)
            {
                flags |= StockIconsNativeMethods.StockIconOptions.Selected;
            }

            if (LinkOverlay)
            {
                flags |= StockIconsNativeMethods.StockIconOptions.LinkOverlay;
            }

            // Create a StockIconInfo structure to pass to the native method.
            StockIconsNativeMethods.StockIconInfo info = new StockIconsNativeMethods.StockIconInfo();
            info.StuctureSize = (UInt32)Marshal.SizeOf(typeof(StockIconsNativeMethods.StockIconInfo));

            // Pass the struct to the native method
            HResult hr = StockIconsNativeMethods.SHGetStockIconInfo(identifier, 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.Ok)
            {
                if (hr == HResult.InvalidArguments)
                {
                    throw new InvalidOperationException(
                              string.Format(System.Globalization.CultureInfo.InvariantCulture,
                                            LocalizedMessages.StockIconInvalidGuid,
                                            identifier));
                }

                return(IntPtr.Zero);
            }

            // If we succeed, return the HIcon
            return(info.Handle);
        }
Example #2
0
        private IntPtr GetHIcon()
        {
            // Create our internal flag to pass to the native method
            StockIconsNativeMethods.StockIconOptions flags = StockIconsNativeMethods.StockIconOptions.Handle;

            // Based on the current settings, update the flags
            if (CurrentSize == StockIconSize.Small)
            {
                flags |= StockIconsNativeMethods.StockIconOptions.Small;
            }
            else if (CurrentSize == StockIconSize.ShellSize)
            {
                flags |= StockIconsNativeMethods.StockIconOptions.ShellSize;
            }
            else
            {
                flags |= StockIconsNativeMethods.StockIconOptions.Large;  // default
            }

            if (Selected)
            {
                flags |= StockIconsNativeMethods.StockIconOptions.Selected;
            }

            if (LinkOverlay)
            {
                flags |= StockIconsNativeMethods.StockIconOptions.LinkOverlay;
            }

            // Create a StockIconInfo structure to pass to the native method.
            StockIconsNativeMethods.StockIconInfo info = new StockIconsNativeMethods.StockIconInfo();
            info.StuctureSize = (UInt32)Marshal.SizeOf(typeof(StockIconsNativeMethods.StockIconInfo));

            // Pass the struct to the native method
            HResult hr = StockIconsNativeMethods.SHGetStockIconInfo(identifier, 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.Ok)
            {
                if (hr == HResult.InvalidArguments)
                {
                    throw new InvalidOperationException(
                        string.Format(System.Globalization.CultureInfo.InvariantCulture,
                        LocalizedMessages.StockIconInvalidGuid,
                        identifier));
                }

                return IntPtr.Zero;
            }

            // If we succeed, return the HIcon
            return info.Handle;
        }