winform时钟c#代码

时间:2021-10-26 22:44:06

代码如下:

 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", );
Graphics g = CreateGraphics();
g.DrawString(date.ToString(), font, Brushes.Firebrick, , );
g.DrawString(date.DayOfWeek.ToString(), font, Brushes.Red, , );
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 / * s);
double y = center.Y - radius * Math.Cos(Math.PI / * s);
p.X = (int)x;
p.Y = (int)y;
return p;
} void DrawDial(Graphics g)//外圆及刻度
{
int n;
Rectangle rect = new Rectangle(, , , );
//g.FillEllipse(Brushes.White, 40, 10, 300, 300);
g.DrawEllipse(new Pen(Color.Black, ), rect);
Point p1, p2;
Point center = new Point(, );
for (n = ; n < ; n++)
{
p1 = GetPosition(n, center, );
if (n % == )
{
p2 = GetPosition(n, center, );
g.DrawLine(new Pen(Color.Black, ), p1, p2);
}
else
{
p2 = GetPosition(n, center, );
g.DrawLine(Pens.Red, p1, p2);
}
}
Font font = new Font("Times New Roman", );
n = ;
p1 = GetPosition(n, center, );
g.DrawString("XII", font, Brushes.Black, p1.X - , p1.Y);
n += ;
p1 = GetPosition(n, center, );
g.DrawString("III", font, Brushes.Black, p1.X - , p1.Y - );
n += ;
p1 = GetPosition(n, center, );
g.DrawString("VI", font, Brushes.Black, p1.X - , p1.Y - );
n += ;
p1 = GetPosition(n, center, );
g.DrawString("IX", font, Brushes.Black, p1.X, p1.Y - );
}
//秒针部分
void DrawSecondPointer(Graphics g)
{
Point center = new Point(, );
Point p;
p = GetPosition(date.Second, center, );
g.DrawLine(Pens.Red, center, p);
g.FillEllipse(Brushes.Red, new Rectangle(p.X - , p.Y - , , )); }
//分针部分
void DrawMinutePointer(Graphics g)
{
Point center = new Point(, );
Point p;
p = GetPosition(date.Minute, center, );
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 / * h + Math.PI / * 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(, );
Point p = GetHourPosition(center, );
g.DrawLine(new Pen(Brushes.Black, ), center, p);
//去指针圆尖 http://www.cnblogs.com/sosoft/
// g.FillEllipse(Brushes.Black,
// new Rectangle(p.X - 6, p.Y - 6, 12, 12));
g.FillEllipse(Brushes.YellowGreen,
new Rectangle(center.X - , center.Y - , , )); } }
}