public sealed class Clock : PictureBox
{
public Clock()
{
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
SetStyle(ControlStyles.DoubleBuffer, true);
SetStyle(ControlStyles.ResizeRedraw, true);
SetStyle(ControlStyles.Selectable, true);
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
SetStyle(ControlStyles.UserPaint, true);
BackColor = Color.Black;
Timer = new Timer {Interval = Interval, Enabled = true};
Timer.Tick += Timer_Tick;
InitColors();
InitNumber();
} /// <summary>
///
/// </summary>
private void Timer_Tick(object sender, EventArgs e)
{
var bmp = new Bitmap(Width, Height);
var g = Graphics.FromImage(bmp);
g.SmoothingMode = SmoothingMode.AntiAlias;
if (Type != TimeType.时分秒)
{
var year1 = Convert.ToInt32(DateTime.Now.Year.ToString("")[].ToString(CultureInfo.InvariantCulture));
var year2 = Convert.ToInt32(DateTime.Now.Year.ToString("")[].ToString(CultureInfo.InvariantCulture));
var year3 = Convert.ToInt32(DateTime.Now.Year.ToString("")[].ToString(CultureInfo.InvariantCulture));
var year4 = Convert.ToInt32(DateTime.Now.Year.ToString("")[].ToString(CultureInfo.InvariantCulture));
DrawNumber(g, , year1);
DrawNumber(g, , year2);
DrawNumber(g, , year3);
DrawNumber(g, , year4);
DrawNumber(g, , -); var month1 = Convert.ToInt32(DateTime.Now.Month.ToString("")[].ToString(CultureInfo.InvariantCulture));
var month2 = Convert.ToInt32(DateTime.Now.Month.ToString("")[].ToString(CultureInfo.InvariantCulture));
DrawNumber(g, , month1);
DrawNumber(g, , month2);
DrawNumber(g, , -); var day1 = Convert.ToInt32(DateTime.Now.Day.ToString("")[].ToString(CultureInfo.InvariantCulture));
var day2 = Convert.ToInt32(DateTime.Now.Day.ToString("")[].ToString(CultureInfo.InvariantCulture));
DrawNumber(g, , day1);
DrawNumber(g, , day2);
}
if (Type != TimeType.年月日)
{
var hour1 = Convert.ToInt32(DateTime.Now.Hour.ToString("")[].ToString(CultureInfo.InvariantCulture));
var hour2 = Convert.ToInt32(DateTime.Now.Hour.ToString("")[].ToString(CultureInfo.InvariantCulture));
DrawNumber(g, Type == TimeType.时分秒 ? : , hour1);
DrawNumber(g, Type == TimeType.时分秒 ? : , hour2); DrawColon(g, );
var minute1 =
Convert.ToInt32(DateTime.Now.Minute.ToString("")[].ToString(CultureInfo.InvariantCulture));
var minute2 =
Convert.ToInt32(DateTime.Now.Minute.ToString("")[].ToString(CultureInfo.InvariantCulture));
DrawNumber(g, Type == TimeType.时分秒 ? : , minute1);
DrawNumber(g, Type == TimeType.时分秒 ? : , minute2); DrawColon(g, );
var second1 =
Convert.ToInt32(DateTime.Now.Second.ToString("")[].ToString(CultureInfo.InvariantCulture));
var second2 =
Convert.ToInt32(DateTime.Now.Second.ToString("")[].ToString(CultureInfo.InvariantCulture));
DrawNumber(g, Type == TimeType.时分秒 ? : , second1);
DrawNumber(g, Type == TimeType.时分秒 ? : , second2);
}
BackgroundImage = bmp;
} /// <summary>
/// 绘制数字
/// </summary>
/// <param name="g">GDI</param>
/// <param name="index">数字位置</param>
/// <param name="value">数值</param>
private void DrawNumber(Graphics g, int index,int value)
{
if (_numbers.Length > index && WordColor.ContainsKey(value))
{
foreach (var key in _numbers[index].Keys)
{
var colors = WordColor[value].Where(s => s.Type == key).ToList();
if (colors.Any())
{
var c = colors[].Color;
using (var brush = new SolidBrush(c))
{
g.FillPath(brush, _numbers[index][key]);
}
}
}
}
}
/// <summary>
/// 绘制冒号
/// </summary>
/// <param name="g">GDI</param>
/// <param name="index">绘制第几个</param>
private void DrawColon(Graphics g, int index)
{
if (_colons.Length > index)
{
using (var brush = new SolidBrush(BrightColor))
{
g.FillRectangles(brush, _colons[index]);
}
}
} #region 属性
private Timer Timer { set; get; }
private TimeType _type = TimeType.时分秒;
private int _interval = ;
[Description("时钟显示的时间类型"), DefaultValue(typeof(TimeType)), Browsable(true)]
public TimeType Type
{
set
{
Stop();
_type = value;
InitNumber();
Start();
Invalidate();
}
get { return _type; }
} /// <summary>
/// 数字路径
/// </summary>
private Dictionary<WordType, GraphicsPath>[] _numbers = new Dictionary<WordType, GraphicsPath>[];
/// <summary>
/// 冒号
/// </summary>
private readonly RectangleF[][] _colons = new RectangleF[][]; /// <summary>
/// 时钟跳动间隔
/// </summary>
[Description("时钟的跳动间隔(毫秒)"),DefaultValue(typeof(Int32)),Browsable(true)]
public int Interval
{
set
{
_interval = value;
Invalidate();
}
get { return _interval; }
} private readonly Color[] _colors = {Color.FromArgb(, , ), Color.FromArgb(, , )}; /// <summary>
/// 亮色
/// </summary>
[Description("数字的颜色"), DefaultValue(typeof(Color)), Browsable(true)]
public Color BrightColor
{
set
{
_colors[] = value;
InitColors();
Invalidate();
}
get { return _colors[]; }
} /// <summary>
/// 暗色
/// </summary>
[Description("数字的背景颜色"), DefaultValue(typeof(Color)), Browsable(true)]
public Color DarkColor
{
set
{
_colors[] = value;
InitColors();
Invalidate();
}
get { return _colors[]; }
}
/// <summary>
/// 绘制路径
/// </summary>
private Dictionary<WordType,GraphicsPath> Paths { set; get; }
/// <summary>
/// 字体颜色设置
/// </summary>
private Dictionary<int, Word[]> WordColor { set; get; } /// <summary>
/// 初始化字体颜色
/// </summary>
private void InitColors()
{
WordColor = new Dictionary<int, Word[]>
{
{
-, new[]
{
new Word(WordType.TopHor, DarkColor),
new Word(WordType.LeftTopVer, DarkColor),
new Word(WordType.RightTopVer, DarkColor),
new Word(WordType.MiddleHor, BrightColor),
new Word(WordType.LeftBottomVer, DarkColor),
new Word(WordType.RightBottomVer, DarkColor),
new Word(WordType.BottomHor, DarkColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, BrightColor),
new Word(WordType.LeftTopVer, BrightColor),
new Word(WordType.RightTopVer, BrightColor),
new Word(WordType.MiddleHor, DarkColor),
new Word(WordType.LeftBottomVer, BrightColor),
new Word(WordType.RightBottomVer, BrightColor),
new Word(WordType.BottomHor, BrightColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, DarkColor),
new Word(WordType.LeftTopVer, DarkColor),
new Word(WordType.RightTopVer, BrightColor),
new Word(WordType.MiddleHor, DarkColor),
new Word(WordType.LeftBottomVer, DarkColor),
new Word(WordType.RightBottomVer, BrightColor),
new Word(WordType.BottomHor, DarkColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, BrightColor),
new Word(WordType.LeftTopVer, DarkColor),
new Word(WordType.RightTopVer, BrightColor),
new Word(WordType.MiddleHor, BrightColor),
new Word(WordType.LeftBottomVer, BrightColor),
new Word(WordType.RightBottomVer, DarkColor),
new Word(WordType.BottomHor, BrightColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, BrightColor),
new Word(WordType.LeftTopVer, DarkColor),
new Word(WordType.RightTopVer, BrightColor),
new Word(WordType.MiddleHor, BrightColor),
new Word(WordType.LeftBottomVer, DarkColor),
new Word(WordType.RightBottomVer, BrightColor),
new Word(WordType.BottomHor, BrightColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, DarkColor),
new Word(WordType.LeftTopVer, BrightColor),
new Word(WordType.RightTopVer, BrightColor),
new Word(WordType.MiddleHor, BrightColor),
new Word(WordType.LeftBottomVer, DarkColor),
new Word(WordType.RightBottomVer, BrightColor),
new Word(WordType.BottomHor, DarkColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, BrightColor),
new Word(WordType.LeftTopVer, BrightColor),
new Word(WordType.RightTopVer, DarkColor),
new Word(WordType.MiddleHor, BrightColor),
new Word(WordType.LeftBottomVer, DarkColor),
new Word(WordType.RightBottomVer, BrightColor),
new Word(WordType.BottomHor, BrightColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, BrightColor),
new Word(WordType.LeftTopVer, BrightColor),
new Word(WordType.RightTopVer, DarkColor),
new Word(WordType.MiddleHor, BrightColor),
new Word(WordType.LeftBottomVer, BrightColor),
new Word(WordType.RightBottomVer, BrightColor),
new Word(WordType.BottomHor, BrightColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, BrightColor),
new Word(WordType.LeftTopVer, DarkColor),
new Word(WordType.RightTopVer, BrightColor),
new Word(WordType.MiddleHor, DarkColor),
new Word(WordType.LeftBottomVer, DarkColor),
new Word(WordType.RightBottomVer, BrightColor),
new Word(WordType.BottomHor, DarkColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, BrightColor),
new Word(WordType.LeftTopVer, BrightColor),
new Word(WordType.RightTopVer, BrightColor),
new Word(WordType.MiddleHor, BrightColor),
new Word(WordType.LeftBottomVer, BrightColor),
new Word(WordType.RightBottomVer, BrightColor),
new Word(WordType.BottomHor, BrightColor)
}
},
{
, new[]
{
new Word(WordType.TopHor, BrightColor),
new Word(WordType.LeftTopVer, BrightColor),
new Word(WordType.RightTopVer, BrightColor),
new Word(WordType.MiddleHor, BrightColor),
new Word(WordType.LeftBottomVer, DarkColor),
new Word(WordType.RightBottomVer, BrightColor),
new Word(WordType.BottomHor, DarkColor)
}
}
};
} #endregion /// <summary>
/// 启动时钟
/// </summary>
public void Start()
{
if (Timer != null)
{
Timer.Start();
}
} /// <summary>
/// 关闭时钟
/// </summary>
public void Stop()
{
if (Timer != null)
{
Timer.Stop();
}
} /// <summary>
/// 初始化数字路径
/// </summary>
private void InitNumber()
{
var length = ;
switch (Type)
{
case TimeType.年月日:
_numbers = new Dictionary<WordType, GraphicsPath>[];
length = ;
break;
case TimeType.年月日时分秒:
_numbers = new Dictionary<WordType, GraphicsPath>[];
length = ;
break;
case TimeType.时分秒:
length = ;
_numbers = new Dictionary<WordType, GraphicsPath>[];
break;
}
var w = Height/;
int x = ;
int index = ;
for (int i = ; i < length; i++)
{
switch (Type)
{
case TimeType.时分秒:
if (i == || i == )
_colons[i == ? : ] = Colon(x, , Height - );
else
{
_numbers[index] = Number(x, , Height - );
index ++;
}
x = (i == || i == ) ? x + : w + x + ;
break;
case TimeType.年月日:
_numbers[i] = Number(x, , Height - );
x = w + x + ;
break;
case TimeType.年月日时分秒:
if (i != && i != )
{
_numbers[index] = Number(x, , Height - );
index++;
}
x = (i == ) ? w* + x : (i == || i == ) ? x : w + x + ;
if (i == || i == )
{
_colons[i == ? : ] = Colon(x, , Height - );
x = x + w/;
}
break;
default:
x = w + x + ;
_numbers[i] = Number(x, , Height - );
break;
}
}
} #region 绘制图像方法 /// <summary>
/// 绘制冒号
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <param name="height"></param>
/// <returns></returns>
private RectangleF[] Colon(float x, float y, float height)
{
var h = height/;
var r1 = new RectangleF(x, y + h*, h*, h*);
var r2 = new RectangleF(x, y + h*, h*, h*);
return new[] {r1, r2};
} /// <summary>
/// 绘制数字
/// </summary>
/// <param name="x">左上角X轴坐标</param>
/// <param name="y">左上角Y轴坐标</param>
/// <param name="height">数字高度</param>
private Dictionary<WordType, GraphicsPath> Number(float x, float y, float height)
{
Paths = new Dictionary<WordType, GraphicsPath>();
var h = height/;
var w = h;
var width = w*;
var x1 = x;
var x2 = x + w;
var x3 = x + w*;
var x4 = x + width - w*;
var x5 = x + width-w;
var x6 = x + width;
var y1 = y;
var y2 = y + h;
var y3 = y + h*;
var y4 = y + (height/ - h);
var y5 = y + (height/);
var y6 = y + (height/ + h);
var y7 = y + height - h*;
var y8 = y + height - h;
var y9 = y + height;
const float offset = 0.5f;
var hor1 = new GraphicsPath();
hor1.AddLines(new[]
{
new PointF(x2 + offset, y2 - offset),
new PointF(x3 + offset, y1 + offset),
new PointF(x4 - offset, y1 + offset),
new PointF(x5 - offset, y2 - offset),
new PointF(x4 - offset, y3 - offset),
new PointF(x3 + offset, y3 - offset),
new PointF(x2 + offset, y2 - offset)
});
hor1.CloseFigure();
Paths.Add(WordType.TopHor, hor1);
var hor2 = new GraphicsPath();
hor2.AddLines(new[]
{
new PointF(x2 +offset, y5),
new PointF(x3 + offset, y4 + offset),
new PointF(x4 - offset, y4 + offset),
new PointF(x5 - offset, y5),
new PointF(x4 - offset, y6 - offset),
new PointF(x3 + offset, y6 - offset),
new PointF(x2 + offset, y5)
});
hor2.CloseFigure();
Paths.Add(WordType.MiddleHor, hor2);
var hor3 = new GraphicsPath();
hor3.AddLines(new[]
{
new PointF(x2 + offset, y8),
new PointF(x3 + offset, y7),
new PointF(x4 - offset, y7),
new PointF(x5 - offset, y8),
new PointF(x4 - offset, y9),
new PointF(x3 + offset, y9),
new PointF(x2 + offset, y8)
});
hor3.CloseFigure();
Paths.Add(WordType.BottomHor, hor3);
var ver1 = new GraphicsPath();
ver1.AddLines(new[]
{
new PointF(x1, y3 + offset),
new PointF(x2, y2 + offset),
new PointF(x3, y3 + offset),
new PointF(x3, y4 - offset),
new PointF(x2, y5 - offset),
new PointF(x1, y4 - offset)
});
ver1.CloseFigure();
Paths.Add(WordType.LeftTopVer, ver1);
var ver2 = new GraphicsPath();
ver2.AddLines(new[]
{
new PointF(x4, y3 + offset),
new PointF(x5, y2 + offset),
new PointF(x6, y3 + offset),
new PointF(x6, y4 - offset),
new PointF(x5, y5 - offset),
new PointF(x4, y4 - offset)
});
ver2.CloseFigure();
Paths.Add(WordType.RightTopVer, ver2);
var ver3 = new GraphicsPath();
ver3.AddLines(new[]
{
new PointF(x1, y6 + offset),
new PointF(x2, y5 + offset),
new PointF(x3, y6 + offset),
new PointF(x3, y7 - offset),
new PointF(x2, y8 - offset),
new PointF(x1, y7 - offset)
});
ver3.CloseFigure();
Paths.Add(WordType.LeftBottomVer, ver3);
var ver4 = new GraphicsPath();
ver4.AddLines(new[]
{
new PointF(x4, y6 + offset),
new PointF(x5, y5 + offset),
new PointF(x6, y6 + offset),
new PointF(x6, y7 - offset),
new PointF(x5, y8 - offset),
new PointF(x4, y7 - offset)
});
ver4.CloseFigure();
Paths.Add(WordType.RightBottomVer, ver4);
return Paths;
} #endregion protected override void OnResize(EventArgs e)
{
InitNumber();
base.OnResize(e);
} /// <summary>
/// 数字线条结构
/// </summary>
public struct Word
{
public Word(WordType type, Color color)
: this()
{
Type = type;
Color = color;
}
/// <summary>
/// 线条类型
/// </summary>
public WordType Type { set; get; }
/// <summary>
/// 线条颜色
/// </summary>
public Color Color { set; get; }
}
#region 隐藏的属性
[Description("背景图像"),DefaultValue(null),Browsable(false)]
public new Image BackgroundImage {get { return base.BackgroundImage; } set { base.BackgroundImage = value; } }
[Description("背景图像布局"), DefaultValue(typeof(ImageLayout)), Browsable(false)]
public new ImageLayout BackgroundImageLayout { get { return base.BackgroundImageLayout; } set { base.BackgroundImageLayout = value; } }
[Description("显示的图像"), DefaultValue(null), Browsable(false)]
public new Image Image{get { return base.Image; } set { base.Image = value; }}
[Description("加载错误时显示的图像"), DefaultValue(null), Browsable(false)]
public new Image ErrorImage{get { return base.ErrorImage; } set { base.ErrorImage = value; }}
[Description("加初始化时显示的图像"), DefaultValue(null), Browsable(false)]
public new Image InitialImage{set { base.InitialImage = value; } get { return base.InitialImage; }}
[Description("只是如何显示图像"), DefaultValue(typeof(PictureBoxSizeMode)), Browsable(false)]
public new PictureBoxSizeMode SizeMode {set { base.SizeMode = value; } get { return base.SizeMode; } }
#endregion
} /// <summary>
/// 数字线条枚举
/// </summary>
public enum WordType
{
TopHor = ,
LeftTopVer = ,
RightTopVer = ,
MiddleHor = ,
LeftBottomVer = ,
RightBottomVer = ,
BottomHor =
} public enum TimeType
{
年月日,
时分秒,
年月日时分秒
}

winfrom LED时钟的更多相关文章

  1. Angular6封装LED时钟数字组件

    一.运行截图 截图1: 截图2: 二.代码 html代码: <div class="time" > <ng-container #container> &l ...

  2. STM32入门系列-使用库函数点亮LED,LED初始化函数

    要点亮LED,需要完成LED的驱动, 在工程模板上新建一个led.c和led.h文件,将其存放在led文件夹内.这两个文件需要我们自己编写. 通常xxx.c文件用于存放编写的驱动程序,xxx.h文件用 ...

  3. STM32学习之路-SysTick的应用(时间延迟)

    开发板:奋斗V5 好~ 菜B要来搞实验了.. 前面已经说了SysTick的工作原理什么的了,这里就不说了.. 先来做第一个实验: 盗自奋斗的样例,嘿嘿, 用SysTick产生1ms的基准时间,产生中断 ...

  4. jquery+html5制作超酷的圆盘时钟表

    自己封装的一个用HTML5+jQuery写的时钟表 代码: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ...

  5. 漫谈LiteOS之开发板-GPIO(基于GD32450i-EVAL)

    [摘要] 本文主要从GPIO的定义.工作模式.特色.工作场合.以及GD32450i-EVAL开发板的引脚.对应的寄存器以及GPIO的流水灯示例对GPIO加以介绍,希望对你有所帮助. 1定义 GPIO( ...

  6. 桌面小部件----LED电子时钟实现

    桌面控件是通过 Broadcast 的形式来进行控制的,因此每个桌面控件都对应于一个BroadcastReceiver.为了简化桌面控件的开发,Android 系统提供了一个 AppWidgetPro ...

  7. 基于Systick系统时钟延时的LED闪烁灯

    1.回顾我们的51 单片机编程,当我们需要做系统延迟的时候,最常采用的一种方式就是使用for 循环的空语句等待来实现. 当然,在STM32 里面也可以这么实现.但是在STM32 的Cortex 内核里 ...

  8. NEXYS 3开发板练手--LED与数码管时钟

    做科研的时候从学校拿到一块基于Xilinx公司Spartan-6主芯片的FPGA开发板,因为之前一直在用Altera公司的FPGA,一开始接触它还真有点不太习惯.但毕竟核心的东西还是不会变的,于是按照 ...

  9. 一个简易的 LED 数字时钟实现方法

    这个应该是已经有很多人做过的东西,我应该只是算手痒,想写一下,所以,花了点时间折腾了这个,顺便把 Dark Mode 的处理也加上了. 首先可以很明确的一点,这个真没技术含量存在,只是需要点耐心. L ...

随机推荐

  1. NGUI panel使用soft clip时,屏幕缩放后无法正常工作的问题解决

    最近开始使用NGUI,通过查找,搞定了屏幕缩放问题,但在用到panel的soft clip时,碰到了问题,NGUI给出了警告 “clipped panels must have a uniform s ...

  2. C# TextBox 换行 滚动到最后一行

    .要让一个Windows Form的TextBox显示多行文本就得把它的Multiline属性设置为true. 这个大家都知道,可是当你要在代码中为Text属性设置多行文本的时候可能会遇到点麻烦:) ...

  3. SQL SERVER中架构的理解

    在sqlserver 2005中,可能大家在工作或学习的时候会经常发现这样一些问题,你使用一个账户在数据库中创建了一张表,却发现你自己创建的表却没有修改和查询的权限,这是一件很郁闷的事情,在sqlse ...

  4. java_log4j多文件配置

    今天配置了log4j中写多个文件的内容,配置了半天才搞出来,为了避免类似问题,写个博客吧. 首先说一下需求,每天要在7个文件夹中生成文件,文件格式为xxx.log.2000.01.01,自己开发个写文 ...

  5. struts2拦截器源码分析

    前面博客我们介绍了开发struts2应用程序的基本流程(开发一个struts2的实例),通过前面我们知道了struts2实现请求转发和配置文件加载都是拦截器进行的操作,这也就是为什么我们要在web.x ...

  6. 【分割圆】Uva 10213 - How Many Pieces of Land ?

    一个椭圆上有N个点,将这n个点两两相连,问最多能将这个椭圆分成多少片. 理清思路,慢慢推. 首先我们要想到欧拉公式:V+E-F=2 其中V为图上的顶点数,E为边数,F为平面数. 计算时的可以枚举点,从 ...

  7. COGS 265 线段覆盖

    265. 线段覆盖 ★★☆   输入文件:xdfg.in   输出文件:xdfg.out   简单对比时间限制:2 s   内存限制:20 MB [问题描述] 有一根长度为 L 的白色条状物.有两种操 ...

  8. sublime text使用技巧

    常用快捷键 Ctrl + L  选择整行(按住-继续选择下行) Ctrl + KK  从光标处删除至行尾 Ctrl + Shift+K  删除整行 Ctrl + Shift+D  复制光标所在整行,插 ...

  9. 【TOMCAT】Tomcat gzip压缩传输数据

    概述 由于我们项目的三维模型文件非常大,为了提高传输速度,在服务端对其做zip压缩处理非常有必要,能够极大的提高传输速度. 配置 首先需要修改web.xml中请求的数据文件的mime类型的mappin ...

  10. WCF简介

    WCF(Windows communication Foundation),顾名思义,就是在windows平台下解决通信的基础框架.WCF做为.NET Framework 3.0的一个组件发布出来的, ...