C#
时钟控件的创建与使用
<shapetype id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t" path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"></path><lock v:ext="edit" aspectratio="t"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 162pt; HEIGHT: 173.25pt" type="#_x0000_t75"><imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image001.png" o:title="" croptop="14473f" cropbottom="16459f" cropleft="9926f" cropright="29999f"></imagedata></shape>
简介:效果如图。
本文假定读者熟悉基本的
C#
编程过程,但对建立控件的过程不太了解,以一个时钟控件为例,详细描述了开发过程,希望对大家有所帮助。
1、
时间变换部分
-----
只需要比较简单的数学知识
首先取得当前时间
int hour = dateTime.Hour % 12; //
将小时数(
24
基)转换成
12
基的
int minute = dateTime.Minute;
int sec = dateTime.Second;
将这些数据转换成相应的弧度(因为
sin
所)
float hourRadian = hour * 360/12 * PI/180;
float minRadian = minute * 360/60 * PI/180;
float secRadian = sec * 360/60 * PI/180;
根据弧度得出了时针的端点
float hourEndPointX = lengthofHourHand * System.Math.Sin(hourRadian)
float hourEndPointY = lengthofHourHand * System.Math.Cos(hourRadian)
则时针画法为
Line(centerX, centerY, hourEndPointX, hourEndPointY)
分针、秒针以此类类推。
2、
详细步骤
<shape id="_x0000_i1026" style="WIDTH: 219pt; HEIGHT: 150pt" type="#_x0000_t75"><imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image003.png" o:title="" croptop="15851f" cropbottom="20209f" cropleft="15178f" cropright="15732f"><font face="Times New Roman" size="3"></font></imagedata></shape>
首先创建一个工程命名为
AnalogClockControl.
选择
Windows Control Library
为模板。
从工程中删除
usercontrol1.cs
文件,并向其中新增加一个类。
<shape id="_x0000_i1027" style="WIDTH: 219pt; HEIGHT: 246.75pt" type="#_x0000_t75"><imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image005.png" o:title="" cropbottom="29412f" cropleft="39843f"><font size="3"></font></imagedata></shape>
向你的控件中拖入一个
timer
控件,将其
Interval
属性设为
1000
(每
1000
毫秒发出一个
WM_TIMER
消息)。
下面开始编程
,
类的架构:
class AnalogClock : System.Windows.Forms.UserControl
{
const float PI=<chmetcnv unitname="F" sourcevalue="3.141592654" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">3.141592654F</chmetcnv>;
DateTime dateTime;
float fRadius;
float fCenterX;
float fCenterY;
float fCenterCircleRadius;
float fHourLength;
float fMinLength;
float fSecLength;
float fHourThickness;
float fMinThickness;
float fSecThickness;
bool bDraw5MinuteTicks=true;
bool bDraw1MinuteTicks=true;
float fTicksThickness=1;
Color hrColor=Color.DarkMagenta ;
Color minColor=Color.Green ;
Color secColor=Color.Red ;
Color circleColor=Color.Red;
Color ticksColor=Color.Black;
...
...
为了确保控件被初次加载及被改变大小时都能正常显示时间,我们有必要在
Load
与
Resize
事件发生时予以响应。
程序如下:
private void AnalogClock_Load(object sender, System.EventArgs e)
{
dateTime=DateTime.Now;
this.AnalogClock_Resize(sender,e);
}
private void AnalogClock_Resize(object sender, System.EventArgs e)
{
this.Width = this.Height;
this.fRadius = this.Height/2;
this.fCenterX = this.ClientSize.Width/2;
this.fCenterY = this.ClientSize.Height/2;
this.fHourLength = (float)this.Height/3/<chmetcnv unitname="F" sourcevalue="1.65" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.65F</chmetcnv>;
this.fMinLength = (float)this.Height/3/<chmetcnv unitname="F" sourcevalue="1.2" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.20F</chmetcnv>;
this.fSecLength = (float)this.Height/3/<chmetcnv unitname="F" sourcevalue="1.15" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.15F</chmetcnv>;
this.fHourThickness = (float)this.Height/100;
this.fMinThickness = (float)this.Height/150;
this.fSecThickness = (float)this.Height/200;
this.fCenterCircleRadius = this.Height/50;
this.Refresh();
}
为完成时钟效果,程序应该每秒钟刷新一次时、分、秒针的位置。双击
timer
控件,在其响应函数中加入如下代码:
private void timer1_Tick(object sender, System.EventArgs e)
{
this.dateTime=DateTime.Now;
this.Refresh();
}
下面来写一下两个与图形有关的函数,
DrawLine()
与
DrawPolygon(),
它们的作用是在窗体上画出秒针与时分针。
代码如下:
private void DrawLine(float fThickness, float fLength, Color color,
float fRadians, System.Windows.Forms.PaintEventArgs e)
{
e.Graphics.DrawLine(new Pen( color, fThickness ),
fCenterX - (float)(fLength/9*System.Math.Sin(fRadians)),
fCenterY + (float)(fLength/9*System.Math.Cos(fRadians)),
fCenterX + (float)(fLength*System.Math.Sin(fRadians)),
fCenterY - (float)(fLength*System.Math.Cos(fRadians)));
}
private void DrawPolygon(float fThickness, float fLength, Color color,
float fRadians, System.Windows.Forms.PaintEventArgs e)
{
PointF A=new PointF( (float)(fCenterX+
fThickness*2*System.Math.Sin(fRadians+PI/2)),
(float)(fCenterY -
fThickness*2*System.Math.Cos(fRadians+PI/2)) );
PointF B=new PointF( (float)(fCenterX+
fThickness*2*System.Math.Sin(fRadians-PI/2)),
(float)(fCenterY -
fThickness*2*System.Math.Cos(fRadians-PI/2)) );
PointF C=new PointF( (float)(fCenterX+
fLength*System.Math.Sin(fRadians)),
(float) (fCenterY -
fLength*System.Math.Cos(fRadians)) );
PointF D=new PointF( (float)(fCenterX-
fThickness*4*System.Math.Sin(fRadians)),
(float)(fCenterY +
fThickness*4*System.Math.Cos(fRadians) ));
PointF[] points={A,D,B,C};
e.Graphics.FillPolygon( new SolidBrush(color), points );
}
为了使控件的用户能够控制这个时钟控件的启停,我们在其中加入另外两个函数。
public void Start()
{
timer1.Enabled=true;
this.Refresh();
}
public void Stop()
{
timer1.Enabled=false;
}
控件对于
Paint
消息的响应函数是这个控件中最核心的程序,完成了时间提取、弧度转换及画指针这一系列活动。
代码如下:
private void AnalogClock_Paint(object sender,
System.Windows.Forms.PaintEventArgs e)
{
float fRadHr=(dateTime.Hour%12+dateTime.Minute/<chmetcnv unitname="F" sourcevalue="60" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">60F</chmetcnv>) *30*PI/180;
float fRadMin=(dateTime.Minute)*6*PI/180;
float fRadSec=(dateTime.Second)*6*PI/180;
DrawPolygon(this.fHourThickness,
this.fHourLength, hrColor, fRadHr, e);
DrawPolygon(this.fMinThickness,
this.fMinLength, minColor, fRadMin, e);
DrawLine(this.fSecThickness,
this.fSecLength, secColor, fRadSec, e);
for(int i=0;i<60;i++)
{
if ( this.bDraw5MinuteTicks==true && i%5==0 )
// Draw 5 minute ticks
{
e.Graphics.DrawLine( new Pen( ticksColor, fTicksThickness ),
fCenterX +
(float)( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.5" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.50F</chmetcnv>*System.Math.Sin(i*6*PI/180) ),
fCenterY -
(float)( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.5" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.50F</chmetcnv>*System.Math.Cos(i*6*PI/180) ),
fCenterX +
(float)( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.65" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.65F</chmetcnv>*System.Math.Sin(i*6*PI/180) ),
fCenterY -
(float)( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.65" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.65F</chmetcnv>*System.Math.Cos(i*6*PI/180)) );
}
else if ( this.bDraw1MinuteTicks==true ) // draw 1 minute ticks
{
e.Graphics.DrawLine( new Pen( ticksColor, fTicksThickness ),
fCenterX +
(float) ( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.5" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.50F</chmetcnv>*System.Math.Sin(i*6*PI/180) ),
fCenterY -
(float) ( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.5" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.50F</chmetcnv>*System.Math.Cos(i*6*PI/180) ),
fCenterX +
(float) ( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.55" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.55F</chmetcnv>*System.Math.Sin(i*6*PI/180) ),
fCenterY -
(float) ( this.fRadius/<chmetcnv unitname="F" sourcevalue="1.55" hasspace="False" negative="False" numbertype="1" tcsc="0" w:st="on">1.55F</chmetcnv>*System.Math.Cos(i*6*PI/180) ) );
}
}
//draw circle at center
e.Graphics.FillEllipse( new SolidBrush( circleColor ),
fCenterX-fCenterCircleRadius/2,
fCenterY-fCenterCircleRadius/2,
fCenterCircleRadius, fCenterCircleRadius);
}
最后,还须为此控件设置个三个属性,使用户可以更改其颜色。
代码如下:
public Color HourHandColor
{
get { return this.hrColor; }
set { this.hrColor=value; }
}
public Color MinuteHandColor
{
get { return this.minColor; }
set { this.minColor=value; }
}
public Color SecondHandColor
{
get { return this.secColor; }
set { this.secColor=value;
this.circleColor=value; }
}
3、
在其它工程中应用此控件
在对上述控件工程进行正确编译后,得到一个
DLL
文件,可以将其拷入其它任意工程文件夹中,右键工具栏,选
choose Items…
<shape id="_x0000_i1028" style="WIDTH: 157.5pt; HEIGHT: 228pt" type="#_x0000_t75"><imagedata src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image007.png" o:title="" cropbottom="36222f" cropright="49314f"><font face="Times New Roman" size="3"></font></imagedata></shape>
在其后出现的对话框中选
browse
钮,选择相应的
dll,
相应的时钟控件即出现在工具栏中,我们可以对它进行任意拖动。
Enjoy it !
译自:
例程下载:
C#时钟控件的创建与使用