C#时钟控件的创建与使用

              C#时钟控件的创建与使用

 

简介:效果如图。 

 

       本文假定读者熟悉基本的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、  详细步骤  

首先创建一个工程命名为AnalogClockControl.选择Windows Control Library为模板。

从工程中删除usercontrol1.cs文件,并向其中新增加一个类。

 

向你的控件中拖入一个timer控件,将其Interval属性设为1000(每1000毫秒发出一个WM_TIMER消息)。

 

下面开始编程,类的架构:

class AnalogClock : System.Windows.Forms.UserControl

{

    const float PI=3.141592654F;

 

    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/1.65F;

        this.fMinLength = (float)this.Height/3/1.20F;

        this.fSecLength = (float)this.Height/3/1.15F;

        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/60F) *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/1.50F*System.Math.Sin(i*6*PI/180) ),

                  fCenterY -

                  (float)( this.fRadius/1.50F*System.Math.Cos(i*6*PI/180) ),

                  fCenterX +

                  (float)( this.fRadius/1.65F*System.Math.Sin(i*6*PI/180) ),

                  fCenterY -

                  (float)( this.fRadius/1.65F*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/1.50F*System.Math.Sin(i*6*PI/180) ),

                  fCenterY -

                  (float) ( this.fRadius/1.50F*System.Math.Cos(i*6*PI/180) ),

                  fCenterX +

                  (float) ( this.fRadius/1.55F*System.Math.Sin(i*6*PI/180) ),

                  fCenterY -

                  (float) ( this.fRadius/1.55F*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…

在其后出现的对话框中选browse钮,选择相应的dll,相应的时钟控件即出现在工具栏中,我们可以对它进行任意拖动。Enjoy it !

你可能感兴趣的:(C#时钟控件的创建与使用)