c#改写的,vb.net模拟时钟

ClockControl.cs

using System;

using System.Windows.Forms;

using System.Drawing;

using System.Drawing.Drawing2D;

namespace ClockTime

{

/// <summary>

/// ClockControl 的摘要说明。

/// </summary>

public class ClockControl:System.Windows.Forms.UserControl

{

private DateTime dt;

public ClockControl()

{

//

// TODO: 在此处添加构造函数逻辑

//

this.ResizeRedraw=true;

this.Enabled=false;

}

public DateTime Time

{

set

{

Graphics grfx=this.CreateGraphics();

Pen pn=new Pen(this.BackColor);

InitializeCoordinates(grfx);

if(dt.Hour!=value.Hour)

{

DrawHourHand(grfx,pn);

}

if(dt.Minute!=value.Minute)

{

DrawHourHand(grfx,pn);

DrawMinuteHand(grfx,pn);

}

if(dt.Second!=value.Second)

{

DrawMinuteHand(grfx,pn);

DrawSecondHand(grfx,pn);

}

if(dt.Millisecond!=value.Millisecond)

{

DrawSecondHand(grfx,pn);

}

dt=value;

pn=new Pen(ForeColor);

DrawHourHand(grfx,pn);

DrawMinuteHand(grfx,pn);

DrawSecondHand(grfx,pn);

grfx.Dispose();

}

get

{return dt;

}

}

protected override void OnPaint(PaintEventArgs e)

{

Graphics grfx=e.Graphics;

Pen pn=new Pen(ForeColor);

SolidBrush br=new SolidBrush(ForeColor);

InitializeCoordinates(grfx);

DrawDots(grfx,br);

DrawHourHand(grfx,pn);

DrawSecondHand(grfx,pn);

DrawMinuteHand(grfx,pn);

}

protected virtual void InitializeCoordinates(Graphics grfx)

{

if(this.Width==0 || this.Height==0) return;

grfx.TranslateTransform(this.Width/2,this.Height/2);

Single fInches=Math.Min(this.Width/grfx.DpiX,this.Height/grfx.DpiY);

grfx.ScaleTransform(fInches*grfx.DpiX/2000,fInches*grfx.DpiY/2000);

}

protected virtual void DrawDots(Graphics grfx ,Brush br)

{

int i,iSize;

for(i=0;i<=59;i++)

{

if(i%5==0)

{

iSize=100;

}

else

iSize=30;

grfx.FillEllipse(br,0-iSize/2,-900-iSize/2,iSize,iSize);

grfx.RotateTransform(6);

}

}

protected virtual void DrawHourHand(Graphics grfx,Pen pn)

{

GraphicsState gs=grfx.Save();

grfx.RotateTransform(360.0F*Time.Hour/12+30.0F*Time.Minute/60);

grfx.DrawPolygon(pn,new Point[]{new Point(0,150),new Point(100,0),new Point(0,-600),new Point(-100,0)});

grfx.Restore(gs);

}

protected virtual void DrawMinuteHand(Graphics grfx,Pen pn)

{

GraphicsState gs=grfx.Save();

grfx.RotateTransform(360.0F*Time.Minute/60+6.0F*Time.Second/60);

grfx.DrawPolygon(pn,new Point[]{new Point(0,200),new Point(50,0),new Point(0,-800),new Point(-50,0)});

grfx.Restore(gs);

}

protected virtual void DrawSecondHand(Graphics grfx,Pen pn)

{

GraphicsState gs=grfx.Save();

grfx.RotateTransform(360.0F*Time.Second/60+6.0F*Time.Millisecond/1000);

grfx.DrawLine(pn,0,0,0,-800);

grfx.Restore(gs);

}

}

}

form1.cs

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

using ClockTime;

namespace ClockTime

{

/// <summary>

/// Form1 的摘要说明。

/// </summary>

public class Form1 : System.Windows.Forms.Form

{

private System.ComponentModel.IContainer components;

private System.Windows.Forms.Timer tmr;

private ClockTime.ClockControl clkctrl;

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.clkctrl = new ClockTime.ClockControl();

this.tmr = new System.Windows.Forms.Timer(this.components);

this.SuspendLayout();

//

// clkctrl

//

this.clkctrl.BackColor = System.Drawing.Color.Black;

this.clkctrl.Dock = System.Windows.Forms.DockStyle.Fill;

this.clkctrl.Enabled = false;

this.clkctrl.ForeColor = System.Drawing.Color.White;

this.clkctrl.Location = new System.Drawing.Point(0, 0);

this.clkctrl.Name = "clkctrl";

this.clkctrl.Size = new System.Drawing.Size(292, 273);

this.clkctrl.TabIndex = 0;

this.clkctrl.Time = new System.DateTime(2005, 6, 24, 10, 19, 26, 62);

//

// tmr

//

this.tmr.Enabled = true;

this.tmr.Interval = 1000;

this.tmr.Tick += new System.EventHandler(this.tmr_Tick);

//

// Form1

//

this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);

this.BackColor = System.Drawing.SystemColors.Window;

this.ClientSize = new System.Drawing.Size(292, 273);

this.Controls.Add(this.clkctrl);

this.ForeColor = System.Drawing.SystemColors.WindowText;

this.Name = "Form1";

this.Text = "ClockControl";

this.ResumeLayout(false);

}

#endregion

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void tmr_Tick(object sender, EventArgs e)

{

clkctrl.Time=DateTime.Now;

}

}

}

编译过程:

csc /t:library clockcontrol.cs

csc /t:winexe /r:clockcontrol.dll form1.cs