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
新闻热点
疑难解答
图片精选