Code :
using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Text; using System.Windows.Forms; namespace DigitalClock { public partial class Digital : UserControl { public enum Digits { Zero = 119, One = 20, Two = 59, Three = 62, Four = 92, Five = 110, Six = 111, Seven = 52, Eight = 127, Nine = 126 } private int digit = 128; private Color onColor = Color.LightGreen; private Color offColor = Color.DarkGreen; private float ledWidth = 1f; private bool canDraw = true; public Color OnColor { get { return onColor; } set { onColor = value; Render(); } } public Color OffColor { get { return offColor; } set { offColor = value; Render(); } } public float LedWidth { get { return ledWidth; } set { ledWidth = value; Refresh(); } } public new void SuspendLayout() { canDraw = false; } public new void ResumeLayout() { canDraw = true; Refresh(); } public Digital() { this.Digit = Digits.Zero; this.Lighted = false; } protected override void OnPaint(PaintEventArgs e) { Render(); base.OnPaint(e); } protected override void OnBackColorChanged(EventArgs e) { Render(); base.OnBackColorChanged(e); } private void Render() { if (!canDraw) return; Graphics g = CreateGraphics(); g.Clear(BackColor); Pen pon = new Pen(onColor, ledWidth); Pen poff = new Pen(offColor, ledWidth); g.DrawLine(((digit & 128) == 0 || (digit & 64) == 0) ? poff : pon, ledWidth / 2f, ledWidth / 2f, ledWidth / 2f, (float)Height / 2f); g.DrawLine(((digit & 128) == 0 || (digit & 32) == 0) ? poff : pon, ledWidth / 2f, ledWidth / 2f, (float)Width - ledWidth, ledWidth / 2f); g.DrawLine(((digit & 128) == 0 || (digit & 16) == 0) ? poff : pon, (float)Width - ledWidth, ledWidth / 2f, (float)Width - ledWidth, (float)Height / 2f); g.DrawLine(((digit & 128) == 0 || (digit & 8) == 0) ? poff : pon, ledWidth / 2f, (float)Height / 2f, (float)Width - ledWidth, (float)Height / 2f); g.DrawLine(((digit & 128) == 0 || (digit & 4) == 0) ? poff : pon, (float)Width - ledWidth, (float)Height / 2f, (float)Width - ledWidth, (float)Height - ledWidth); g.DrawLine(((digit & 128) == 0 || (digit & 2) == 0) ? poff : pon, ledWidth / 2f, (float)Height - ledWidth, (float)Width - ledWidth, (float)Height - ledWidth); g.DrawLine(((digit & 128) == 0 || (digit & 1) == 0) ? poff : pon, ledWidth / 2f, (float)Height / 2f, ledWidth / 2f, (float)Height - ledWidth); } public Digits Digit { get { return (Digits)(digit & 127); } set { digit &= 128 + (int)value; this.Refresh(); } } public int IntDigit { set { if (value > 9 || value < 0) { throw new Exception(string.Format("{0} is not a digit", value)); } switch (value) { case 0: digit = (int)Digits.Zero; break; case 1: digit = (int)Digits.One; break; case 2: digit = (int)Digits.Two; break; case 3: digit = (int)Digits.Three; break; case 4: digit = (int)Digits.Four; break; case 5: digit = (int)Digits.Five; break; case 6: digit = (int)Digits.Six; break; case 7: digit = (int)Digits.Seven; break; case 8: digit = (int)Digits.Eight; break; case 9: digit = (int)Digits.Nine; break; default: throw new Exception(string.Format("Unexpected exception! The digit {0} is not reconized!", value)); } this.Refresh(); } get { switch (Digit) { case Digits.Zero: return 0; case Digits.One: return 1; case Digits.Two: return 2; case Digits.Three: return 3; case Digits.Four: return 4; case Digits.Five: return 5; case Digits.Six: return 6; case Digits.Seven: return 7; case Digits.Eight: return 8; case Digits.Nine: return 9; default: throw new Exception(string.Format("Unexpected exception! The internal digit representation {0} is not reconized!", digit)); } } } public bool Lighted { get { if ((digit & 128) > 0) { return true; } else { return false; } } set { if (value) { digit |= 128; } else { digit &= 127; } this.Refresh(); } } } }
|