C#自定义控件六高级时钟.docx
- 文档编号:5626473
- 上传时间:2022-12-29
- 格式:DOCX
- 页数:13
- 大小:53.04KB
C#自定义控件六高级时钟.docx
《C#自定义控件六高级时钟.docx》由会员分享,可在线阅读,更多相关《C#自定义控件六高级时钟.docx(13页珍藏版)》请在冰豆网上搜索。
C#自定义控件六高级时钟
C#自定义控件六高级时钟
效果图:
接着上次的简单时钟,这次要高级多了,算法更正,可以自定义时钟分钟秒钟刻度等颜色,与系统时钟对应,随你怎么改,源代码具有很高的参考价值。
第一步:
添加用户控件,命名为myNewClock
第二步:
定义变量,要用到时钟,画布等
privateTimermyTimer;//定义时钟,定时重新绘制
privateGraphicsg;//创建画布
privatePenpen;//创建画笔
privateintwidth;//画布高度
privateintheight;//画布宽度
第三步:
定义属性,可以自由增加
ColorhourColor=Color.Red;
///
///时钟颜色
///
[CategoryAttribute("颜色"),Description("时钟颜色")]
publicColorHourColor
{
get{returnhourColor;}
set{hourColor=value;}
}
ColorminuteColor=Color.Green;
///
///分钟颜色
///
[CategoryAttribute("颜色"),Description("分钟颜色")]
publicColorMinuteColor
{
get{returnminuteColor;}
set{minuteColor=value;}
}
ColorsecondColor=Color.Blue;
///
///秒钟颜色
///
[CategoryAttribute("颜色"),Description("秒钟颜色")]
publicColorSecondColor
{
get{returnsecondColor;}
set{secondColor=value;}
}
ColorbigScaleColor=Color.DarkGreen;
///
///大刻度颜色
///
[CategoryAttribute("颜色"),Description("大刻度颜色")]
publicColorBigScaleColor
{
get{returnbigScaleColor;}
set{bigScaleColor=value;}
}
ColorlitterScaleColor=Color.Olive;
///
///小刻度颜色
///
[CategoryAttribute("颜色"),Description("小刻度颜色")]
publicColorLitterScaleColor
{
get{returnlitterScaleColor;}
set{litterScaleColor=value;}
}
ColortextColor=Color.White;
///
///刻度值颜色
///
[CategoryAttribute("颜色"),Description("刻度值颜色")]
publicColorTextColor
{
get{returntextColor;}
set{textColor=value;}
}
ColorbigBackColor=Color.Black;
///
///外圆背景色
///
[CategoryAttribute("颜色"),Description("外圆背景颜色")]
publicColorBigBackColor
{
get{returnbigBackColor;}
set{bigBackColor=value;}
}
ColorlitterBackColor=Color.White;
///
///内圆颜色
///
[CategoryAttribute("颜色"),Description("内圆颜色")]
publicColorLitterBackColor
{
get{returnlitterBackColor;}
set{litterBackColor=value;}
}
第四步:
重写OnPaint方法
protectedoverridevoidOnPaint(PaintEventArgse)
{
base.OnPaint(e);
g=e.Graphics;
g.SmoothingMode=SmoothingMode.AntiAlias;//
g.SmoothingMode=SmoothingMode.HighQuality;//绘图模式默认为粗糙模式,将会出现锯齿!
width=this.Width;//时钟宽度
height=this.Height;//时钟高度
intx1=0;//开始绘制时钟起点X坐标
inty1=0;//开始绘制时钟起点Y坐标
/*------------------------------------------------------------------------------
计算:
整点刻度12个,每个刻度偏移角度为360/12=30度及为小时偏移角度
分秒刻度为60个,每个刻度偏移角度为360/60=6度及为分、秒偏移角度
--------------------------------------------------------------------------------*/
g.FillEllipse(newSolidBrush(bigBackColor),x1+2,y1+2,width-4,height-4);//外圆
pen=newPen(newSolidBrush(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(intx=0;x<60;x++)//小刻度
{
g.FillRectangle(newSolidBrush(litterScaleColor),newRectangle(-2,(System.Convert.ToInt16(height-8)/2-2)*(-1),3,10));
g.RotateTransform(6);//偏移角度
}
for(inti=12;i>0;i--)//大刻度
{
stringmyString=i.ToString();
//绘制整点刻度
g.FillRectangle(newSolidBrush(bigScaleColor),newRectangle(-3,(System.Convert.ToInt16(height-8)/2-2)*(-1),6,20));
//绘制数值
g.DrawString(myString,newFont(newFontFamily("TimesNewRoman"),14,FontStyle.Bold,GraphicsUnit.Pixel),newSolidBrush(textColor),newPointF(myString.Length*(-6),(height-8)/-2+26));
//顺时针旋转30度
g.RotateTransform(-30);//偏移角度
}
//获得系统时间值
intsecond=DateTime.Now.Second;
intminute=DateTime.Now.Minute;
inthour=DateTime.Now.Hour;
/*------------------------------------------------------------------------------------
每秒偏移6度,秒针偏移=当前秒*6
每分偏移6读,分针偏移=当前分*6+当前秒*(6/60)
每小时偏移60读,时针偏移=当前时*30+当前分*(6/60)+当前秒*(6/60/60)
--------------------------------------------------------------------------------------*/
//绘秒针
pen=newPen(secondColor,1);
pen.EndCap=LineCap.ArrowAnchor;
g.RotateTransform(6*second);
floaty=(float)((-1)*(height/2.75));
g.DrawLine(pen,newPointF(0,0),newPointF((float)0,y));
////绘分针
pen=newPen(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,newPointF(0,0),newPointF((float)0,y));
////绘时针
pen=newPen(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,newPointF(0,0),newPointF((float)0,y));
}
贴出全部代码:
//控件名:
myNewClock
//作者:
刘典武
//时间:
2011-06-10
usingSystem;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Drawing;
usingSystem.Data;
usingSystem.Text;
usingSystem.Windows.Forms;
usingSystem.Drawing.Drawing2D;
namespacemyControl
{
publicpartialclassmyNewClock:
UserControl
{
publicmyNewClock()
{
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=newTimer();
myTimer.Interval=1000;
myTimer.Enabled=true;
myTimer.Tick+=newEventHandler(myTimer_Tick);
}
privatevoidmyTimer_Tick(objectsender,EventArgse)
{
this.Invalidate();
}
privateTimermyTimer;//定义时钟,定时重新绘制
privateGraphicsg;//创建画布
privatePenpen;//创建画笔
privateintwidth;//画布高度
privateintheight;//画布宽度
ColorhourColor=Color.Red;
///
///时钟颜色
///
[CategoryAttribute("颜色"),Description("时钟颜色")]
publicColorHourColor
{
get{returnhourColor;}
set{hourColor=value;}
}
ColorminuteColor=Color.Green;
///
///分钟颜色
///
[CategoryAttribute("颜色"),Description("分钟颜色")]
publicColorMinuteColor
{
get{returnminuteColor;}
set{minuteColor=value;}
}
ColorsecondColor=Color.Blue;
///
///秒钟颜色
///
[CategoryAttribute("颜色"),Description("秒钟颜色")]
publicColorSecondColor
{
get{returnsecondColor;}
set{secondColor=value;}
}
ColorbigScaleColor=Color.DarkGreen;
///
///大刻度颜色
///
[CategoryAttribute("颜色"),Description("大刻度颜色")]
publicColorBigScaleColor
{
get{returnbigScaleColor;}
set{bigScaleColor=value;}
}
ColorlitterScaleColor=Color.Olive;
///
///小刻度颜色
///
[CategoryAttribute("颜色"),Description("小刻度颜色")]
publicColorLitterScaleColor
{
get{returnlitterScaleColor;}
set{litterScaleColor=value;}
}
ColortextColor=Color.White;
///
///刻度值颜色
///
[CategoryAttribute("颜色"),Description("刻度值颜色")]
publicColorTextColor
{
get{returntextColor;}
set{textColor=value;}
}
ColorbigBackColor=Color.Black;
///
///外圆背景色
///
[CategoryAttribute("颜色"),Description("外圆背景颜色")]
publicColorBigBackColor
{
get{returnbigBackColor;}
set{bigBackColor=value;}
}
ColorlitterBackColor=Color.White;
///
///内圆颜色
///
[CategoryAttribute("颜色"),Description("内圆颜色")]
publicColorLitterBackColor
{
get{returnlitterBackColor;}
set{litterBackColor=value;}
}
protectedoverridevoidOnPaint(PaintEventArgse)
{
base.OnPaint(e);
g=e.Graphics;
g.SmoothingMode=SmoothingMode.AntiAlias;//
g.SmoothingMode=SmoothingMode.HighQuality;//绘图模式默认为粗糙模式,将会出现锯齿!
width=this.Width;//时钟宽度
height=this.Height;//时钟高度
intx1=0;//开始绘制时钟起点X坐标
inty1=0;//开始绘制时钟起点Y坐标
/*------------------------------------------------------------------------------
计算:
整点刻度12个,每个刻度偏移角度为360/12=30度及为小时偏移角度
分秒刻度为60个,每个刻度偏移角度为360/60=6度及为分、秒偏移角度
--------------------------------------------------------------------------------*/
g.FillEllipse(newSolidBrush(bigBackColor),x1+2,y1+2,width-4,height-4);//外圆
pen=newPen(newSolidBrush(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(intx=0;x<60;x++)//小刻度
{
g.FillRectangle(newSolidBrush(litterScaleColor),newRectangle(-2,(System.Convert.ToInt16(height-8)/2-2)*(-1),3,10));
g.RotateTransform(6);//偏移角度
}
for(inti=12;i>0;i--)//大刻度
{
stringmyString=i.ToString();
//绘制整点刻度
g.FillRectangle(newSolidBrush(bigScaleColor),newRectangle(-3,(System.Convert.ToInt16(height-8)/2-2)*(-1),6,20));
//绘制数值
g.DrawString(myString,newFont(newFontFamily("TimesNewRoman"),14,FontStyle.Bold,GraphicsUnit.Pixel),newSolidBrush(textColor),newPointF(myString.Length*(-6),(height-8)/-2+26));
//顺时针旋转30度
g.RotateTransform(-30);//偏移角度
}
//获得系统时间值
intsecond=DateTime.Now.Second;
intminute=DateTime.Now.Minute;
inthour=DateTime.Now.Hour;
/*------------------------------------------------------------------------------------
每秒偏移6度,秒针偏移=当前秒*6
每分偏移6读,分针偏移=当前分*6+当前秒*(6/60)
每小时偏移60读,时针偏移=当前时*30+当前分*(6/60)+当前秒*(6/60/60)
--------------------------------------------------------------------------------------*/
//绘秒针
pen=newPen(secondColor,1);
pen.EndCap=LineCap.ArrowAnchor;
g.R
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- C# 自定义 控件 高级 时钟