C# 时钟控件

//控件名:myNewClock
//作者:刘典武
//时间:2011-06-10
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 OI.Screen
{
public partial class myNewClock : UserControl
{
public myNewClock()
{
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 = ;
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.White;
/// <summary>
/// 时钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("时钟颜色")]
public Color HourColor
{
get { return hourColor; }
set { hourColor = value; }
}
Color minuteColor = Color.White;
/// <summary>
/// 分钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("分钟颜色")]
public Color MinuteColor
{
get { return minuteColor; }
set { minuteColor = value; }
}
Color secondColor = Color.Red;
/// <summary>
/// 秒钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("秒钟颜色")]
public Color SecondColor
{
get { return secondColor; }
set { secondColor = value; }
}
//Color bigScaleColor = Color.DarkGreen;
Color bigScaleColor = Color.White;
/// <summary>
/// 大刻度颜色
/// </summary>
[CategoryAttribute("颜色"), Description("大刻度颜色")]
public Color BigScaleColor
{
get { return bigScaleColor; }
set { bigScaleColor = value; }
}
Color litterScaleColor = Color.White;
/// <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 = ;//开始绘制时钟起点X坐标
int y1 = ;//开始绘制时钟起点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), );
//g.DrawEllipse(pen, x1 + 7, y1 + 7, width - 13, height - 13);// 内圆
g.TranslateTransform(x1 + (width / ), y1 + (height / ));//重新设置坐标原点
g.FillEllipse(Brushes.White, -, -, , );//绘制表盘中心 g.DrawString("Time",new Font("宋体",),Brushes.White, -, (-height+)/);//绘制表盘中心 for (int x = ; x < ; x++) //小刻度
{
g.FillRectangle(new SolidBrush(litterScaleColor), new Rectangle(-, (System.Convert.ToInt16(height - ) / - ) * (-), , ));
g.RotateTransform();//偏移角度
}
for (int i = ; i > ; i--) //大刻度
{
string myString = i.ToString();
//绘制整点刻度
g.FillRectangle(new SolidBrush(bigScaleColor), new Rectangle(-, (System.Convert.ToInt16(height - ) / - ) * (-), , ));
//绘制数值
//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 + 16));
//顺时针旋转30度
g.RotateTransform(-);//偏移角度
}
//获得系统时间值
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, );
pen.EndCap = LineCap.NoAnchor;
g.RotateTransform( * second);
float y = (float)((-) * (height / 2.5));
g.DrawLine(pen, new PointF(, ), new PointF((float), y));
////绘分针
pen = new Pen(minuteColor, 2.5f);
pen.EndCap = LineCap.NoAnchor;
g.RotateTransform(- * second); //恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.1 + minute * ));
y = (float)((-) * ((height - ) / 2.1));
g.DrawLine(pen, new PointF(, ), new PointF((float), y));
////绘时针
pen = new Pen(hourColor, 2.5f);
pen.EndCap = LineCap.NoAnchor;
g.RotateTransform((float)(-second * 0.1 - minute * ));//恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.01 + minute * 0.1 + hour * ));
y = (float)((-) * ((height - ) / 2.30));
g.DrawLine(pen, new PointF(, ), new PointF((float), y));
}
}
}
原文参考--http://www.cnblogs.com/feiyangqingyun/archive/2011/07/07/2100154.html
C# 时钟控件的更多相关文章
- 使用Canvas绘制简单的时钟控件
Canvas是HTML5新增的组件,它就像一块幕布,可以用JavaScript在上面绘制各种图表.动画等. 没有Canvas的年代,绘图只能借助Flash插件实现,页面不得不用JavaScript和F ...
- 9款精致HTML5/jQuery日历时钟控件源码下载(源码请见百度云) 链接:http://pan.baidu.com/s/1geIXe75 密码:7m4a
现在的网页应用越来越丰富,我们在网页中填写日期和时间已经再也不用手动输入了,而是使用各种各样的日期时间选择控件,大部分样式华丽的日期选择和日历控件都是基于jQuery和HTML5的,比如今天要分享的这 ...
- android控件拖动,移动、解决父布局重绘时控件回到原点
这是主要代码: 保证其params发生改变,相对于父布局的位置就能达到位置移动到原来的位置 // 每次移动都要设置其layout,不然由于父布局可能嵌套listview,当父布局发生改变冲毁(如下拉刷 ...
- js小时分钟控件--
直接上代码: var str = ""; document.writeln("<div id=\"_contents\" tabindex=99 ...
- easyui表单插件-包括日期时控件-列表
← jQuery EasyUI 表单插件 – Numberspinner 数值微调器 jQuery EasyUI 表单插件 - Timespinner 时间微调器 jQuery EasyUI 插件 ...
- 关于VS2010 在设计窗口时控件消失问题
我特喵的,见鬼了. 几个相同的Tabpage中添加相同toolStrip控件,每次都是第二个Tabpage中的消失,但是查看设计器下面又显示控件存在,点击也会出现,运行后就没有了,真的是奇怪. 最后经 ...
- VS2010保存时控件验证(用onclientclick事件) js脚本
控件按钮代码: asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" ...
- 使用disqus搭建comment时一件非常二的事
近期在github 上面搭建自己的博客,搭建comment部分的时候出现了一个问题:配置都配置好了,可是comment就是不成功.昨天为这个问题折腾了了半晚上没找出原因,今天晚上我突然发现一个地方设置 ...
- Android自定义控件1--自定义控件介绍
Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...
随机推荐
- 导入PrefixHeader.pch 报错UNknow The type "NSString",等基础类
进入到项目,在Buid Settings收索Compile Source 把Compile Source As 改成Objective-C问题即可解决.
- @RequestParam 和 @ PathVariable 的区别
@RequestParam 和 @ PathVariable 的区别http://localhost:8080/Springmvc/user/page.do?pageSize=3&pageNo ...
- JS实现日期选择
简单的JS实现日期选择,对于PHP来说就像是遍历一样,不过我个人觉得JS这个很有趣,随便记录一下 开始: <select name="gh_date"><opti ...
- 基本的java加密算法MD5等等
简单的java加密算法有: BASE64 严格地说,属于编码格式,而非加密算法 MD5 (Message Digest algorithm 5,信息摘要算法) SH ...
- [LeetCode&Python] Problem 704. Binary Search
Given a sorted (in ascending order) integer array nums of n elements and a target value, write a fun ...
- redux源码图解:createStore 和 applyMiddleware
在研究 redux-saga时,发现自己对 redux middleware 不是太了解,因此,便决定先深入解读一下 redux 源码.跟大多数人一样,发现 redux源码 真的很精简,目录结构如下: ...
- redis数据库可视化工具(RedisDesktopManager)
RedisDesktopManager下载地址:https://redisdesktop.com/download 使用过程中可能会遇到的问题我在文后有所总结 我下载Windows版的: 我的redi ...
- vlookup使用
数据处理过程中,需要excel进行简单的操作,比如vlookup,摸索之后,总结如下:
- [Python]_ELVE_centos7安装Python3.7.1与Python2共存
注:该博客转载至PengYunjing博客,加以改动. #0x01 安装依赖环境 yum -y install zlib-devel bzip2-devel openssl-devel ncurses ...
- 谷歌chrome浏览器vue调试工具vue-devtools的安装
先导 vue-devtools是一款基于chrome浏览器的插件,用于vue应用的调试,这款vue调试神器可以极大地提高我们的调试效率.帮助我们快速的调试开发vue应用. 第一步: 我们可以先从git ...