Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Clipz - A Friendly Introduction to the Windows 7 Taskbar Features

, 17 Dec 2009 CPOL
An overview of the Windows 7 taskbar features, and how to use then in your own applications.
Clipz.zip
Clipz
bin
Debug
Clipz.csproj.user
CopyIcon.ico
DeleteIcon.ico
Model
Native Methods
Properties
Settings.settings
Resources
Audio.png
Clipboard.png
NextIcon.ico
PreviousIcon.ico
Utility
Core
AppRestartRecovery
Dialogs
Common
TaskDialogs
Interop
AppRestartRecovery
Dialogs
NetworkList
PowerManagement
TaskDialogs
NetworkList
PowerManagement
Properties
PropertySystem
SafeHandles
Libraries
StructureMap.dll
Shell
Common
CommonFileDialogs
Controls
Design
ShellObjects.cd
ShellThumbnailClassDiagram.cd
DesktopWindowManager
ExplorerBrowser
ExplorerBrowserDiagram.cd
Interop
Common
Dialogs
ExplorerBrowser
KnownFolders
PropertySystem
StockIcons
Taskbar
KnownFolders
Properties
PropertySystem
StockIcons
Taskbar
//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)

Share

About the Author

TylerBrinks
Web Developer PageLabs
United States United States
I'm the founder of PageLabs, a web-based performance and SEO optimization site.

Give your site a boost in performance, even take a free speed test!
 
http://www.pagelabs.com
Follow on   Twitter

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150224.1 | Last Updated 17 Dec 2009
Article Copyright 2009 by TylerBrinks
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid