欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

c# winform时钟的实现代码

时间:2020-10-13 13:21:51|栏目:.NET代码|点击:

代码如下:

复制代码 代码如下:

using System;
 using System.Collections.Generic;
 using System.ComponentModel;
 using System.Data;
 using System.Drawing;

 using System.Text;
 using System.Windows.Forms;

 namespace Simpclock
 {
     public partial class Form1 : Form
     {
         DateTime date = DateTime.Now;

         public Form1()
         {
             InitializeComponent();

         }

         private void Form1_Load(object sender, EventArgs e)
         {
             date = DateTime.Now;

         }

         private void OnPaint(object sender, PaintEventArgs e)
         {
             Font font = new Font("Times New Roman", 20);
             Graphics g = CreateGraphics();
             g.DrawString(date.ToString(), font, Brushes.Firebrick, 10,330 );
             g.DrawString(date.DayOfWeek.ToString(), font, Brushes.Red, 250, 330);
             DrawDial(g);
             DrawSecondPointer(g);
             DrawMinutePointer(g);
             DrawHourPointer(g);

         }
         //刷新时间
         private void OnTime(object sender, EventArgs e)
         {
             date = DateTime.Now;
             Invalidate();
         }
         //画钟表
         //表盘部分
         Point GetPosition(int s, Point center, double radius)//定位
         {
             Point p = new Point();
             double x = center.X + radius * Math.Sin(Math.PI / 30 * s);
             double y = center.Y - radius * Math.Cos(Math.PI / 30 * s);
             p.X = (int)x;
             p.Y = (int)y;
             return p;
         }

         void DrawDial(Graphics g)//外圆及刻度
         {
             int n;
             Rectangle rect = new Rectangle(40, 10, 300, 300);
             //g.FillEllipse(Brushes.White, 40, 10, 300, 300);
             g.DrawEllipse(new Pen(Color.Black, 3), rect);
             Point p1, p2;
             Point center = new Point(190, 160);
             for (n = 0; n < 60; n++)
             {
                 p1 = GetPosition(n, center, 150);
                 if (n % 5 == 0)
                 {
                     p2 = GetPosition(n, center, 130);
                     g.DrawLine(new Pen(Color.Black, 2), p1, p2);
                 }
                 else
                 {
                     p2 = GetPosition(n, center, 140);
                     g.DrawLine(Pens.Red, p1, p2);
                 }
             }
             Font font = new Font("Times New Roman", 20);
             n = 0;
             p1 = GetPosition(n, center, 130);
             g.DrawString("XII", font, Brushes.Black, p1.X - 25, p1.Y);
             n += 15;
             p1 = GetPosition(n, center, 130);
             g.DrawString("III", font, Brushes.Black, p1.X - 35, p1.Y - 15);
             n += 15;
             p1 = GetPosition(n, center, 130);
             g.DrawString("VI", font, Brushes.Black, p1.X - 20, p1.Y - 30);
             n += 15;
             p1 = GetPosition(n, center, 130);
             g.DrawString("IX", font, Brushes.Black, p1.X, p1.Y - 15);
         }
         //秒针部分
         void DrawSecondPointer(Graphics g)
         {
             Point center = new Point(190, 160);
             Point p;
             p = GetPosition(date.Second, center, 130);
             g.DrawLine(Pens.Red, center, p);
             g.FillEllipse(Brushes.Red, new Rectangle(p.X - 2, p.Y - 2, 4, 4));

         }
         //分针部分
         void DrawMinutePointer(Graphics g)
         {
             Point center = new Point(190, 160);
             Point p;
             p = GetPosition(date.Minute, center, 120);
             g.DrawLine(Pens.Blue, center, p);
             //g.FillEllipse(Brushes.Blue, new Rectangle(p.X - 4, p.Y - 4, 8, 8));
         }
         //时针部分
         Point GetHourPosition(Point center, double radius)
         {
             Point p = new Point();
             int h = date.Hour;
             int m = date.Minute;
             double t = Math.PI / 6 * h + Math.PI / 360 * m;
             double x = center.X + radius * Math.Sin(t);
             double y = center.Y - radius * Math.Cos(t);
             p.X = (int)x;
             p.Y = (int)y;
             return p;
         }
         void DrawHourPointer(Graphics g)
         {
             Point center = new Point(190, 160);
             Point p = GetHourPosition(center, 100);
             g.DrawLine(new Pen(Brushes.Black, 2), center, p);
             //去指针圆尖
             // g.FillEllipse(Brushes.Black,
             //              new Rectangle(p.X - 6, p.Y - 6, 12, 12));
             g.FillEllipse(Brushes.YellowGreen,
             new Rectangle(center.X - 6, center.Y - 6, 12, 12));

 
         }

     }
}

上一篇:asp.net利用ashx文件实现文件的上传功能

栏    目:.NET代码

下一篇:带着问题读CLR via C#(笔记二)类型基础

本文标题:c# winform时钟的实现代码

本文地址:http://www.codeinn.net/misctech/10824.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有