C#自定义控件四简易时钟

C#自定义控件简易时钟

效果图:

简易时钟,顾名思义,简单容易,简单到什么程度呢?界面只有数字和指针,甚至连与当前时间都不能匹配!呵呵!就这么简单,学习嘛,从简单开始。

毫无疑问,这里肯定要用到Timer控件啦,每秒钟刷新界面,这样的话就要求双缓冲啦,你懂的。

第一步:添加用户控件,命名为myClock。

第二步:构造函数实例化控件和做双缓冲处理。

public myClock()

        {

            InitializeComponent();

            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            this.SetStyle(ControlStyles.DoubleBuffer, true);

            this.SetStyle(ControlStyles.ResizeRedraw, true);

            this.SetStyle(ControlStyles.Selectable, true);

            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.SetStyle(ControlStyles.UserPaint, true);

            myTimer = new Timer();

            myTimer.Interval = 1000;

            myTimer.Enabled = true;

            myTimer.Tick += new EventHandler(myTimer_Tick);

        }

第三步:Paint重绘事件

private void myClock_Paint(object sender, PaintEventArgs e)

        {

            Graphics gra = e.Graphics;//创建一个画板

            /*********************************/

            //表盘上的指针

            using (SolidBrush sb = new SolidBrush(Color.Blue))

            {

                Font f = new Font("宋体", 18);

                gra.DrawString("1", f, sb, 195, 20);

                gra.DrawString("2", f, sb, 235, 60);

                gra.DrawString("3", f, sb, 250, 123);

                gra.DrawString("4", f, sb, 235, 183);

                gra.DrawString("5", f, sb, 195, 225);

                gra.DrawString("6", f, sb, 125, 245);

                gra.DrawString("7", f, sb, 55, 225);

                gra.DrawString("8", f, sb, 15, 183);

                gra.DrawString("9", f, sb, 0, 123);

                gra.DrawString("10", f, sb, 15, 60);

                gra.DrawString("11", f, sb, 55, 20);

                gra.DrawString("12", f, sb, 125, 0);

            }

            

            //表盘的框架:黑色,宽度是3

            using (Pen p = new Pen(Color.Black, 3))

            {

                p.DashStyle = DashStyle.Solid;

                gra.DrawEllipse(p, 0, 0, 270, 270);

            }

            

            //表盘上的三个指针i,j,k;

            using (Pen p1 = new Pen(Color.Red))

            {

                using (Pen p2 = new Pen(Color.Blue))

                {

                    using (Pen p3 = new Pen(Color.Yellow))

                    {

                        gra.DrawPie(p3, 20, 20, 230, 230, k, 0.1f);//秒          

                        gra.DrawPie(p2, 30, 30, 210, 210, j, 0.1f);//分

                        gra.DrawPie(p1, 40, 40, 190, 190, i, 0.1f);//小时

                    }

                }

            }

            

            //指针旋转的代码

            if (i < 360)

            {

                i += 6;

                if (i == 270)

                {

                    if (j <= 360)

                    {

                        j += 6;

                        if (j == 270)

                        {

                            if (k <= 360)

                            {

                                k += 30;

                            }

                            else

                            {

                                k = 30;

                            }

                        }

                    }

                    else

                    {

                        j = 6;

                    }

                }

            }

            else

            {

                i = 6;

            }

        }

贴出所有代码:

//控件名:myClock

//作者:刘典武

//时间:2011-06-09

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Text;

using System.Windows.Forms;

using System.Drawing.Drawing2D;

namespace myControl

{

    public partial class myClock : UserControl

    {

        public myClock()

        {

            InitializeComponent();

            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);

            this.SetStyle(ControlStyles.DoubleBuffer, true);

            this.SetStyle(ControlStyles.ResizeRedraw, true);

            this.SetStyle(ControlStyles.Selectable, true);

            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.SetStyle(ControlStyles.UserPaint, true);

            myTimer = new Timer();

            myTimer.Interval = 1000;

            myTimer.Enabled = true;

            myTimer.Tick += new EventHandler(myTimer_Tick);

        }

        private Timer myTimer;

        //定义三个变量

        float i = 270, j = 270, k = 270;

        private void myTimer_Tick(object sender, EventArgs e)

        {

            this.Invalidate();           

        }

        private void myClock_Paint(object sender, PaintEventArgs e)

        {

            Graphics gra = e.Graphics;//创建一个画板

            /*********************************/

            //表盘上的指针

            using (SolidBrush sb = new SolidBrush(Color.Blue))

            {

                Font f = new Font("宋体", 18);

                gra.DrawString("1", f, sb, 195, 20);

                gra.DrawString("2", f, sb, 235, 60);

                gra.DrawString("3", f, sb, 250, 123);

                gra.DrawString("4", f, sb, 235, 183);

                gra.DrawString("5", f, sb, 195, 225);

                gra.DrawString("6", f, sb, 125, 245);

                gra.DrawString("7", f, sb, 55, 225);

                gra.DrawString("8", f, sb, 15, 183);

                gra.DrawString("9", f, sb, 0, 123);

                gra.DrawString("10", f, sb, 15, 60);

                gra.DrawString("11", f, sb, 55, 20);

                gra.DrawString("12", f, sb, 125, 0);

            }

            

            //表盘的框架:黑色,宽度是3

            using (Pen p = new Pen(Color.Black, 3))

            {

                p.DashStyle = DashStyle.Solid;

                gra.DrawEllipse(p, 0, 0, 270, 270);

            }

            

            //表盘上的三个指针i,j,k;

            using (Pen p1 = new Pen(Color.Red))

            {

                using (Pen p2 = new Pen(Color.Blue))

                {

                    using (Pen p3 = new Pen(Color.Yellow))

                    {

                        gra.DrawPie(p3, 20, 20, 230, 230, k, 0.1f);//秒          

                        gra.DrawPie(p2, 30, 30, 210, 210, j, 0.1f);//分

                        gra.DrawPie(p1, 40, 40, 190, 190, i, 0.1f);//小时

                    }

                }

            }

            

            //指针旋转的代码

            if (i < 360)

            {

                i += 6;

                if (i == 270)

                {

                    if (j <= 360)

                    {

                        j += 6;

                        if (j == 270)

                        {

                            if (k <= 360)

                            {

                                k += 30;

                            }

                            else

                            {

                                k = 30;

                            }

                        }

                    }

                    else

                    {

                        j = 6;

                    }

                }

            }

            else

            {

                i = 6;

            }

        }        

    }

}

posted @ 2011-07-05 08:38  飞扬青云  阅读(6107)  评论(3编辑  收藏  举报