-
C#教程之c# winform时钟的实现代码
代码如下:
复制代码 代码如下:
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));
}
}
}
栏目列表
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
SQL SERVER中递归
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
这是目前我见过最好的跨域解决方案!
减少回流与重绘
减少回流与重绘
如何使用KrpanoToolJS在浏览器切图
performance.now() 与 Date.now() 对比
一款纯 JS 实现的轻量化图片编辑器
关于开发 VS Code 插件遇到的 workbench.scm.
前端设计模式——观察者模式
前端设计模式——中介者模式
创建型-原型模式