MarqueeLabel.cs
changeset 0 f6eca6facd07
child 2 f516c3f656bf
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/MarqueeLabel.cs	Sat Jun 14 12:51:25 2014 +0200
     1.3 @@ -0,0 +1,110 @@
     1.4 +using System;
     1.5 +using System.Collections.Generic;
     1.6 +using System.ComponentModel;
     1.7 +using System.Diagnostics;
     1.8 +using System.Linq;
     1.9 +using System.Text;
    1.10 +using System.Threading.Tasks;
    1.11 +//using System.Timers;
    1.12 +using System.Windows.Forms;
    1.13 +using System.Drawing;
    1.14 +
    1.15 +namespace SharpDisplayManager
    1.16 +{
    1.17 +    class MarqueeLabel : Label
    1.18 +    {
    1.19 +        private int CurrentPosition { get; set; }
    1.20 +        private Timer Timer { get; set; }
    1.21 +
    1.22 +
    1.23 +        [Category("Behavior")]
    1.24 +        [Description("How fast is our text scrolling, in pixels per second.")]
    1.25 +        [DefaultValue(32)]
    1.26 +        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    1.27 +        public int PixelsPerSecond { get; set; }
    1.28 +
    1.29 +        private DateTime LastTickTime { get; set; }
    1.30 +        private double PixelsLeft { get; set; }
    1.31 +        //DateTime a = new DateTime(2010, 05, 12, 13, 15, 00);
    1.32 +//DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
    1.33 +//Console.WriteLine(b.Subtract(a).TotalMinutes);
    1.34 +
    1.35 +        public MarqueeLabel()
    1.36 +        {
    1.37 +            UseCompatibleTextRendering = true;
    1.38 +            Timer = new Timer();
    1.39 +            Timer.Interval = 10;
    1.40 +            Timer.Tick += new EventHandler(Timer_Tick);
    1.41 +            Timer.Start();
    1.42 +            //PixelsPerSecond = 32;
    1.43 +            LastTickTime = DateTime.Now;
    1.44 +            PixelsLeft = 0;
    1.45 +        }
    1.46 +
    1.47 +        void Timer_Tick(object sender, EventArgs e)
    1.48 +        {
    1.49 +            while (CurrentPosition > Width)
    1.50 +            {
    1.51 +                CurrentPosition = -Width;
    1.52 +            }
    1.53 +
    1.54 +            DateTime NewTickTime=DateTime.Now;
    1.55 +            PixelsLeft += NewTickTime.Subtract(LastTickTime).TotalSeconds * PixelsPerSecond;
    1.56 +            LastTickTime = NewTickTime;
    1.57 +            //offset += PixelsLeft;
    1.58 +
    1.59 +            //Keep track of our pixels left over
    1.60 +            //PixelsLeft = offset - Math.Truncate(offset);
    1.61 +            double offset = Math.Truncate(PixelsLeft);
    1.62 +            PixelsLeft -= offset;
    1.63 +
    1.64 +            CurrentPosition += Convert.ToInt32(offset);
    1.65 +
    1.66 +            /*
    1.67 +            if (offset > 1.0)
    1.68 +            {
    1.69 +                BackColor = Color.Red;
    1.70 +            }
    1.71 +            else if (offset==1.0)
    1.72 +            {
    1.73 +                if (BackColor != Color.White)
    1.74 +                {
    1.75 +                    BackColor = Color.White;
    1.76 +                }
    1.77 +
    1.78 +            }
    1.79 +            else
    1.80 +            {
    1.81 +                //Too slow
    1.82 +                //BackColor = Color.Green;
    1.83 +            }*/
    1.84 +
    1.85 +            //Only redraw if something has changed
    1.86 +            if (offset != 0)
    1.87 +            {
    1.88 +                Invalidate();
    1.89 +            }
    1.90 +
    1.91 +
    1.92 +
    1.93 +        }
    1.94 +
    1.95 +        protected override void OnPaint(PaintEventArgs e)
    1.96 +        {
    1.97 +            //Disable anti-aliasing
    1.98 +            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
    1.99 +            e.Graphics.TranslateTransform((float)CurrentPosition, 0);
   1.100 +            base.OnPaint(e);
   1.101 +        }
   1.102 +
   1.103 +        protected override void Dispose(bool disposing)
   1.104 +        {
   1.105 +            if (disposing)
   1.106 +            {
   1.107 +                if (Timer != null)
   1.108 +                    Timer.Dispose();
   1.109 +            }
   1.110 +            Timer = null;
   1.111 +        }
   1.112 +    }
   1.113 +}