![]() |
Desktop Development »
Miscellaneous »
Miscellaneous Controls
Intermediate
License: The Code Project Open License (CPOL)
EyesBy Niel M.ThomasCreating an eye control. |
C# (C#2.0), Windows, .NET (.NET2.0), GDI+, Dev
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
This article demonstrates how to create a perfect, useless control using C# WinForms and .NET 2.0. The control uses pure managed code, and all visual elements can be adjusted through its properties.
Is it not all developer's deepest desire to create something living? Well, not me! This was solely to have fun playing with the graphics. So, don't expect to see a mouth, nose, or brains in the future.

Above are the basic elements of the eye: background, iris, shadow, pupil, reflex, and the eye-lid. The eye is drawn in this order.
The Eye is a single class that inherits from the System.Windows.Forms.Control. Below are the properties and methods of the Eye class.

Using the Eye class is straightforward, but some properties may need some explanation.
BlinkStep is the rate in which the eye will close when blinking. FocusPoint is the point the eye will look, in screen coordinates. FocusAngle and FocusDistance are derived (read only) from FocusPoint. LidOffset is used when TypeOfEye is Left or Right.
The offset in pixels is the width of the iris divided by the value. Minimum is 3.
SlitSize is a percentage of the eye height. TypeOfEye is an enum, and can take the values Left, Right, and Cyclops (default). Blink() starts a new thread that will blink the eye. The detailed iris is done using a color blend:
using (var path = new GraphicsPath())
{
path.AddEllipse(rect);
using (var gradientBrush = new PathGradientBrush(path))
{
gradientBrush.CenterPoint = centerPoint;
var cb = new ColorBlend(4)
{
Colors = irisColors,
Positions = new[] { 0.0F, 0.05F, 0.1F, 1.0F }
};
if (cb.Colors == null) return;
gradientBrush.InterpolationColors = cb;
g.FillPath(gradientBrush, path);
}
}
I attempted to squeeze the iris rectangle to make it more eye-like when going to the edge, but it never quite looked right.
This is the first version - 1.0.
General
News
Question
Answer
Joke
Rant
Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads.
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 11 Sep 2008 Editor: Sean Ewington |
Copyright 2008 by Niel M.Thomas Everything else Copyright © CodeProject, 1999-2010 Web21 | Advertise on the Code Project |