数字时钟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/ ...
随机推荐
- ASP.NET MVC 学习之路-6
本文在于巩固基础 上文中使用的Code First创建数据库 本文将使用数据库生成模型 这里使用ADO.NET实体数据模型来生成模型 下面按照指导完成操作 下面看看如何使用这个框架 数据访问修改主要是 ...
- sql必知必会(第四版) 学习笔记一
温习一遍简单的sql语法,把自己掌握还不够的地方,做了些笔记.... 1 去重复关键词,distinct select distinct sname from student; 2 限制结果top的用 ...
- JavaScript 中 关于 this 的学习笔记
今天上午主要学习了js中的 this ,因为之前学习面向对象时,this这个东西出现的还是很频繁的,理解的很不透彻,感觉老被JAVA的思想带进坑里,所以对它特别关注. 首先贴一个大神的一篇博客,我是通 ...
- css系列教程--选择器
css派生选择器:是指在某个样式表或者dom元素的行内定义行内元素的属性,而其他同名样式在其他dom节点无效的样式表定义方式.例如:div ul li{border:1px solid red;}&l ...
- android的color整理(一)
很全面的颜色收集方便以后使用. <!-- 白色 --> <color name="ivory">#fffff0</color> <!-- ...
- MySQL如何使用索引 较为详细的分析和例子
在数据库表中,使用索引可以大大提高查询速度. 假如我们创建了一个 testIndex 表: CREATE TABLE testIndex(i_testID INT NOT NULL,vc_Name V ...
- [ZT]DAS\NAS\IP SAN\FC SAN之区别
DAS:服务器直接后挂存储设备,最经济的一种结构. NAS:网络上直接挂接的存储设备,其实就是处于以太网上的一台利用NFS.CIFS等网络文件系统的文件共享服务器. SAN是网络上的磁盘,NAS是一个 ...
- php中json_encode中文编码问题
1 <?php 2 class myClass { 3 public$item1=1; 4 public$item2='中文'; 5 6 function to_json() { 7 //url ...
- Cordova+Asp.net Mvc+GIS
Cordova+Asp.net Mvc+GIS跨平台移动应用开发实战1-系统初步搭建(附演示,apk,全部源码) 1.前言 身处在移动互联网的今天,移动应用开发炙手可热,身为程序猿的我们怎么能错过 ...
- OpenStreetMap(OSM) for developers
This article from: http://wiki.openstreetmap.org/wiki/Develop OpenStreetMap isn't just open data - i ...