Click here to Skip to main content
11,570,750 members (51,158 online)
Click here to Skip to main content
Add your own
alternative version

Full Screen WPF Application with a Low-Level Keyboard Hook

, 26 Nov 2008 CPOL 75K 2.1K 47
This application displays shapes of random sizes and colors. It's perfect for babies and toddlers who love to sit and press away at the keyboard.
using System;
using System.Windows;
using System.Windows.Media;

namespace ShapeShow.Library
{
    /// <summary>
    /// A factory class to create new shapes.
    /// </summary>
    public class ShapeFactory
    {
        #region Private Members

        static Int32 _shapeCount = 0;
        static Int32 ShapeCount
        {
            get
            {
                if (_shapeCount == 0)
                {
                    _shapeCount = Enum.GetNames(typeof(ShapeType)).Length;
                }

                return _shapeCount;
            }
        }

        static Int32 _colorCount = 0;
        static Int32 ColorCount
        {
            get
            {
                if (_colorCount == 0)
                {
                    _colorCount = Enum.GetNames(typeof(ShapeColor)).Length;
                }

                return _colorCount;
            }
        }

        static Double MinHeight = 50;
        static Double MinWidth = 50;

        static Random RandomNumber = new Random();

        #endregion

        #region Public Methods

        /// <summary>
        /// Creates a new shape randomly, bound by the screen size.
        /// </summary>
        /// <returns>A new shape</returns>
        public static IShape CreateShape()
        {
            return CreateShape((ShapeType)RandomNumber.Next(0, ShapeCount));
        }

        /// <summary>
        /// Creates a new shape of the specified type, bound by the screen size.
        /// </summary>
        /// <param name="shapeType">The type of shape to create</param>
        /// <returns>A new shape</returns>
        public static IShape CreateShape(ShapeType shapeType)
        {
            return CreateShape(shapeType, (ShapeColor)RandomNumber.Next(0, ColorCount));
        }

        /// <summary>
        /// Creates a new shape of the specified type and color, bound by the screen size.
        /// </summary>
        /// <param name="shapeType">The type of shape to create</param>
        /// <param name="shapeColor">The color of the shape</param>
        /// <returns>A new shape</returns>
        public static IShape CreateShape(ShapeType shapeType, ShapeColor shapeColor)
        {
            IShape shape = CreateShapeFromType(shapeType);

            shape.Height = RandomNumber.NextDouble() * (SystemParameters.VirtualScreenHeight / 2) + MinHeight;
            shape.Width = RandomNumber.NextDouble() * (SystemParameters.VirtualScreenWidth / 2) + MinWidth;
            shape.Top = RandomNumber.NextDouble() * (SystemParameters.VirtualScreenHeight - shape.Height);
            shape.Left = RandomNumber.NextDouble() * (SystemParameters.VirtualScreenWidth - shape.Width);
            shape.FillBrush = CreateBrushFromColor(shapeColor);

            return shape;
        }

        #endregion

        #region Private Methods

        private static IShape CreateShapeFromType(ShapeType shapeType)
        {
            switch (shapeType)
            {
                case ShapeType.Circle:
                    return new Circle();

                case ShapeType.Triangle:
                    return new Triangle();

                case ShapeType.Square:
                    return new Square();

                default:
                    return null;
            }
        }

        private static Brush CreateBrushFromColor(ShapeColor shapeColor)
        {
            switch (shapeColor)
            {
                case ShapeColor.Red:
                    return Brushes.Red;

                case ShapeColor.Green:
                    return Brushes.Green;

                case ShapeColor.Blue:
                    return Brushes.Blue;

                case ShapeColor.Yellow:
                    return Brushes.Yellow;

                case ShapeColor.Pink:
                    return Brushes.Pink;

                case ShapeColor.LightBlue:
                    return Brushes.LightBlue;

                case ShapeColor.LightGreen:
                    return Brushes.LightGreen;

                default:
                    return null;
            }
        }

        #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

Barry Dorman
Software Developer
United States United States
Barry Dorman is a software engineer from the Birmingham, AL area. His primary focus is ASP.NET. Barry graduated with a BS in Electrical Engineering from The University of Alabama at Birmingham in 2006. Since then, he has kept himself busy working in the healthcare industry.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150624.2 | Last Updated 26 Nov 2008
Article Copyright 2008 by Barry Dorman
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid