//控件名: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. python 面试题1

    1.大数据的文件读取 ① 利用生成器generator ②迭代器进行迭代遍历:for line in file 2.迭代器和生成器的区别 1)迭代器是一个更抽象的概念,任何对象,如果它的类有next方 ...

  2. H5视频直播扫盲

    H5视频直播扫盲 2016-05-25 • 前端杂项 • 14 条评论 • lvming19901227 视频直播这么火,再不学就out了. 为了紧跟潮流,本文将向大家介绍一下视频直播中的基本流程和主 ...

  3. 点击按钮下载图片(ie,FF,chrome)

    参考网上的一些资料后,总结出来 <!DOCTYPE html><html> <head> <meta content="text/html; cha ...

  4. Redis事务和实现秒杀功能的实现

    今天带着学生学习了Redis的事务功能,Redis的事务与传统的关系型数据库(如MySQL)有所不同,Redis的事务不能回滚. Redis中使用multi.exec.discard.watch.un ...

  5. github/gitee使用办法2

    打开自己的仓库 git pull 把内容复制过去 git add . 添加所有 查看状态 git status 提交 git commit -m 'XXX' 最后push git push 如果内容本 ...

  6. 图片懒加载 echo.js

    (function (root, factory) { if (typeof define === 'function' && define.amd) { define(functio ...

  7. creator NDK_PROJECT_PATH=null

    NDK_PROJECT_PATH=null 其实不是一个错误= =,少年,不要纠结于此了. 主要问题在于这一句: process_begin: CreateProcess(NULL, E:/Andro ...

  8. ARM Cortex M0 程序映像和启动流程

  9. 树的子结构(JAVA)

    树的子结构 题目描述 输入两棵二叉树A,B,判断B是不是A的子结构.(ps:我们约定空树不是任意一个树的子结构) public boolean HasSubtree(TreeNode root1, T ...

  10. c#函数地址传入c++

    c# private delegate void ValidateEvent(int eventCode); private static void ValidateEventCallback(int ...