首先建立数字显示类:

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的更多相关文章

  1. 模拟时钟(AnalogClock)和数字时钟(DigitalClock)

    Demo2\clock_demo\src\main\res\layout\activity_main.xml <LinearLayout xmlns:android="http://s ...

  2. C#开发漂亮的数字时钟

    今天用C#做了一个漂亮的数字时钟.界面如下. 实现技术:主要是通过Graphics类的DrawImage方法来绘制数字时钟中所有的数字,这些数字是从网上找的一些图片文件.时钟使用DateTime中No ...

  3. Qt仿Android带特效的数字时钟源码分析(滑动,翻页,旋转效果)

    这个数字时钟的源码可以在Qt Demo中找到,风格是仿Android的,不过该Demo中含有三种动画效果(鉴于本人未曾用过Android的系统,因此不知道Android的数字时钟是否也含有这三种效果) ...

  4. android脚步---数字时钟和模拟时钟

    时钟UI组件是两个非常简单的组件,分为Digitalclock  和Analogclock, main.xml文件,书中程序有问题,加了两个组件,一个Button和一个<Chronometer ...

  5. 基于Verilog HDL 的数字时钟设计

    基于Verilog HDL的数字时钟设计 一.实验内容:     利用FPGA实现数字时钟设计,附带秒表功能及时间设置功能.时间设置由开关S1和S2控制,分别是增和减.开关S3是模式选择:0是正常时钟 ...

  6. HandlerThread实现数字时钟

    1.描述 刚看完Android多线程编程,对HandlerThread比较感兴趣,趁热巩固练习,实现一个了数字时钟,希望对学习HandlerThread有所帮助.如下: 启动一个HandlerThre ...

  7. js动态数字时钟

    js动态数字时钟 主要用到知识点: 主要是通过数组的一些方法,如:Array.from() Array.reduce() Array.find() 时间的处理和渲染 js用到面向对象的写法 实现的功能 ...

  8. 简单酷炫的Canvas数字时钟

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 我记得很早之前就看过这个DEMO,是岑安大大博客里看到的: 就是这个数字时钟,当时觉得这个创意不错,但是也没去折腾.直到昨天同事又在网上看 ...

  9. 使用jQuery和CSS3实现一个数字时钟

    点击进入更详细教程及源码下载     在线演示 我们经常会在网站中看见一个时钟的效果.今天向大家分享一个使用jQuery和CSS3实现一个数字时钟教程. http://www.html5cn.org/ ...

随机推荐

  1. Oracle日志文件常用操作

    Oracle关于日志文件基本操作1.查询系统使用的是哪一组日志文件:select * from v$log; 2.查询正在使用的组所对应的日志文件:select * from v$logfile; 3 ...

  2. (八)Android广播接收器BroadcastReceiver

    一.使用Broadcast Reciver 1.右击java文件夹,new->other->Broadcast Receiver后会在AndroidManifest.xml文件中生成一个r ...

  3. 关于C#重写,隐藏的一些事

    第一次开始写技术博客,不知该从何处下手,本人算是菜鸟一枚,每每看到博客园里面的大牛们分享的技术文章,只能望其项背,高不可攀.但细细想来,若不尝试着从小处从低处慢慢去积累分享,想要成为技术大牛也只能沦为 ...

  4. Java环境配置原理

    Java环境配置原理详解 1.Jdk安装目录文件说明: 一般jdk安装目录及路径 \Java\jdk1.7.0_79\lib,里面主要包含以下文件夹. bin:主要存放的是java工具中常用命令如:j ...

  5. C++容器在遍历时的删除问题

    容器是非常便捷常用的,经常用容器来存储多条数据,然后对数据进行增删查改. 有时要在遍历的同时删除一条数据,但是这样删除的时候程序会导致程序崩溃. 这个问题在GCC 中不会出现,而在VS2008,VS2 ...

  6. codeforces 几道题目

    BZOJ挂了....明天就要出发去GDKOI了....不能弃疗. 于是在cf水了几道题, 写写详(jian)细(dan)题解, 攒攒RP, 希望GDKOI能好好发挥.......  620E. New ...

  7. vim highlight whitespace at end of line and auto delete them

    install Vundle.vim mkdir ~/.vim/bundle git clone https://github.com/gmarik/Vundle.vim.git ~/.vim/bun ...

  8. 本地网址连不上远程mysql问题

    问题:host 'XXX.XXX.XXX.XXX'is not allowed to connect to this MySQL server 解决办法: 进入远程mysql #mysql -u ro ...

  9. 浏览器的重绘repaints与重排reflows深入分析

    重绘是一个元素外观的改变所触发的浏览器行为,浏览器会根据元素的新属性重新绘制,使元素呈现新的外观,接下来将详细介绍,需要了解的朋友可以参考下: 在项目的交互或视觉评审中,前端同学常常会对一些交互效果质 ...

  10. ARM 7 用户模式下禁止/使能中断的一种方法--使用软中断 for Keil MDK

    最近写一个程序,需要在用户模式下关中断,但ARM 7的体系结构决定了中断必须在特权模式下才可以更改,所以想到使用ARM的软中断来实现关中断和开中断. 使用软中断,首先要有硬件指令的支持.ARM有条指令 ...