c# 精致时钟的代码

建立一个:项目-windows窗体应用程序

 

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            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 void myTimer_Tick(object sender, EventArgs e)
        {

            this.Invalidate();

        }

        private Timer myTimer;//定义时钟,定时重新绘制

        private Graphics g;//创建画布

        private Pen pen;//创建画笔      

        private int width;//画布高度

        private int height;//画布宽度

        Color hourColor = Color.Red;

        /// <summary>

        /// 时钟颜色

        /// </summary>

        [CategoryAttribute("颜色"), Description("时钟颜色")]

        public Color HourColor
        {

            get { return hourColor; }

            set { hourColor = value; }

        }

        Color minuteColor = Color.Green;

        /// <summary>

        /// 分钟颜色

        /// </summary>

        [CategoryAttribute("颜色"), Description("分钟颜色")]

        public Color MinuteColor
        {

            get { return minuteColor; }

            set { minuteColor = value; }

        }

        Color secondColor = Color.Blue;

        /// <summary>

        /// 秒钟颜色

        /// </summary>

        [CategoryAttribute("颜色"), Description("秒钟颜色")]

        public Color SecondColor
        {

            get { return secondColor; }

            set { secondColor = value; }

        }

        Color bigScaleColor = Color.DarkGreen;

        /// <summary>

        /// 大刻度颜色

        /// </summary>

        [CategoryAttribute("颜色"), Description("大刻度颜色")]

        public Color BigScaleColor
        {

            get { return bigScaleColor; }

            set { bigScaleColor = value; }

        }

        Color litterScaleColor = Color.Olive;

        /// <summary>

        /// 小刻度颜色

        /// </summary>

        [CategoryAttribute("颜色"), Description("小刻度颜色")]

        public Color LitterScaleColor
        {

            get { return litterScaleColor; }

            set { litterScaleColor = value; }

        }

        Color textColor = Color.White;

        /// <summary>

        /// 刻度值颜色

        /// </summary>

        [CategoryAttribute("颜色"), Description("刻度值颜色")]

        public Color TextColor
        {

            get { return textColor; }

            set { textColor = value; }

        }

        Color bigBackColor = Color.Black;

        /// <summary>

        /// 外圆背景色

        /// </summary>

        [CategoryAttribute("颜色"), Description("外圆背景颜色")]

        public Color BigBackColor
        {

            get { return bigBackColor; }

            set { bigBackColor = value; }

        }

        Color litterBackColor = Color.White;

        /// <summary>

        /// 内圆颜色

        /// </summary>

        [CategoryAttribute("颜色"), Description("内圆颜色")]

        public Color LitterBackColor
        {

            get { return litterBackColor; }

            set { litterBackColor = value; }

        }

        protected override void OnPaint(PaintEventArgs e)
        {

            base.OnPaint(e);

            g = e.Graphics;

            g.SmoothingMode = SmoothingMode.AntiAlias; //

            g.SmoothingMode = SmoothingMode.HighQuality;//绘图模式 默认为粗糙模式,将会出现锯齿!

            width = this.Width;//时钟宽度

            height = this.Height;//时钟高度

            int x1 = 0;//开始绘制时钟起点X坐标

            int y1 = 0;//开始绘制时钟起点Y坐标

            /*------------------------------------------------------------------------------

            计算:整点刻度12个,每个刻度偏移角度为360/12 = 30 度 及为小时偏移角度

                  分秒刻度为60个,每个刻度偏移角度为360/60 = 6 度 及为分、秒偏移角度

            --------------------------------------------------------------------------------*/

            g.FillEllipse(new SolidBrush(bigBackColor), x1 + 2, y1 + 2, width - 4, height - 4);  //外圆

            pen = new Pen(new SolidBrush(litterBackColor), 2);

            g.DrawEllipse(pen, x1 + 7, y1 + 7, width - 13, height - 13);// 内圆

            g.TranslateTransform(x1 + (width / 2), y1 + (height / 2));//重新设置坐标原点

            g.FillEllipse(Brushes.White, -5, -5, 10, 10);//绘制表盘中心

 

            for (int x = 0; x < 60; x++)  //小刻度
            {

                g.FillRectangle(new SolidBrush(litterScaleColor), new Rectangle(-2, (System.Convert.ToInt16(height - 8) / 2 - 2) * (-1), 3, 10));

                g.RotateTransform(6);//偏移角度

            }

            for (int i = 12; i > 0; i--)  //大刻度
            {

                string myString = i.ToString();

                //绘制整点刻度

                g.FillRectangle(new SolidBrush(bigScaleColor), new Rectangle(-3, (System.Convert.ToInt16(height - 8) / 2 - 2) * (-1), 6, 20));

                //绘制数值

                g.DrawString(myString, new Font(new FontFamily("Times New Roman"), 14, FontStyle.Bold, GraphicsUnit.Pixel), new SolidBrush(textColor), new PointF(myString.Length * (-6), (height - 8) / -2 + 26));

                //顺时针旋转30度

                g.RotateTransform(-30);//偏移角度

            }

            //获得系统时间值

            int second = DateTime.Now.Second;

            int minute = DateTime.Now.Minute;

            int hour = DateTime.Now.Hour;

            /*------------------------------------------------------------------------------------

            每秒偏移6度,秒针偏移=当前秒*6

            每分偏移6读,分针偏移 = 当前分*6+当前秒*(6/60)

            每小时偏移60读,时针偏移 = 当前时*30+当前分*(6/60)+当前秒*(6/60/60)

            --------------------------------------------------------------------------------------*/

            //绘秒针

            pen = new Pen(secondColor, 1);

            pen.EndCap = LineCap.ArrowAnchor;

            g.RotateTransform(6 * second);

            float y = (float)((-1) * (height / 2.75));

            g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));

            绘分针

            pen = new Pen(minuteColor, 4);

            pen.EndCap = LineCap.ArrowAnchor;

            g.RotateTransform(-6 * second);  //恢复系统偏移量,再计算下次偏移

            g.RotateTransform((float)(second * 0.1 + minute * 6));

            y = (float)((-1) * ((height - 30) / 2.75));

            g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));

            绘时针

            pen = new Pen(hourColor, 6);

            pen.EndCap = LineCap.ArrowAnchor;

            g.RotateTransform((float)(-second * 0.1 - minute * 6));//恢复系统偏移量,再计算下次偏移

            g.RotateTransform((float)(second * 0.01 + minute * 0.1 + hour * 30));

            y = (float)((-1) * ((height - 45) / 2.75));

            g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));

        }

    }

}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C#写的闹钟应用程序。到处都是注释,一看就懂! 核心代码和效果展示见我的博客:http://blog.csdn.net/luochao5862426/article/details/78570141 个人特色: 1、可以这么所说,别人有的,我有!别人没有的,我也有。集百家之长,选我就对了,嘿嘿。 2、代码详细,基本上每一条稍微重要点的代码都有注释这行代码是干嘛的,所以你可以看到好多好多注释,详细的不能再详细! 3、里面包含,可直接导入的项目文件、该程序的PPT展示以及录制的视频展示。 4、模块清晰,注释详细,低耦合,高内聚。 主页面介绍:分三个部分 一、动态时钟部分,像石英钟一样时、分、秒针不停转动。 二、定点闹钟部分,简单点就是可以定闹钟。 三、闹钟备忘录部分,显而易见,为了添加提示功能。 本人设计了两种可选模式: 1、懒人模式(可多次延时响铃,下面主要讲述这个模式) 2、生存模式(本次考验失败后则下次的闹钟提前几分钟响铃。由于时间有限本人没去实现这个功能) 主要功能介绍: 一、时钟石英钟) 1、使用C#的GDI+画出石英钟时、分、秒针不停转动的效果并加上了指针的尾巴。 二、闹钟 1、定闹钟时添加备注。 2、自选(默认铃声或本地铃声)试听铃声。所以机智的你可以当一个MP3用了。 3、设定多个闹钟。重点是,你可以设置不同类型(今天、每天、自定义星期、指定日期)的闹钟。 4、设定不同的响铃方式。包括:只响一次、不断响铃、静音响铃。 5、定时关机。定闹钟的时候选择了定时关机这个选项,那么,在闹钟到点后的一定时间内(我设置的3秒)会自动关机。 6、开机自启动。这个可以自己设定,很多人不需要。 7、响铃抖屏。闹钟到点后会抖动一小段时间(我设置的3秒)的屏幕,并同步跳到你打开的所有窗口的最顶层窗体。 8、系统托盘。可以隐藏到系统托盘。 三、备忘录 {备忘录组成:时段+时间+备注+尾巴(可删除,知识为了查看有哪些操作)} 1、移除所定的闹钟。 2、把闹钟备忘录保存至本地。 3、从本地导入至闹钟备忘录。所以你可以在本地修改备忘录咯,包括时间和内容。 4、修改闹钟备忘录内容。在程序界面修改备忘录。 5、查找备忘录内容。在程序界面查找备忘录内容。 6、显示倒计时。你在定闹钟的时候要是选了倒计时这个选项,则你可以在备忘录里面选中,显示倒计时。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值