主题:[讨论]由vb.net改写的C#电子时钟
SevenSegment.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Num_clock
{
/// <summary>
/// SevenSegmentDisplay 的摘要说明。
/// </summary>
public class SevenSegmentDisplay
{ 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}};
//定义7个片断的点
Point[][] apt=new Point[7][];
Graphics grfx;
int numapt;
public SevenSegmentDisplay(Graphics grfx)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.grfx=grfx;
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)};
numapt=apt.Length;
}
public SizeF MeasureString(String str,Font fnt)
{
SizeF szf=new SizeF(0,this.grfx.DpiX*fnt.SizeInPoints/72);
foreach( Char ch in str)
{
if(Char.IsDigit(ch))
{
szf.Width+=42*grfx.DpiX*fnt.SizeInPoints/72/72;
}
else if(ch.Equals(':'))
{
szf.Width+=12*grfx.DpiX*fnt.SizeInPoints/72/72;
}
}
return szf;
}
public void DrawString(string str,Font fnt,Brush br,Single x,Single y)
{
foreach(char ch in str)
{
if(char.IsDigit(ch))
{
x=Number((int)(ch-'0'),fnt,br,x,y);
}
else if(ch.Equals(':'))
{
x=Colon(fnt,br,x,y);
}
}
}
public Single Number(int num,Font fnt,Brush br,Single x,Single y)
{
int i;
for(i=0;i<numapt;i++)
{
if(bySegment[num,i]==1)
{
Fill(apt[i],fnt,br,x,y);
}
}
return x+42*grfx.DpiX*fnt.SizeInPoints/72/72;
}
public Single Colon(Font fnt,Brush br,Single x,Single y)
{
int i;
Point[][] apt=new Point[2][];
int ss=apt.GetUpperBound(0);
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(i=0;i<ss;i++)
{
Fill(apt[i],fnt,br,x,y);
}
return x+12*grfx.DpiX*fnt.SizeInPoints/72/72;
}
public void Fill(Point[] apt,Font fnt,Brush br,Single x,Single y)
{
int i;
PointF[] aptf=new PointF[apt.GetUpperBound(0)];
for(i=0;i<apt.GetUpperBound(0);i++)
{
aptf[i].X=x+apt[i].X *grfx.DpiX*fnt.SizeInPoints/72/72;
aptf[i].Y=y+apt[i].Y*grfx.DpiX*fnt.SizeInPoints/72/72;
}
grfx.FillPolygon(br,aptf);
}
}
}
Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Globalization;
namespace Num_clock
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private DateTime dt;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text="Seven-Segment Colck";
this.BackColor=SystemColors.Window;
this.ForeColor=SystemColors.WindowText;
Timer tmr=new Timer();
tmr.Tick+=new EventHandler(tmr_Tick);
tmr.Interval=1000;
tmr.Enabled=true;
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void tmr_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;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
SevenSegmentDisplay ssd=new SevenSegmentDisplay(e.Graphics);
string strTime=dt.ToString("T",DateTimeFormatInfo.InvariantInfo);
SizeF szf=ssd.MeasureString(strTime,this.Font);
Single fScale=Math.Min(ClientSize.Width/szf.Width,ClientSize.Height/szf.Height);
Font fnt=new Font(Font.FontFamily,fScale*Font.SizeInPoints);
szf=ssd.MeasureString(strTime,fnt);
ssd.DrawString(strTime,fnt,Brushes.Red,(ClientSize.Width-szf.Width)/2,(ClientSize.Height-szf.Height)/2);
}
}
}
首先在用csc 编译dll文件:csc /t:library sevensegmentdisplay.cs
生成sevensegmentdisplay.dll
在编译form1.cs生成winexe文件
csc /t:winexe /r:sevensegmentdisplay.dll form1.cs
using System;
using System.Drawing;
using System.Windows.Forms;
namespace Num_clock
{
/// <summary>
/// SevenSegmentDisplay 的摘要说明。
/// </summary>
public class SevenSegmentDisplay
{ 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}};
//定义7个片断的点
Point[][] apt=new Point[7][];
Graphics grfx;
int numapt;
public SevenSegmentDisplay(Graphics grfx)
{
//
// TODO: 在此处添加构造函数逻辑
//
this.grfx=grfx;
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)};
numapt=apt.Length;
}
public SizeF MeasureString(String str,Font fnt)
{
SizeF szf=new SizeF(0,this.grfx.DpiX*fnt.SizeInPoints/72);
foreach( Char ch in str)
{
if(Char.IsDigit(ch))
{
szf.Width+=42*grfx.DpiX*fnt.SizeInPoints/72/72;
}
else if(ch.Equals(':'))
{
szf.Width+=12*grfx.DpiX*fnt.SizeInPoints/72/72;
}
}
return szf;
}
public void DrawString(string str,Font fnt,Brush br,Single x,Single y)
{
foreach(char ch in str)
{
if(char.IsDigit(ch))
{
x=Number((int)(ch-'0'),fnt,br,x,y);
}
else if(ch.Equals(':'))
{
x=Colon(fnt,br,x,y);
}
}
}
public Single Number(int num,Font fnt,Brush br,Single x,Single y)
{
int i;
for(i=0;i<numapt;i++)
{
if(bySegment[num,i]==1)
{
Fill(apt[i],fnt,br,x,y);
}
}
return x+42*grfx.DpiX*fnt.SizeInPoints/72/72;
}
public Single Colon(Font fnt,Brush br,Single x,Single y)
{
int i;
Point[][] apt=new Point[2][];
int ss=apt.GetUpperBound(0);
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(i=0;i<ss;i++)
{
Fill(apt[i],fnt,br,x,y);
}
return x+12*grfx.DpiX*fnt.SizeInPoints/72/72;
}
public void Fill(Point[] apt,Font fnt,Brush br,Single x,Single y)
{
int i;
PointF[] aptf=new PointF[apt.GetUpperBound(0)];
for(i=0;i<apt.GetUpperBound(0);i++)
{
aptf[i].X=x+apt[i].X *grfx.DpiX*fnt.SizeInPoints/72/72;
aptf[i].Y=y+apt[i].Y*grfx.DpiX*fnt.SizeInPoints/72/72;
}
grfx.FillPolygon(br,aptf);
}
}
}
Form1.cs
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Globalization;
namespace Num_clock
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
private DateTime dt;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.Size = new System.Drawing.Size(300,300);
this.Text="Seven-Segment Colck";
this.BackColor=SystemColors.Window;
this.ForeColor=SystemColors.WindowText;
Timer tmr=new Timer();
tmr.Tick+=new EventHandler(tmr_Tick);
tmr.Interval=1000;
tmr.Enabled=true;
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void tmr_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;
this.Invalidate();
}
}
protected override void OnPaint(PaintEventArgs e)
{
SevenSegmentDisplay ssd=new SevenSegmentDisplay(e.Graphics);
string strTime=dt.ToString("T",DateTimeFormatInfo.InvariantInfo);
SizeF szf=ssd.MeasureString(strTime,this.Font);
Single fScale=Math.Min(ClientSize.Width/szf.Width,ClientSize.Height/szf.Height);
Font fnt=new Font(Font.FontFamily,fScale*Font.SizeInPoints);
szf=ssd.MeasureString(strTime,fnt);
ssd.DrawString(strTime,fnt,Brushes.Red,(ClientSize.Width-szf.Width)/2,(ClientSize.Height-szf.Height)/2);
}
}
}
首先在用csc 编译dll文件:csc /t:library sevensegmentdisplay.cs
生成sevensegmentdisplay.dll
在编译form1.cs生成winexe文件
csc /t:winexe /r:sevensegmentdisplay.dll form1.cs