//控件名: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# 时钟控件的更多相关文章

  1. 使用Canvas绘制简单的时钟控件

    Canvas是HTML5新增的组件,它就像一块幕布,可以用JavaScript在上面绘制各种图表.动画等. 没有Canvas的年代,绘图只能借助Flash插件实现,页面不得不用JavaScript和F ...

  2. 9款精致HTML5/jQuery日历时钟控件源码下载(源码请见百度云) 链接:http://pan.baidu.com/s/1geIXe75 密码:7m4a

    现在的网页应用越来越丰富,我们在网页中填写日期和时间已经再也不用手动输入了,而是使用各种各样的日期时间选择控件,大部分样式华丽的日期选择和日历控件都是基于jQuery和HTML5的,比如今天要分享的这 ...

  3. android控件拖动,移动、解决父布局重绘时控件回到原点

    这是主要代码: 保证其params发生改变,相对于父布局的位置就能达到位置移动到原来的位置 // 每次移动都要设置其layout,不然由于父布局可能嵌套listview,当父布局发生改变冲毁(如下拉刷 ...

  4. js小时分钟控件--

    直接上代码: var str = ""; document.writeln("<div id=\"_contents\" tabindex=99 ...

  5. easyui表单插件-包括日期时控件-列表

    ← jQuery EasyUI 表单插件 – Numberspinner 数值微调器 jQuery EasyUI 表单插件 - Timespinner 时间微调器  jQuery EasyUI 插件 ...

  6. 关于VS2010 在设计窗口时控件消失问题

    我特喵的,见鬼了. 几个相同的Tabpage中添加相同toolStrip控件,每次都是第二个Tabpage中的消失,但是查看设计器下面又显示控件存在,点击也会出现,运行后就没有了,真的是奇怪. 最后经 ...

  7. VS2010保存时控件验证(用onclientclick事件) js脚本

    控件按钮代码: asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" ...

  8. 使用disqus搭建comment时一件非常二的事

    近期在github 上面搭建自己的博客,搭建comment部分的时候出现了一个问题:配置都配置好了,可是comment就是不成功.昨天为这个问题折腾了了半晚上没找出原因,今天晚上我突然发现一个地方设置 ...

  9. Android自定义控件1--自定义控件介绍

    Android控件基本介绍 Android本身提供了很多控件比如我们常用的有文本控件TextView和EditText:按钮控件Button和ImageButton状态开关按钮ToggleButton ...

随机推荐

  1. Installshield创建快捷方式不能正常运行的几种原因

    Installshield软件提供创建快捷方式的功能,但是经常有朋友发现创建了以后快捷方式不能运行,在此列一下几种常见原因供各位朋友参考: 1. 没有选择相应的working directory,这种 ...

  2. ODI Scenario 场景

    ODI中,场景的作用类似发布版本,当映射最终修改版完成时,可以生成场景.无论是映射(Mapping)还是包(Package)都可以生成场景. 包调用映射和调用场景的区别: 1,包直接调用映射,当映射修 ...

  3. jmeter使用手册

    1.在bin文件中找到jmeter.bat文件启动 2.创建测试计划-填写计划名称 3.添加线程组(右键点击) 4.设置线程-红框内均可设置,线程数-并发次数 5.在线程组下添加http请求 6.在h ...

  4. 同时使用多个JDK版本的方法

    开发环境需要使用JDK1.8,但是有些系统仅支持JDK1.6,以下方法可满足此需求: 1.使用安装包安装JDK1.6,安装之后记得设置不自动更新,控制面板-JAVA:        2.使用免安装版J ...

  5. post表单翻页保存搜索条件

    问题:搜索条件下的数据,进行翻页行为后,搜索条件丢失 1.搜索表单 2.翻页 解决:既然点击页面跳转的a标签使用的方法是GET,而点击“搜索”按钮使用的方法是POST,那么可以让点击a标签实际上就是提 ...

  6. zoj 1649 bfs

    Angel was caught by the MOLIGPY! He was put in prison by Moligpy. The prison is described as a N * M ...

  7. 解决win7无法运行bat批处理文件的方法

    在win7系统中我们可以将一些命令制作为bat批处理文件,只需双击打开即可运行命令,方便使用. 那么,要怎么运行bat批处理呢?最近有用户反馈,遇到无法运行bat批处理的现象,该怎么办呢? 修复方法一 ...

  8. Javascript 3.2

    对象的三种类型:1.用户定义对象:程序员自己创建的对象 2.内建对象:Javascript语言中的固定对象,如Array/Math/Data等 3.宿主对象:由浏览器提供的对象 BOM:浏览器对象模型 ...

  9. H3C_IRF_BFD配置

    IRF典型配置举例(BFD MAD检测方式)1. 组网需求 由于网络规模迅速扩大,当前中心交换机(Device A)转发能力已经不能满足需求,现需要在保护现有投资的基础上将网络转发能力提高一倍,并要求 ...

  10. Python实例之抓取淘宝商品数据(json型数据)并保存为TXT

    本实例实现了抓取淘宝网中以‘python’为关键字的搜索结果,经详细查看数据存储于html文档中的js脚本中,数据类型为JSON 具体实现代码如下: import requests import re ...