实例介绍
【实例截图】
【核心代码】
// Digital Clock Display by Sriram Chitturi (c) Copyright 2004 // A class to display digital digits and characters on a rectangular area using System; using System.Drawing; using System.Drawing.Drawing2D; namespace SriClocks { // A digit class which can draw a number on a graphics surface internal sealed class DigitalDisplay { // pens used to draw the digits private static Pen pen = new Pen(Color.FromArgb(255,0,0)); private static Pen dimPen = null; private float linewidth = 20.0F; private Point[] Points; // end point coordinates of lines in the digital display // for each digit the display bits are set into an int // 'A' and 'P' are included for AM and PM display in the clock private int[] displayNum = new int[12]{63,12,118,94,77,91,123,14,127,95, 111, // to display 'A' 103}; // to display 'P' // Rectangles in which colons are displayed private Rectangle colonRect1, colonRect2; // This function is called by the paint method to display the numbers // A set of bits in the 'displayNum' variable define which of the // display legs to display // Based on this the ones with a '1' are in bright color and the rest // with '0's are in a dull color giving the effect of a digital clock internal void Draw(int num, // number to display Graphics g) // graphics object for drawing { int check; // used to check if a leg of digit should be bright or dull // although pens are global linewidths are specific to each instance pen.Width = dimPen.Width = linewidth; for (int i=0; i<7; i ) { check = (int)System.Math.Pow(2, i); if ((check & displayNum[num])==0) g.DrawLine(dimPen, Points[i*2], Points[i*2 1]); else g.DrawLine(pen, Points[i*2], Points[i*2 1]); } } static private void setDimPen() { if (dimPen == null) dimPen = (Pen)pen.Clone(); dimPen.Width = pen.Width; dimPen.Color = Color.FromArgb( pen.Color.R > 0 ? 60:0, pen.Color.G > 0 ? 60:0, pen.Color.B > 0 ? 60:0); } static internal void SetPenColor(DigitalColor dclr) { pen.Color = Color.FromArgb( (dclr == DigitalColor.RedColor) ? 255:0 , (dclr == DigitalColor.GreenColor) ? 255:0, (dclr == DigitalColor.BlueColor) ? 255:0); setDimPen(); } // function that draws a colon in the middle of the rectangular panel // possible modes are circular or rectangular points in the colon internal void DrawColon(Graphics g, ColonType type, bool dim) { pen.Width = dimPen.Width = linewidth; Pen p = (dim) ? dimPen : pen; // choose a pen for blinking switch(type) { case ColonType.Circular: g.DrawEllipse(p, colonRect1); g.DrawEllipse(p, colonRect2); break; case ColonType.Rectangular: g.DrawRectangle(p, colonRect1); g.DrawRectangle(p, colonRect2); break; } } // Draws the complete rectangle in dim shade to give the digital effect :-) internal void Draw(Graphics g) { // althought pens are static, linewidths are specific to each instance dimPen.Width = linewidth; for (int i=0; i<7; i ) g.DrawLine(dimPen, Points[i*2], Points[i*2 1]); } // Overloaded function to display characters 'A' and 'P' for AM and PM // Using the same algorithm used to display numbers above internal void Draw(char ch, // character to display Graphics g) // graphics object for drawing { // 10 and 11 are indices of A and P in the displayNum array switch(Char.ToUpper(ch)) { case 'A': Draw(10, g); break; case 'P': Draw(11, g); break; } } // Constructor takes a rectangle and prepares the end points // of the lines to be drawn for the clock internal DigitalDisplay(Rectangle rect) { pen.StartCap = LineCap.Triangle; pen.EndCap = LineCap.Triangle; Points = new Point[14]; // there are 7 lines in a display for (int i=0; i<14; i ) Points[i] = new Point(0,0); CalculateAllParameters(rect); } internal void CalculateAllParameters(Rectangle rect) { linewidth = (int)(rect.Width/5); if (linewidth < 2) linewidth = 2; if (linewidth > 20) linewidth = 20; pen.Width = linewidth; setDimPen(); CalculateLineEnds(rect); CalculateColonRectangles(rect); } // Function calculates end points of lines to display // The draw function will draw lines using this data private void CalculateLineEnds(Rectangle rect) { // 0,1,2,9,10,11,12 points share the same left edge X coordinate Points[0].X = Points[1].X = Points[2].X = Points[9].X = Points[10].X = Points[11].X = Points[12].X = rect.Left; // points 3,4,5,6,7,8,13 the right edge X coordinate Points[3].X = Points[4].X = Points[5].X = Points[6].X = Points[7].X = Points[8].X = Points[13].X= rect.Right-(int)linewidth; // Points 1,2,3,4 are the top most points Points[1].Y = Points[2].Y = Points[3].Y = Points[4].Y = (int)(rect.Top); // Points 0,11,12,13,5,6 are the middle points Points[0].Y = Points[11].Y = Points[12].Y = Points[13].Y = Points[5].Y = Points[6].Y = rect.Top (int)((rect.Height-linewidth)/2.0); // points 7,8,9,10 are on the bottom edge Points[7].Y = Points[8].Y = Points[9].Y = Points[10].Y = rect.Top (int)(rect.Height-linewidth); // now adjust the coordinates that were computed, to get the digital look AdjustCoordinates(); } // This function is necessary to give the lines a digital clock look // Push the coordinates a little away so that they look apart private void AdjustCoordinates() { Point swap; // required in case points have to be swapped for (int i=0; i<7; i ) { // Always draw from left to right and top to bottom // Adjust the end points accordingly if (Points[i*2].X > Points[(i*2) 1].X || Points[i*2].Y > Points[(i*2) 1].Y) { swap = Points[i*2]; Points[i*2]= Points[(i*2) 1]; Points[(i*2) 1]=swap; } // for horizontal lines adjust the X coord if (Points[i*2].X != Points[(i*2) 1].X) { Points[i*2].X = (int)(linewidth/1.6); Points[(i*2) 1].X -= (int)(linewidth/1.6); } // for vertical lines adjust the y coord if (Points[i*2].Y != Points[(i*2) 1].Y) { Points[i*2].Y = (int)(linewidth/1.6); Points[(i*2) 1].Y -= (int)(linewidth/1.6); } } } // function to calculate the rectangles required to drawn colon dot inside private void CalculateColonRectangles(Rectangle rect) { colonRect1 = colonRect2 = rect; colonRect1.X = colonRect2.X = rect.X (int)((rect.Width - linewidth)/2.0); colonRect1.Y = rect.Y rect.Height/3; colonRect2.Y = rect.Y (rect.Height*2)/3; colonRect1.Width = colonRect1.Height = colonRect2.Width = colonRect2.Height = (int) linewidth; } } }
标签: C# 数字时钟
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论