Click here to Skip to main content
15,896,468 members
Articles / Programming Languages / Visual Basic

Windows 7 new features: Step by step in VB.NET and C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (88 votes)
31 Aug 2010CPOL9 min read 180K   4.7K   211  
Explaining all the new must have features in Windows 7 to make your application look shiny and professional, like the new features of the task bar and more.
//Copyright (c) Microsoft Corporation.  All rights reserved.

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media.Imaging;
using MS.WindowsAPICodePack.Internal;

namespace Microsoft.WindowsAPICodePack.Shell
{
    /// <summary>
    /// Represents a standard system icon.
    /// </summary>
    public class StockIcon : IDisposable
    {
        #region Private Members

        private StockIconIdentifier identifier = StockIconIdentifier.Application;
        private StockIconSizes currentSize = StockIconSizes.Large;
        private bool linkOverlay = false;
        private bool selected = false;
        private bool invalidateIcon = true;
        private IntPtr hIcon = IntPtr.Zero;

        #endregion

        #region Public Constructors

        /// <summary>
        /// Creates a new StockIcon instance with the specified identifer, default size 
        /// and no link overlay or selected states.
        /// </summary>
        /// <param name="id">A value that identifies the icon represented by this instance.</param>
        public StockIcon(StockIconIdentifier id)
        {
            identifier = id;
            invalidateIcon = true;
        }

        /// <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(StockIconIdentifier id, StockIconSizes size, bool isLinkOverlay, bool isSelected)
        {
            identifier = id;
            linkOverlay = isLinkOverlay;
            selected = isSelected;
            currentSize = size;
            invalidateIcon = true;
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets or sets a value indicating whether the icon appears selected.
        /// </summary>
        /// <value>A <see cref="System.Boolean"/> value.</value>
        public bool Selected
        {
            get { return selected; }
            set
            {
                selected = value;
                invalidateIcon = true;
            }
        }

        /// <summary>
        /// Gets or sets a value that cotrols whether to put a link overlay on the icon.
        /// </summary>
        /// <value>A <see cref="System.Boolean"/> value.</value>
        public bool LinkOverlay
        {
            get { return linkOverlay; }
            set
            {
                linkOverlay = value;
                invalidateIcon = true;
            }
        }

        /// <summary>
        /// Gets or sets a value that controls the size of the Stock Icon.
        /// </summary>
        /// <value>A <see cref="Microsoft.WindowsAPICodePack.Shell.StockIconSizes"/> value.</value>
        public StockIconSizes CurrentSize
        {
            get { return currentSize; }
            set
            {
                currentSize = value;
                invalidateIcon = true;
            }
        }

        /// <summary>
        /// Gets or sets the Stock Icon identifier associated with this icon.
        /// </summary>
        public StockIconIdentifier Identifier
        {
            get { return identifier; }
            set
            {
                identifier = value;
                invalidateIcon = true;
            }
        }

        /// <summary>
        /// Gets the icon image in <see cref="System.Drawing.Bitmap"/> format. 
        /// </summary>
        public Bitmap Bitmap
        {
            get
            {
                UpdateHIcon();

                return hIcon != IntPtr.Zero ? Bitmap.FromHicon(hIcon) : null;
            }
        }

        /// <summary>
        /// Gets the icon image in <see cref="System.Windows.Media.Imaging.BitmapSource"/> format. 
        /// </summary>
        public BitmapSource BitmapSource
        {
            get
            {
                UpdateHIcon();

                if (hIcon != IntPtr.Zero)
                    return Imaging.CreateBitmapSourceFromHIcon(hIcon, Int32Rect.Empty, null);
                else
                    return null;
            }
        }

        /// <summary>
        /// Gets the icon image in <see cref="System.Drawing.Icon"/> format.
        /// </summary>
        public Icon Icon
        {
            get
            {
                UpdateHIcon();

                return hIcon != IntPtr.Zero ? Icon.FromHandle(hIcon) : null;
            }
        }

        #endregion

        #region Private Methods

        private void UpdateHIcon()
        {
            if (invalidateIcon)
            {
                if (hIcon != IntPtr.Zero)
                    CoreNativeMethods.DestroyIcon(hIcon);

                hIcon = GetHIcon();

                invalidateIcon = false;
            }
        }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Globalization", "CA1305:SpecifyIFormatProvider", MessageId = "System.String.Format(System.String,System.Object)")]
        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 == StockIconSizes.Small)
                flags |= StockIconsNativeMethods.StockIconOptions.Small;
            else if (CurrentSize == StockIconSizes.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.S_OK)
            {
                if (hr == HRESULT.E_INVALIDARG)
                    throw new ArgumentException(string.Format("The Stock Icon identifier given is invalid ({0})", identifier));
                else
                    return IntPtr.Zero;
            }

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

        #endregion

        #region IDisposable Members

        /// <summary>
        /// Release the native and managed objects
        /// </summary>
        /// <param name="disposing">Indicates that this is being called from Dispose(), rather than the finalizer.</param>
        public void Dispose(bool disposing)
        {
            if (disposing)
            {
                // dispose managed resources here
            }

            // Unmanaged resources
            if (hIcon != IntPtr.Zero)
                CoreNativeMethods.DestroyIcon(hIcon);
        }

        /// <summary>
        /// Release the native objects
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// 
        /// </summary>
        ~StockIcon()
        {
            Dispose(false);
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer IDS
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions