数字时钟DigClock
首先建立数字显示类:
using System;
using System.Drawing; namespace CsDev
{
class SevenSegmentDispay
{
Graphics grph;
//0,1,2,3,4,5,6,7,8,9
static byte[,] bySegment = { { 1, 1, 1, 0, 1, 1,1 }, { 0, 0, 1, 0, 0,1, 0 },
{1,0,1,1,1,0,1},{1,0,1,1,0,1,1},
{0,1,1,1,0,1,0},{1,1,0,1,0,1,1},
{1,1,0,1,1,1,1},{1,0,1,0,0,1,0},
{1,1,1,1,1,1,1},{1,1,1,1,0,1,1}};
readonly Point[][] apt = new Point[7][]; public SevenSegmentDispay(Graphics grph)
{
this.grph = grph;
apt[0] = new Point[] { new Point(3, 2), new Point(39, 2), new Point(31, 10), new Point(11, 10) };
apt[1] = new Point[] { new Point(2, 3), new Point(10, 11), new Point(10, 31), new Point(2, 35) };
apt[2] = new Point[] { new Point(40, 3), new Point(40, 35), new Point(32, 31), new Point(32, 11) };
apt[3] = new Point[] { new Point(3, 36), new Point(11, 32), new Point(31, 32), new Point(39, 36),
new Point(31, 40),new Point(11, 40)};
apt[4] = new Point[] { new Point(2, 37), new Point(10, 41), new Point(10, 61), new Point(2, 69) };
apt[5] = new Point[] { new Point(40, 37), new Point(40, 69), new Point(32, 61), new Point(32, 41) };
apt[6] = new Point[] { new Point(11, 62), new Point(31, 62), new Point(39, 70), new Point(3, 70) };
} public SizeF MeasureString(string str, Font font)
{
SizeF sizef = new SizeF(0, grph.DpiX * font.SizeInPoints / 72); for (int i = 0; i < str.Length; i++)
{
if (Char.IsDigit(str[i]))
sizef.Width += 42 * grph.DpiX * font.SizeInPoints / 72 / 72;
else if (str[i] == ':')
sizef.Width += 12 * grph.DpiX * font.SizeInPoints / 72 / 72;
}
return sizef;
}
void Fill(Point[] apt, Font font, Brush brush, float x, float y)
{
PointF[] aptf = new PointF[apt.Length];
for (int i = 0; i < apt.Length; i++)
{
aptf[i].X = x + apt[i].X * grph.DpiX * font.SizeInPoints / 72 / 72;
aptf[i].Y = y + apt[i].Y * grph.DpiY * font.SizeInPoints / 72 / 72;
}
grph.FillPolygon(brush, aptf);
}
float Number(int num, Font font, Brush brush, float x, float y)
{
for (int i = 0; i < apt.Length; i++)
if (bySegment[num, i] == 1)
Fill(apt[i], font, brush, x, y);
return x + 42 * grph.DpiX * font.SizeInPoints / 72 / 72;
}
float Colon(Font font, Brush brush, float x, float y)//画冒号
{
Point[][] apt = new Point[2][];
apt[0] = new Point[] { new Point(2, 21), new Point(6, 17), new Point(10, 21), new Point(6, 25) };
apt[1] = new Point[] { new Point(2, 51), new Point(6, 47), new Point(10, 51), new Point(6, 55) };
for (int i = 0; i < apt.Length; i++)
Fill(apt[i], font, brush, x, y);
return x + 12 * grph.DpiX * font.SizeInPoints / 72 / 72;
}
public void DrawString(string str, Font font, Brush brush, float x, float y)
{
for (int i = 0; i < str.Length; i++)
{
if (Char.IsDigit(str[i]))
x = Number(str[i] - '0', font, brush, x, y);
else if (str[i] == ':')
x = Colon(font, brush, x, y);
}
}
}
}
实现类:
using System;
using System.Windows.Forms;
using System.Drawing;
using System.Globalization; namespace CsDev
{
class DigClock:Form
{
DateTime dt;
public static void Main()
{
Application.Run(new DigClock());
}
public DigClock()
{
Text = "数字时钟";
ResizeRedraw = true;
MinimumSize = SystemInformation.MinimumWindowSize + new Size(0, 1); dt = DateTime.Now;
Timer timer = new Timer();
timer.Tick+=new EventHandler(timer_Tick);
timer.Interval = 100;
timer.Enabled = true;
}
void timer_Tick(object obj, EventArgs e)
{
DateTime dtNow = DateTime.Now;
dtNow = new DateTime(dtNow.Year, dtNow.Month, dtNow.Day, dtNow.Hour, dtNow.Minute, dtNow.Second);
if (dt != dtNow)
{
dt = dtNow;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
SevenSegmentDispay ssd = new SevenSegmentDispay(e.Graphics);
string strTime = dt.ToString("T",DateTimeFormatInfo.InvariantInfo);
SizeF sizef = ssd.MeasureString(strTime, Font);
float fScale = Math.Min(ClientSize.Width/sizef.Width,ClientSize.Height/sizef.Height);
Font font = new Font(Font.FontFamily, fScale * Font.SizeInPoints);
sizef = ssd.MeasureString(strTime, font);//注意font是自定义 ssd.DrawString(strTime, font, Brushes.Red, (ClientSize.Width-sizef.Width)/2,(ClientSize.Height-sizef.Height)/2);
base.OnPaint(e);
}
}
}
效果图:
数字时钟DigClock的更多相关文章
- 模拟时钟(AnalogClock)和数字时钟(DigitalClock)
Demo2\clock_demo\src\main\res\layout\activity_main.xml <LinearLayout xmlns:android="http://s ...
- C#开发漂亮的数字时钟
今天用C#做了一个漂亮的数字时钟.界面如下. 实现技术:主要是通过Graphics类的DrawImage方法来绘制数字时钟中所有的数字,这些数字是从网上找的一些图片文件.时钟使用DateTime中No ...
- Qt仿Android带特效的数字时钟源码分析(滑动,翻页,旋转效果)
这个数字时钟的源码可以在Qt Demo中找到,风格是仿Android的,不过该Demo中含有三种动画效果(鉴于本人未曾用过Android的系统,因此不知道Android的数字时钟是否也含有这三种效果) ...
- android脚步---数字时钟和模拟时钟
时钟UI组件是两个非常简单的组件,分为Digitalclock 和Analogclock, main.xml文件,书中程序有问题,加了两个组件,一个Button和一个<Chronometer ...
- 基于Verilog HDL 的数字时钟设计
基于Verilog HDL的数字时钟设计 一.实验内容: 利用FPGA实现数字时钟设计,附带秒表功能及时间设置功能.时间设置由开关S1和S2控制,分别是增和减.开关S3是模式选择:0是正常时钟 ...
- HandlerThread实现数字时钟
1.描述 刚看完Android多线程编程,对HandlerThread比较感兴趣,趁热巩固练习,实现一个了数字时钟,希望对学习HandlerThread有所帮助.如下: 启动一个HandlerThre ...
- js动态数字时钟
js动态数字时钟 主要用到知识点: 主要是通过数组的一些方法,如:Array.from() Array.reduce() Array.find() 时间的处理和渲染 js用到面向对象的写法 实现的功能 ...
- 简单酷炫的Canvas数字时钟
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 我记得很早之前就看过这个DEMO,是岑安大大博客里看到的: 就是这个数字时钟,当时觉得这个创意不错,但是也没去折腾.直到昨天同事又在网上看 ...
- 使用jQuery和CSS3实现一个数字时钟
点击进入更详细教程及源码下载 在线演示 我们经常会在网站中看见一个时钟的效果.今天向大家分享一个使用jQuery和CSS3实现一个数字时钟教程. http://www.html5cn.org/ ...
随机推荐
- HTML5图片预览
两种方式实现 URL FileReader <!DOCTYPE HTML><html> <head> <meta charset="ut ...
- js简易写法
我写JavaScript代码已经很久了,都记不起是什么年代开始的了.对于JavaScript这种语言近几年所取得的成就,我感到非常的兴奋:我很幸运也是这些成就的获益者.我写了不少的文章,章节,还有一本 ...
- jquery知识点积累
网上资源汇总学习: jquery的选择器是CSS1-3,xpath的结合物.JQuery提取了这二种查询语言最好的部分,创造出了最终的jquery表达式查询语言. xpath是一门在xml文档里查找信 ...
- 利用Oracle数据库的UTL_SMTP发送HTML 邮件
Ok, that looks hard, but if you use this procedure I wrote, its really quite easy, it does all of th ...
- (转)DataTable添加行出现“该行已经属于另一个表”的错误!
1 DataTable dt1 = new DataTable(); DataTable dt2 = new DataTable(); //为dt1创建结构 DataColumn pName = ne ...
- 《C++ 标准库》读书笔记 - 第二章 Introduction to C++ and the Standard Library
1. History of the C++ Standards 1.1 History of the C++ Standards C++98 -> C++03 -> TR1 -> C ...
- golang Rsa
package models import ( "crypto/rand" "crypto/rsa" "crypto/x509" " ...
- 用SQL将查询出来的多列的值拼接成一个字符串【转载】
MySQL中: [sql] view plaincopyprint? -- 单列拼接,先查出一行,再加上逗号,接着拼接 查出的下一行 select group_concat(E.SUPPORT) fr ...
- Java-Poi 读取excel 数据
一直想着使用java操作excel,但有时各种原因一直没有实现.由于工作无意间做了个其他demo,为了进一步发散就涉及到了使用excel,为此开始正式接触POI,虽然限制不是很了解POI,但是通过查阅 ...
- C的陷阱和缺陷研读笔记01
词法分析: 编译器将程序分解成符号的方法是 从左到右一个一个字符的读入,如果该字符可能组成一个符号,再读入下一个字符 而c语言里的符号 / * =只有一个字符长, 是单字符的, /* == 一些事双字 ...