c#时钟_C#模拟时钟程序

c#时钟

In this tutorial we are going to use a bit of graphics class to design a C# analog clock. We have also used a timer to continuously move our clock’s pointer.
                                     
Concept
  • We will first initialize some angle values to second, minute, hour hand.
  • Then draw the clock using the graphics class, save it as a bitmap variable and load it in the picturebox.
  • Finally Use the timer to move the second hand.

Instructions

  • Create a new windows form applicaton project in visual c#.
  • Add a picture box on the form.
  • Double click the form to switch to code view. 
  • Delete all the existing code and paste the code given below.

C# Analog Clock Program

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
 
namespace AnalogClock
{
    public partial class Form1 : Form
    {
        Timer t = new Timer();
 
        int WIDTH = 300, HEIGHT = 300, secHAND = 140, minHAND = 110, hrHAND = 80;
 
        //center
        int cx, cy;
 
        Bitmap bmp;
        Graphics g;
 
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            //create bitmap
            bmp = new Bitmap(WIDTH + 1, HEIGHT + 1);
 
            //center
            cx = WIDTH / 2;
            cy = HEIGHT / 2;
 
            //backcolor
            this.BackColor = Color.White;
 
            //timer
            t.Interval = 1000;      //in millisecond
            t.Tick += new EventHandler(this.t_Tick);
            t.Start();
        }
 
        private void t_Tick(object sender, EventArgs e)
        {
            //create graphics
            g = Graphics.FromImage(bmp);
 
            //get time
            int ss = DateTime.Now.Second;
            int mm = DateTime.Now.Minute;
            int hh = DateTime.Now.Hour;
 
            int[] handCoord = new int[2];
 
            //clear
            g.Clear(Color.White);
 
            //draw circle
            g.DrawEllipse(new Pen(Color.Black, 1f), 0, 0, WIDTH, HEIGHT);
 
            //draw figure
            g.DrawString("12", new Font("Arial", 12), Brushes.Black, new PointF(140, 2));
            g.DrawString("3", new Font("Arial", 12), Brushes.Black, new PointF(286, 140));
            g.DrawString("6", new Font("Arial", 12), Brushes.Black, new PointF(142, 282));
            g.DrawString("9", new Font("Arial", 12), Brushes.Black, new PointF(0, 140));
 
            //second hand
            handCoord = msCoord(ss, secHAND);
            g.DrawLine(new Pen(Color.Red, 1f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));
 
            //minute hand
            handCoord = msCoord(mm, minHAND);
            g.DrawLine(new Pen(Color.Black, 2f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));
 
            //hour hand
            handCoord = hrCoord(hh % 12, mm, hrHAND);
            g.DrawLine(new Pen(Color.Gray, 3f), new Point(cx, cy), new Point(handCoord[0], handCoord[1]));
 
            //load bmp in picturebox1
            pictureBox1.Image = bmp;
 
            //disp time
            this.Text = "Analog Clock -  " + hh + ":" + mm + ":" + ss;
 
            //dispose
            g.Dispose();
        }
 
        //coord for minute and second hand
        private int[] msCoord(int val, int hlen)
        {
            int[] coord = new int[2];
            val *= 6;   //each minute and second make 6 degree
 
            if (val >= 0 && val <= 180)
            {
                coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));
                coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
            }
            else
            {
                coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));
                coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
            }
            return coord;
        }
 
        //coord for hour hand
        private int[] hrCoord(int hval, int mval, int hlen)
        {
            int[] coord = new int[2];
 
            //each hour makes 30 degree
            //each min makes 0.5 degree
            int val = (int)((hval * 30) + (mval * 0.5));
 
            if (val >= 0 && val <= 180)
            {
                coord[0] = cx + (int)(hlen * Math.Sin(Math.PI * val / 180));
                coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
            }
            else
            {
                coord[0] = cx - (int)(hlen * -Math.Sin(Math.PI * val / 180));
                coord[1] = cy - (int)(hlen * Math.Cos(Math.PI * val / 180));
            }
            return coord;
        }
    }
}

翻译自: https://www.thecrazyprogrammer.com/2014/12/c-analog-clock-program.html

c#时钟

  • 1
    点赞
  • 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、显示倒计时。你在定闹钟的时候要是选了倒计时这个选项,则你可以在备忘录里面选中,显示倒计时。
比较实用的c#程序,小时钟 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; //download by http://www.codefans.net namespace MyClockApp { public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 得到当前系统时间,并将其拼接成一个字符串 /// </summary> /// <returns>数字时钟要显示的字符串</returns> public string GetTime() { String TimeInString = ""; int hour = DateTime.Now.Hour; int min = DateTime.Now.Minute; int sec = DateTime.Now.Second; //将时,分,秒连成一个字符串 TimeInString = (hour < 10) ? "0" + hour.ToString() : hour.ToString(); TimeInString += ":" + ((min < 10) ? "0" + min.ToString() : min.ToString()); TimeInString += ":" + ((sec < 10) ? "0" + sec.ToString() : sec.ToString()); return TimeInString; } /// <summary> /// 在窗体上画表盘时钟的图形 /// </summary> /// <param name="h"></param> /// <param name="m"></param> /// <param name="s"></param> private void MyDrawClock(int h, int m, int s) { Graphics g = this.CreateGraphics(); Rectangle rect = this.ClientRectangle; g.Clear(Color.White); Pen myPen = new Pen(Color.Black, 1); g.DrawEllipse(myPen, this.ClientRectangle.Width / 2 - 75, this.ClientRectangle.Height / 2-75, 150, 150);//画表盘 Point centerPoint = new Point(this.ClientRectangle.Width / 2, this.ClientRectangle.Height / 2);//表的中心点 //计算出秒针,时针,分针的另外一个商点 Point secPoint = new Point((int)(centerPoint.X + (Math.Sin(s * Math.PI / 30) * 50)), (int)(centerPoint.Y - (Math.Cos(s * Math.PI / 30) * 50))); Point minPoint = new Point((int)(centerPoint.X + (Math.Sin(m * Math.PI / 30) * 40)), (int)(centerPoint.Y - (Math.Cos(m * Math.PI / 30) * 40))); Point hourPoint = new Point((int)(centerPoint.X + (Math.Sin(h * Math.PI / 6) * 30) - m * Math.PI / 360), (int)(centerPoint.Y - (Math.Cos(h * Math.PI / 6) * 30) - m * Math.PI / 360)); //以不同颜色和宽度绘制表针 myPen = new Pen(Color.Red, 1); g.DrawLine(myPen, centerPoint, secPoint); myPen = new Pen(Color.Green, 2); g.DrawLine(myPen, centerPoint, minPoint); myPen = new Pen(Color.Orange, 4); g.DrawLine(myPen, centerPoint, hourPoint); } /// <summary> /// 定时刷新显示时间 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void timer1_Tick(object sender, EventArgs e) { int h = DateTime.Now.Hour; int s = DateTime.Now.Second; int m = DateTime.Now.Minute; MyDrawClock(h, m, s); toolStripStatusLabel1.Text = string.Format("{0}:{1}:{2}", h, m, s); lblTime.Text = GetTime(); } /// <summary> /// 若无此方法,时钟也能显示,但要等窗体显示几秒以后表盘才会显示。有了此方法窗体和表盘同时显示 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Form1_Paint(object sender, PaintEventArgs e) { int h = DateTime.Now.Hour; int s = DateTime.Now.Second; int m = DateTime.Now.Minute; MyDrawClock(h, m, s); toolStripStatusLabel1.Text = string.Format("{0}:{1}:{2}", h, m, s); lblTime.Text = GetTime(); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值