Click here to Skip to main content
15,897,704 members
Articles / Desktop Programming / Windows Forms

Visual Application Launcher

Rate me:
Please Sign up or sign in to vote.
4.91/5 (37 votes)
23 Jan 2012CPOL23 min read 108.2K   3.7K   116  
A WinForms UI using WCF services, Entity Framework, repository data access, repository caching, Unit of Work, Dependency Injection, and every other buzz work you can think of!
using System;
using System.Collections;
using System.Diagnostics;
using System.Drawing;
using System.Reflection;

namespace VAL.ColorPicker
{

    /// <summary>
    ///   Possible sort order for named colors.
    /// </summary>
    public enum ColorsSortOrder
    {
        HSB,
        Alphabetical,
        None
    }

    /// <summary>
    ///   Helper class which collects lists of named and system colors.
    /// </summary>
    internal class ColorsProvider
    {

        /// <summary>
        ///   Hides constructor in order to prevent instantation of the class. 
        /// </summary>
        private ColorsProvider()
        {
        }

        /// <summary>
        ///   Gets an array of system colors.
        /// </summary>
        /// <returns>Array of system colors.</returns>
        public static object[] GetSystemColors()
        {
            Debug.Assert(m_listSystemColors != null && m_listSystemColors.Count > 0);
            return (object[])m_listSystemColors.ToArray(typeof(object));
        }

        /// <summary>
        ///   Gets an array of named colors.
        /// </summary>
        /// <param name="sortOrder">ColorsSortOrder which defines ordering of array returned.</param>
        /// <returns>Array of named colors.</returns>
        public static object[] GetSortedNamedColors(ColorsSortOrder sortOrder)
        {
            Debug.Assert(m_listNamedColors != null && m_listNamedColors.Count > 0);
            ArrayList all = new ArrayList(m_listNamedColors);
            switch (sortOrder)
            {
                case ColorsSortOrder.HSB:
                    all.Sort(new HSBColorComparer());
                    break;
                case ColorsSortOrder.Alphabetical:
                    all.Sort(new NameColorComparer());
                    break;
            }
            return (object[])all.ToArray(typeof(object));

        }

        /// <summary>
        ///   Creates a list of named colors.
        /// </summary>
        /// <returns>ArrayList of named colors.</returns>
        private static ArrayList GetListOfNamedColors()
        {
            Type color = (typeof(Color));
            PropertyInfo[] propertyInfos = color.GetProperties(BindingFlags.Public | BindingFlags.Static);
            ArrayList colors = new ArrayList();
            foreach (PropertyInfo pi in propertyInfos)
            {
                if (pi.PropertyType.Equals(typeof(Color)))
                {
                    Color c = (Color)pi.GetValue(color, null);
                    colors.Add(c);
                }
            }
            return colors;
        }

        /// <summary>
        ///   Creates a list of system colors.
        /// </summary>
        /// <returns>ArrayList of system colors.</returns>
        private static ArrayList GetListOfSystemColors()
        {
            Type color = (typeof(SystemColors));
            PropertyInfo[] propertyInfos = color.GetProperties(BindingFlags.Public | BindingFlags.Static);
            ArrayList colors = new ArrayList();
            foreach (PropertyInfo pi in propertyInfos)
            {
                if (pi.PropertyType.Equals(typeof(Color)))
                {
                    Color c = (Color)pi.GetValue(color, null);
                    colors.Add(c);
                }
            }
            return colors;
        }

        /// <summary>
        ///   Static constructor. Initializes lists of named and system colors.
        /// </summary>
        static ColorsProvider()
        {
            m_listNamedColors = GetListOfNamedColors();
            m_listSystemColors = GetListOfSystemColors();
        }

        private static ArrayList m_listNamedColors;

        private static ArrayList m_listSystemColors;
    }
}

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
Technical Lead
United Kingdom United Kingdom
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions