【Asp.net精品源码栏目提醒】:网学会员为广大网友收集整理了,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.Convert.ToInt16height - 8 / 2 - 2 -1 6 20 //绘制数值 g.DrawStringmyString new Fontnew FontFamilyTimes NewRoman14 FontStyle.Bold GraphicsUnit.Pixelnew SolidBrush textColor newPointFmyString.Length -6 height - 8 / -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 PensecondColor 1 pen.EndCap LineCap.ArrowAnchor g.RotateTransform6 second float y float-1 height / 2.75 g.DrawLinepen new PointF0 0 new PointFfloat0 y ////绘分针 pen new PenminuteColor 4 pen.EndCap LineCap.ArrowAnchor g.RotateTransform-6 second //恢复系统偏移量,再计算下次偏移 g.RotateTransformfloatsecond 0.1 minute 6 y float-1 height - 30 / 2.75 g.DrawLinepen new PointF0 0 new PointFfloat0 y ////绘时针 pen new PenhourColor 6 pen.EndCap LineCap.ArrowAnchor g.RotateTransformfloat-second 0.1 - minute 6//恢复系统偏移量,再计算下次偏移 g.RotateTransformfloatsecond 0.01 minute 0.1 hour 30 y float-1 height - 45 / 2.75 g.DrawLinepen new PointF0 0 new PointFfloat0 y 贴出全部代码://控件名:myNewClock//作者:刘典武//时间:2011-06-10using Systemusing System.Collections.Genericusing System.ComponentModelusing System.Drawingusing System.Datausing System.Textusing System.Windows.Formsusing System.Drawing.Drawing2Dnamespace myControl public partial class myNewClock : UserControl public myNewClock InitializeComponent this.SetStyleControlStyles.AllPaintingInWmPaint true this.SetStyleControlStyles.DoubleBuffer true this.SetStyleControlStyles.ResizeRedraw true this.SetStyleControlStyles.Selectable true this.SetStyleControlStyles.SupportsTransparentBackColor true this.SetStyleControlStyles.UserPaint true myTimer new Timer myTimer.Interval 1000 myTimer.Enabled true myTimer.Tick new EventHandlermyTimer_Tick private void myTimer_Tickobject sender EventArgs e this.Invalidate 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 Col
上一篇:
计算机专业毕业论文主题
下一篇:
首鼠两端