【asp精品源码栏目提醒】:文章导读:在新的一年中,各位网友都进入紧张的学习或是工作阶段。
网学会员整理了asp精品源码-C#自定义控件六高级时钟 - 其它资料的相关内容供大家参考,祝大家在新的一年里工作和学习顺利!
C自定义控件六高级时钟效果图:接着上次的简单时钟,这次要高级多了,算法更正,可以自定义时钟分钟秒钟刻度等颜色,与系统时钟对应,随你怎么改,源代码具有很高的参考价值。
第一步:添加用户控件,命名为 myNewClock第二步:定义变量,要用到时钟,画布等private Timer myTimer//定义时钟,定时重新绘制 private Graphics g//创建画布 private Pen pen//创建画笔 private int width//画布高度 private int height//画布宽度第三步:定义属性,可以自由增加 Color hourColor Color.Red /// /// 时钟颜色 /// CategoryAttribute颜色 Description时钟颜色 public Color HourColor get return hourColor set hourColorvalue Color minuteColor Color.Green /// /// 分钟颜色 /// CategoryAttribute颜色 Description分钟颜色 public Color MinuteColor get return minuteColor set minuteColor value Color secondColor Color.Blue////// 秒钟颜色///CategoryAttribute颜色 Description秒钟颜色public Color SecondColor get return secondColor set secondColor value Color bigScaleColor Color.DarkGreen////// 大刻度颜色///CategoryAttribute颜色 Description大刻度颜色public Color BigScaleColor get return bigScaleColor set bigScaleColor value Color litterScaleColor Color.Olive////// 小刻度颜色///CategoryAttribute颜色 Description小刻度颜色public Color LitterScaleColor get return litterScaleColor set litterScaleColor value Color textColor Color.White////// 刻度值颜色///CategoryAttribute颜色 Description刻度值颜色public Color TextColor get return textColor set textColor value Color bigBackColor Color.Black /// /// 外圆背景色 /// CategoryAttribute颜色 Description外圆背景颜色 public Color BigBackColor get return bigBackColor set bigBackColor value Color litterBackColor Color.White /// /// 内圆颜色 /// CategoryAttribute颜色 Description内圆颜色 public Color LitterBackColor get return litterBackColor set litterBackColor value 第四步:重写 OnPaint 方法protected override void OnPaintPaintEventArgs e base.OnPainte 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.FillEllipsenew SolidBrush bigBackColor x1 2 y1 2 width - 4height - 4 //外圆 pen new Pennew SolidBrushlitterBackColor 2 g.DrawEllipsepen x1 7 y1 7 width - 13 height - 13// 内圆 g.TranslateTransformx1 width / 2 y1 height / 2//重新设置坐标原点 g.FillEllipseBrushes.White -5 -5 10 10//绘制表盘中心 for int x 0 x 60 x //小刻度 g.FillRectanglenew SolidBrushlitterScaleColor newRectangle-2 System.Convert.ToInt16height - 8 / 2 - 2 -1 3 10 g.RotateTransform6//偏移角度 for int i 12 i 0 i-- //大刻度 string myString i.ToString //绘制整点刻度 g.FillRectanglenew SolidBrushbigScaleColor newRectangle-3 System.C