65.9K
CodeProject 正在变化。 阅读更多。
Home

眼睛

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.96/5 (32投票s)

2008 年 9 月 9 日

CPOL

1分钟阅读

viewsIcon

72084

downloadIcon

1136

创建一个眼睛控件。

Sample Image

引言

本文演示了如何使用 C# WinForms 和 .NET 2.0 创建一个完美且无用的控件。该控件使用纯托管代码,所有视觉元素都可以通过其属性进行调整。

背景

创造一个有生命的物体不是所有开发者的终极愿望吗?好吧,对我来说不是!这仅仅是为了玩转图形。所以,不要指望将来看到嘴巴、鼻子或大脑。

图形

The process of building the eye.

上面是眼睛的基本元素:背景、虹膜、阴影、瞳孔、反光和眼睑。眼睛按照这个顺序绘制。

架构

Eye 是一个继承自 System.Windows.Forms.Control 的单一类。以下是 Eye 类的属性和方法。

The Eye class

Using the Code

使用 Eye 类很简单,但有些属性可能需要一些解释。

  • BlinkStep 是眨眼时眼睛关闭的速度。
  • FocusPoint 是眼睛注视的点,以屏幕坐标表示。
  • FocusAngleFocusDistance 是从 FocusPoint 推导出来的(只读)。
  • LidOffset 用于 TypeOfEyeLeftRight 时。

    像素偏移量是虹膜宽度除以该值。最小值为 3。

  • SlitSize 是眼睛高度的百分比。
  • TypeOfEye 是一个枚举,可以取 LeftRightCyclops(默认值)这些值。
  • Blink() 启动一个新线程,该线程将眨动眼睛。

详细的虹膜是通过颜色混合完成的

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);
  }
}

关注点

我尝试压缩虹膜矩形,使其在到达边缘时更像眼睛,但它看起来始终不太对。

历史和鸣谢

这是第一个版本 - 1.0。

© . All rights reserved.