Click here to Skip to main content
12,246,406 members (38,098 online)
Click here to Skip to main content

Stats

43.5K views
1.4K downloads
126 bookmarked
Posted

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
bin
Debug
Clipz.csproj.user
CopyIcon.ico
DeleteIcon.ico
Model
Native Methods
Properties
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
namespace Clipz
{
    using System;
    using System.Collections.Generic;
    using System.Drawing;
    using System.Windows.Forms;
    using StructureMap;

    public class ClipboardManager
    {
        private readonly ClipboardWrapper _wrapper = new ClipboardWrapper();
        private readonly List<ClipboardItem> _clipboardList = new List<ClipboardItem>();
        private int _currentItemIndex = -1;

        /// <summary>
        /// Initializes a new instance of the <see cref="ClipboardManager"/> class.
        /// </summary>
        public ClipboardManager()
        {
            ObjectFactory.Initialize(x => x.ForRequestedType<ClipboardItem>().AddInstances(c =>
                                       {
                                           c.OfConcreteType<AudioClipboardItem>().WithName("audio")
                                               .OnCreation(a => a.AudioStream = Clipboard.GetAudioStream());

                                           c.OfConcreteType<ImageClipboardItem>().WithName("image")
                                               .OnCreation(a => a.BitmapImage = (Bitmap)Clipboard.GetImage());                                               

                                           c.OfConcreteType<TextClipboardItem>().WithName("text")
                                               .OnCreation(a => a.OriginalString = Clipboard.GetText());                                               

                                           c.OfConcreteType<RichTextClipboardItem>().WithName("rtf")
                                               .OnCreation(a => 
                                                   {
                                                       var data = Clipboard.GetDataObject();
                                                       if(data != null)
                                                       {
                                                           a.OriginalString = (string)data.GetData(DataFormats.Rtf);
                                                       }
                                                   });                                               
                                       }));
        }

        public event EventHandler<ClipboardItemAddedEventArgs> ItemAdded;
        public event EventHandler<ClipboardItemRemovedEventArgs> ItemRemoved;
        public event EventHandler<EventArgs> ItemsCleared;
        public event EventHandler<ClipboardItemIndexEventArgs> SelectedIndexChanged;

        /// <summary>
        /// Convenience method for invoking events without having null reference checking everywhere
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="eventHandler">The event handler.</param>
        /// <param name="e">The event arguments.</param>
        public static void SafeInvoke<T>(EventHandler<T> eventHandler, T e) where T : EventArgs
        {
            if (eventHandler != null)
            {
                eventHandler(null, e);
            }
        }

        /// <summary>
        /// Updates the clipboard.
        /// </summary>
        public void UpdateClipboard()
        {
            
                ClipboardItem item = null;

                // This is a set of rules for creating the appropriate clipboard item instance based on the data
                // type that's present in the clipboard.
                var formats = new Func<ClipboardWrapper, ClipboardItem>[]
                                  {
                                      c =>  c.ContainsImage ? ObjectFactory.GetNamedInstance<ClipboardItem>("image") : null,
                                      c => c.ContainsAudio ? ObjectFactory.GetNamedInstance<ClipboardItem>("audio") : null,
                                      c =>  c.ContainsRichText ? ObjectFactory.GetNamedInstance<ClipboardItem>("rtf") : null,
                                      c => c.ContainsText ? ObjectFactory.GetNamedInstance<ClipboardItem>("text") : null
                                  };

                foreach (var format in formats)
                {
                    item = format(_wrapper);
                    if (item != null)
                    {
                        break;
                    }
                }

                // Only maintain 10 items. The thumbnail previews (depending on screen resolution) start displaying 
                // vertically without preview after 10 items.  Plus, a limit of 10 helps manage total memory consumed.
                if (_clipboardList.Count == 10)
                {
                    Remove(0);
                }

                _clipboardList.Add(item);
                _currentItemIndex++;

                // Notify the UI of the new item.
                SafeInvoke(ItemAdded, new ClipboardItemAddedEventArgs
                                          {
                                              Thumbnail = item.Thumbnail,
                                              CurrentItemIndex = _currentItemIndex,
                                              CanMoveNext = false,
                                              CanMovePrevious = _clipboardList.Count > 1
                                          });
        }
        /// <summary>
        /// Moves forward in the list of displayed items.
        /// </summary>
        public void Next()
        {
            _currentItemIndex++;

            if (_currentItemIndex > _clipboardList.Count - 1)
            {
                _currentItemIndex = _clipboardList.Count - 1;
                return;
            }

            NotifyIndexChanged();
        }
        /// <summary>
        /// Moves backwards in the list of displayed items.
        /// </summary>
        public void Previous()
        {
            _currentItemIndex--;

            if(_currentItemIndex < 0)
            {
                _currentItemIndex = 0;
                return;
            }

            NotifyIndexChanged();
        }
        /// <summary>
        /// Copies the contents of a clipboard item back to the Windows clipboard.
        /// </summary>
        /// <param name="index">The index.</param>
        public void Copy(int index)
        {
            _clipboardList[index].Copy();
        }
        /// <summary>
        /// Copies the contents of a clipboard item back to the Windows clipboard.
        /// </summary>
        public void Copy()
        {
            Copy(_currentItemIndex);
        }
        /// <summary>
        /// Removes the item at the specified index.
        /// </summary>
        /// <param name="index">The item index.</param>
        public void Remove(int index)
        {
            _clipboardList.RemoveAt(index);

            if (_currentItemIndex >= index && _currentItemIndex != 0 && _currentItemIndex == _clipboardList.Count)
            {
                _currentItemIndex--;
            }

            SafeInvoke(ItemRemoved,
                       new ClipboardItemRemovedEventArgs
                           {
                               RemovedItemIndex = index,
                               CurrentItemIndex = _currentItemIndex
                           });

            if (_clipboardList.Count > 0)
            {
                NotifyIndexChanged();
            }
            else
            {
                _currentItemIndex = -1;
                SafeInvoke(ItemsCleared, new EventArgs());
            }
        }
        /// <summary>
        /// Removes the current (displayed in paged mode) item.
        /// </summary>
        public void RemoveCurrent()
        {
            Remove(_currentItemIndex);
        }
        /// <summary>
        /// Notifies the UI that the selected index changed.
        /// </summary>
        public void NotifyIndexChanged()
        {
            SafeInvoke(SelectedIndexChanged,
                       new ClipboardItemIndexEventArgs
                           {
                               CurrentItemIndex = _currentItemIndex,
                               CanMoveNext = _currentItemIndex < _clipboardList.Count - 1,
                               CanMovePrevious = _currentItemIndex > 0
                           });
        }
    }
}

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

You may also be interested in...

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