- 浏览: 38724 次
文章分类
最新评论
效果图:
接着上次的简单时钟,这次要高级多了,算法更正,可以自定义时钟分钟秒钟刻度等颜色,与系统时钟对应,随你怎么改,源代码具有很高的参考价值。
第一步:添加用户控件,命名为myNewClock
第二步:定义变量,要用到时钟,画布等
private Timer myTimer;//定义时钟,定时重新绘制
private Graphics g;//创建画布
private Pen pen;//创建画笔
private int width;//画布高度
private int height;//画布宽度
第三步:定义属性,可以自由增加
Color hourColor = Color.Red;
/// <summary>
/// 时钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("时钟颜色")]
public Color HourColor
{
get { return hourColor; }
set { hourColor=value; }
}
Color minuteColor = Color.Green;
/// <summary>
/// 分钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("分钟颜色")]
public Color MinuteColor
{
get { return minuteColor; }
set { minuteColor = value; }
}
Color secondColor = Color.Blue;
/// <summary>
/// 秒钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("秒钟颜色")]
public Color SecondColor
{
get { return secondColor; }
set { secondColor = value; }
}
Color bigScaleColor = Color.DarkGreen;
/// <summary>
/// 大刻度颜色
/// </summary>
[CategoryAttribute("颜色"), Description("大刻度颜色")]
public Color BigScaleColor
{
get { return bigScaleColor; }
set { bigScaleColor = value; }
}
Color litterScaleColor = Color.Olive;
/// <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; }
}
第四步:重写OnPaint方法
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 = 0;//开始绘制时钟起点X坐标
int y1 = 0;//开始绘制时钟起点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), 2);
g.DrawEllipse(pen, x1 + 7, y1 + 7, width - 13, height - 13);// 内圆
g.TranslateTransform(x1 + (width / 2), y1 + (height / 2));//重新设置坐标原点
g.FillEllipse(Brushes.White, -5, -5, 10, 10);//绘制表盘中心
for (int x = 0; x < 60; x++) //小刻度
{
g.FillRectangle(new SolidBrush(litterScaleColor), new Rectangle(-2, (System.Convert.ToInt16(height - / 2 - 2) * (-1), 3, 10));
g.RotateTransform(6);//偏移角度
}
for (int i = 12; i > 0; i--) //大刻度
{
string myString = i.ToString();
//绘制整点刻度
g.FillRectangle(new SolidBrush(bigScaleColor), new Rectangle(-3, (System.Convert.ToInt16(height - / 2 - 2) * (-1), 6, 20));
//绘制数值
g.DrawString(myString, new Font(new FontFamily("Times New Roman"),14, FontStyle.Bold, GraphicsUnit.Pixel),new SolidBrush( textColor), new PointF(myString.Length * (-6), (height - / -2 + 26));
//顺时针旋转30度
g.RotateTransform(-30);//偏移角度
}
//获得系统时间值
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, 1);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform(6 * second);
float y = (float)((-1) * (height / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
////绘分针
pen = new Pen(minuteColor, 4);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform(-6 * second); //恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.1 + minute * 6));
y = (float)((-1) * ((height - 30) / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
////绘时针
pen = new Pen(hourColor, 6);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform((float)(-second * 0.1 - minute * 6));//恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.01 + minute * 0.1 + hour * 30));
y = (float)((-1) * ((height - 45) / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
}
贴出全部代码:
//控件名: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 myControl
{
public partial class myNewClock : UserControl
{
public myNewClock()
{
InitializeComponent();
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 =1000;
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.Red;
/// <summary>
/// 时钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("时钟颜色")]
public Color HourColor
{
get { return hourColor; }
set { hourColor=value; }
}
Color minuteColor = Color.Green;
/// <summary>
/// 分钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("分钟颜色")]
public Color MinuteColor
{
get { return minuteColor; }
set { minuteColor = value; }
}
Color secondColor = Color.Blue;
/// <summary>
/// 秒钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("秒钟颜色")]
public Color SecondColor
{
get { return secondColor; }
set { secondColor = value; }
}
Color bigScaleColor = Color.DarkGreen;
/// <summary>
/// 大刻度颜色
/// </summary>
[CategoryAttribute("颜色"), Description("大刻度颜色")]
public Color BigScaleColor
{
get { return bigScaleColor; }
set { bigScaleColor = value; }
}
Color litterScaleColor = Color.Olive;
/// <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 = 0;//开始绘制时钟起点X坐标
int y1 = 0;//开始绘制时钟起点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), 2);
g.DrawEllipse(pen, x1 + 7, y1 + 7, width - 13, height - 13);// 内圆
g.TranslateTransform(x1 + (width / 2), y1 + (height / 2));//重新设置坐标原点
g.FillEllipse(Brushes.White, -5, -5, 10, 10);//绘制表盘中心
for (int x = 0; x < 60; x++) //小刻度
{
g.FillRectangle(new SolidBrush(litterScaleColor), new Rectangle(-2, (System.Convert.ToInt16(height - / 2 - 2) * (-1), 3, 10));
g.RotateTransform(6);//偏移角度
}
for (int i = 12; i > 0; i--) //大刻度
{
string myString = i.ToString();
//绘制整点刻度
g.FillRectangle(new SolidBrush(bigScaleColor), new Rectangle(-3, (System.Convert.ToInt16(height - / 2 - 2) * (-1), 6, 20));
//绘制数值
g.DrawString(myString, new Font(new FontFamily("Times New Roman"),14, FontStyle.Bold, GraphicsUnit.Pixel),new SolidBrush( textColor), new PointF(myString.Length * (-6), (height - / -2 + 26));
//顺时针旋转30度
g.RotateTransform(-30);//偏移角度
}
//获得系统时间值
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, 1);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform(6 * second);
float y = (float)((-1) * (height / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
////绘分针
pen = new Pen(minuteColor, 4);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform(-6 * second); //恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.1 + minute * 6));
y = (float)((-1) * ((height - 30) / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
////绘时针
pen = new Pen(hourColor, 6);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform((float)(-second * 0.1 - minute * 6));//恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.01 + minute * 0.1 + hour * 30));
y = (float)((-1) * ((height - 45) / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
}
}
}
接着上次的简单时钟,这次要高级多了,算法更正,可以自定义时钟分钟秒钟刻度等颜色,与系统时钟对应,随你怎么改,源代码具有很高的参考价值。
第一步:添加用户控件,命名为myNewClock
第二步:定义变量,要用到时钟,画布等
private Timer myTimer;//定义时钟,定时重新绘制
private Graphics g;//创建画布
private Pen pen;//创建画笔
private int width;//画布高度
private int height;//画布宽度
第三步:定义属性,可以自由增加
Color hourColor = Color.Red;
/// <summary>
/// 时钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("时钟颜色")]
public Color HourColor
{
get { return hourColor; }
set { hourColor=value; }
}
Color minuteColor = Color.Green;
/// <summary>
/// 分钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("分钟颜色")]
public Color MinuteColor
{
get { return minuteColor; }
set { minuteColor = value; }
}
Color secondColor = Color.Blue;
/// <summary>
/// 秒钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("秒钟颜色")]
public Color SecondColor
{
get { return secondColor; }
set { secondColor = value; }
}
Color bigScaleColor = Color.DarkGreen;
/// <summary>
/// 大刻度颜色
/// </summary>
[CategoryAttribute("颜色"), Description("大刻度颜色")]
public Color BigScaleColor
{
get { return bigScaleColor; }
set { bigScaleColor = value; }
}
Color litterScaleColor = Color.Olive;
/// <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; }
}
第四步:重写OnPaint方法
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 = 0;//开始绘制时钟起点X坐标
int y1 = 0;//开始绘制时钟起点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), 2);
g.DrawEllipse(pen, x1 + 7, y1 + 7, width - 13, height - 13);// 内圆
g.TranslateTransform(x1 + (width / 2), y1 + (height / 2));//重新设置坐标原点
g.FillEllipse(Brushes.White, -5, -5, 10, 10);//绘制表盘中心
for (int x = 0; x < 60; x++) //小刻度
{
g.FillRectangle(new SolidBrush(litterScaleColor), new Rectangle(-2, (System.Convert.ToInt16(height - / 2 - 2) * (-1), 3, 10));
g.RotateTransform(6);//偏移角度
}
for (int i = 12; i > 0; i--) //大刻度
{
string myString = i.ToString();
//绘制整点刻度
g.FillRectangle(new SolidBrush(bigScaleColor), new Rectangle(-3, (System.Convert.ToInt16(height - / 2 - 2) * (-1), 6, 20));
//绘制数值
g.DrawString(myString, new Font(new FontFamily("Times New Roman"),14, FontStyle.Bold, GraphicsUnit.Pixel),new SolidBrush( textColor), new PointF(myString.Length * (-6), (height - / -2 + 26));
//顺时针旋转30度
g.RotateTransform(-30);//偏移角度
}
//获得系统时间值
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, 1);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform(6 * second);
float y = (float)((-1) * (height / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
////绘分针
pen = new Pen(minuteColor, 4);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform(-6 * second); //恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.1 + minute * 6));
y = (float)((-1) * ((height - 30) / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
////绘时针
pen = new Pen(hourColor, 6);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform((float)(-second * 0.1 - minute * 6));//恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.01 + minute * 0.1 + hour * 30));
y = (float)((-1) * ((height - 45) / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
}
贴出全部代码:
//控件名: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 myControl
{
public partial class myNewClock : UserControl
{
public myNewClock()
{
InitializeComponent();
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 =1000;
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.Red;
/// <summary>
/// 时钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("时钟颜色")]
public Color HourColor
{
get { return hourColor; }
set { hourColor=value; }
}
Color minuteColor = Color.Green;
/// <summary>
/// 分钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("分钟颜色")]
public Color MinuteColor
{
get { return minuteColor; }
set { minuteColor = value; }
}
Color secondColor = Color.Blue;
/// <summary>
/// 秒钟颜色
/// </summary>
[CategoryAttribute("颜色"), Description("秒钟颜色")]
public Color SecondColor
{
get { return secondColor; }
set { secondColor = value; }
}
Color bigScaleColor = Color.DarkGreen;
/// <summary>
/// 大刻度颜色
/// </summary>
[CategoryAttribute("颜色"), Description("大刻度颜色")]
public Color BigScaleColor
{
get { return bigScaleColor; }
set { bigScaleColor = value; }
}
Color litterScaleColor = Color.Olive;
/// <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 = 0;//开始绘制时钟起点X坐标
int y1 = 0;//开始绘制时钟起点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), 2);
g.DrawEllipse(pen, x1 + 7, y1 + 7, width - 13, height - 13);// 内圆
g.TranslateTransform(x1 + (width / 2), y1 + (height / 2));//重新设置坐标原点
g.FillEllipse(Brushes.White, -5, -5, 10, 10);//绘制表盘中心
for (int x = 0; x < 60; x++) //小刻度
{
g.FillRectangle(new SolidBrush(litterScaleColor), new Rectangle(-2, (System.Convert.ToInt16(height - / 2 - 2) * (-1), 3, 10));
g.RotateTransform(6);//偏移角度
}
for (int i = 12; i > 0; i--) //大刻度
{
string myString = i.ToString();
//绘制整点刻度
g.FillRectangle(new SolidBrush(bigScaleColor), new Rectangle(-3, (System.Convert.ToInt16(height - / 2 - 2) * (-1), 6, 20));
//绘制数值
g.DrawString(myString, new Font(new FontFamily("Times New Roman"),14, FontStyle.Bold, GraphicsUnit.Pixel),new SolidBrush( textColor), new PointF(myString.Length * (-6), (height - / -2 + 26));
//顺时针旋转30度
g.RotateTransform(-30);//偏移角度
}
//获得系统时间值
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, 1);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform(6 * second);
float y = (float)((-1) * (height / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
////绘分针
pen = new Pen(minuteColor, 4);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform(-6 * second); //恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.1 + minute * 6));
y = (float)((-1) * ((height - 30) / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
////绘时针
pen = new Pen(hourColor, 6);
pen.EndCap = LineCap.ArrowAnchor;
g.RotateTransform((float)(-second * 0.1 - minute * 6));//恢复系统偏移量,再计算下次偏移
g.RotateTransform((float)(second * 0.01 + minute * 0.1 + hour * 30));
y = (float)((-1) * ((height - 45) / 2.75));
g.DrawLine(pen, new PointF(0, 0), new PointF((float)0, y));
}
}
}
发表评论
-
怎样提高WebService的性能
2015-02-03 13:51 1754服务器端WebService程序: using Syste ... -
ASP.net网站性能提高办法 无意中从网上浏览到了一篇提高网站性能的帖子转来学习下。
2015-01-16 13:42 474ASP.net网站性能提高办法 无意中从网上浏览到了一篇提高网 ... -
asp.net创建缩略图
2015-01-16 12:01 337记录一个保存缩略图通用的函数。 + View C ... -
让ADO.NET Entity Framework支持Oracle数据库 好东西收藏了
2015-01-16 11:35 477项目需要基于.NET平台,个人的习惯是能用微软自带的就不用第三 ... -
使用SqlBulkCopy导入大数据要注意
2015-01-13 14:17 519第一次用挺好,第二次就出问题了:我的表里边已经有数据,而且加了 ... -
不要让你的错误处理把错误给吃了,千万注意不然费时费力
2015-01-13 10:48 548开发中老鸟总是说你的程序怎么不够友好,都不加错误处理,给用户报 ... -
用微软提供的批量把数据导入到数据库方法SqlBulkCopy真是好用好快
2015-01-13 09:59 2532平时老是碰到把一批数据插入到数据库。通常的办法是循环一条条插进 ... -
SQL Server类型与C#类型对应关系 好东西收藏了
2015-01-13 09:14 1512SQL Server类型 C#类型 bit bool tiny ... -
asp.net 中URL 编码窗体数据无效提示错误处理记录
2015-01-12 15:50 694一个系统,其他用户都没有问题,就是一个用户说报错 后来查 ... -
CSV文件转换类
2015-01-12 13:01 779/// <summary> /// ... -
最全的日期年农历假日节气星座等计算类封装
2015-01-12 09:27 477/// <summary> /// 农历属 ... -
用调用浏览器打印页面1
2015-01-12 08:08 675<!--media=print 这个属性可以在打印时有效 ... -
C#检验数据有效性验证类
2015-01-09 12:46 735using System; using System.Text ... -
Asp.net事假的执行顺序,非常重要这个算是高级编程部分
2015-01-09 09:47 706ASP.NET 母版页和内容页中的事件 母版页和内容页都可以 ... -
用NPOI把数据库内容导入到Excel中实例(NPOI版本2.0)
2015-01-08 14:30 574注意 [url=http://dl.iteye.com/top ... -
开发中货币样式看似简单,没弄过的往往花费比较长时间,本人处理了,贴出来大家分享,也方便自己以后查用
2015-01-08 11:00 452开发中货币样式看似简单,没弄过的往往花费比较长时间,本人处理了 ... -
写流文件的方式把页面中的数据导到Excel的终极代码
2015-01-06 11:12 1029在开发一个小项目的时候,当时说功能简单,但是要实现数据导出功 ...
相关推荐
c# GDI+画时钟 c# GDI+画时钟 c# GDI+画时钟 c# GDI+画时钟 c# GDI+画时钟 c# GDI+画时钟
使用c# GDI+编写的时钟。采用双缓冲无闪屏!
一个利用坐标旋转原理计算坐标,绘制时钟的时针,分针,秒针,动态实现钟表显示的GDI绘图程序。
C#使用GDI+制作时钟.docx
精美的时钟,看效果请看上一条,有编译好的文件,此为源码
c# 使用GDI+,和Timer控件完成简单的时钟设计。
现在使用的还是XP的系统,每次看到数字显示的时钟总是不直观,于是做了一个仿Win7效果的桌面时钟,表盘使用的是图片,美工好的朋友可以自己换一个表盘背景。 代码只有一百多行(主要是画表针),适合新接触GDI+的朋友...
基于C# GDI+ 制作的时钟小工具,适合初学者,欢迎下载
C# GDI+实现钟表(包含时、分、秒指针)源码
GDI+绘制运行的时钟 GDI+绘制时钟 GDI+时钟 C#绘制时钟 C#绘制的时钟
C#编程基础-GDI图像编程、窗体调用-时钟-秒表-定时器
自己写的一个C#小时钟程序,用表盘动态显示时间
C# .NET 中实现钟表秒针,分针,时针同时转动。
用GDI+做的一个模拟时钟,语言为C# 窗口形状为提供的钟面图片非透明区域形状,还使用了接口继承,提供了较多了可拓展点
用GDI画的一个电子时钟,代码比较简单,界面看起来也很简易
C#代码,运用GDI绘图,功能齐全,欢迎下载。
1、使用C#的GDI+画出石英钟时、分、秒针不停转动的效果并加上了指针的尾巴。 二、闹钟 1、定闹钟时添加备注。 2、自选(默认铃声或本地铃声)试听铃声。所以机智的你可以当一个MP3用了。 3、设定多个闹钟。重点是,...
指针式时钟,模拟机械表的运行,涉及GDI画图,
请确保您的电脑安装.netframework2.0或以上版本
用C#做的一个简单的时钟。其中还有一个用GDI+去实现的动画效果