LED显示时间

 

using  System;
using  System.Collections.Generic;
using  System.Text;
using  System.Drawing;
using  System.Windows.Forms;

namespace  testgrid
{
    
class SevenSegmentDisplay
    
{
        Graphics g;
        
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 SevenSegmentDisplay(Graphics g)
        
{
            
this.g = g;
            apt[
0= new Point[] new Point(32), new Point(392), new Point(3110), new Point(1110) };
            apt[
1= new Point[] new Point(23), new Point(1011), new Point(1031), new Point(235) };
            apt[
2= new Point[] new Point(403), new Point(4035), new Point(3231), new Point(3211) };
            apt[
3= new Point[] new Point(336), new Point(1132), new Point(3132), new Point(3936), new Point(3140), new Point(1140) };
            apt[
4= new Point[] new Point(237), new Point(1041), new Point(1061), new Point(269) };
            apt[
5= new Point[] new Point(4037), new Point(4069), new Point(3261), new Point(3241) };
            apt[
6= new Point[] new Point(1162), new Point(3162), new Point(3970), new Point(370) };
        }

        
public SizeF MeasureString(string str,Font font)
        
{
            SizeF sizef 
= new SizeF(0, g.DpiX * font.SizeInPoints / 72);
            
for (int i = 0; i < str.Length; i++)
            
{
                
if (char.IsDigit(str[i]))
                    sizef.Width 
+= 42 * g.DpiX * font.SizeInPoints / 72 / 72;
                
else if (str[i] == ':')
                    sizef.Width 
+= 12 * g.DpiX * font.SizeInPoints / 72 / 72;
            }

            
return sizef;
        }

        
public void DrawString(string str, Font font, Brush br, float x, float y)
        
{
            
for (int i = 0; i < str.Length; i++)
            
{
                
if (char.IsDigit(str[i]))
                
{
                    x 
= Number(str[i] - '0', font, br, x, y);
                }

                
else if (str[i] == ':')
                    x 
= Colon(font, br, x, y);
            }

        }

        
float Number(int num, Font font, Brush br, float x, float y)
        
{
            
for (int i = 0; i < apt.Length; i++)
            
{
                
if (bySegment[num, i] == 1)
                    Fill(apt[i], font, br, x, y);
            }

            
return x + 42 * g.DpiX * font.SizeInPoints / 72 / 72;
        }

        
float Colon(Font font, Brush br, float x, float y)
        
{
            Point[][] apt 
= new Point[2][];
            apt[
0= new Point[] new Point(221), new Point(617), new Point(1021), new Point(625) };
            apt[
1= new Point[] new Point(251), new Point(647), new Point(1051), new Point(655) };
            
for (int i = 0; i < apt.Length; i++)
                Fill(apt[i], font, br, x, y);
            
return x + 12 * g.DpiX * font.SizeInPoints / 72 / 72;
        }

        
void Fill(Point[] apt, Font font, Brush br, 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 * g.DpiX * font.SizeInPoints / 72 / 72;
                aptf[i].Y 
= y + apt[i].Y * g.DpiY * font.SizeInPoints / 72 / 72;
            }

            g.FillPolygon(br, aptf);
        }

    }

}

窗体代码:

using  System;
using  System.Collections.Generic;
using  System.ComponentModel;
using  System.Data;
using  System.Drawing;
using  System.Text;
using  System.Windows.Forms;
using  System.Globalization;

namespace  testgrid
{
    
public partial class SevenSegmentClock : Form
    
{
        DateTime dt;
        
public SevenSegmentClock()
        
{
            InitializeComponent();
            BackColor 
= Color.White;
            ResizeRedraw 
= true;
            MinimumSize 
= SystemInformation.MinimizedWindowSize + new Size(01);
            dt 
= DateTime.Now;
            Timer timer 
= new Timer();
            timer.Tick
+=new EventHandler(timer_Tick);
            timer.Interval 
= 100;
            timer.Enabled 
= true;
        }


        
void timer_Tick(object sender, EventArgs e)
        
{
            DateTime dtnow 
= DateTime.Now;
            dtnow 
= new DateTime(dtnow.Year, dtnow.Month, dtnow.Day, dtnow.Hour, dtnow.Minute, dtnow.Second);
            
if (dtnow != dt)
            
{
                dt 
= dtnow;
                Invalidate();
            }

        }

        
protected override void OnPaint(PaintEventArgs e)
        
{
            SevenSegmentDisplay ssd 
= new SevenSegmentDisplay(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);
            ssd.DrawString(strTime, font, Brushes.Red, (ClientSize.Width 
- sizef.Width) / 2, (ClientSize.Height - sizef.Height) / 2);

        }

    }

}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值