MarqueeLabel.cs
author sl
Mon, 16 Jun 2014 12:18:46 +0200
changeset 2 f516c3f656bf
parent 0 f6eca6facd07
child 5 02716ce36263
permissions -rw-r--r--
Adding OwnTimer property to marquee. This enables us to use an external timer
and thus use a single timer for multiple marquee.
sl@0
     1
using System;
sl@0
     2
using System.Collections.Generic;
sl@0
     3
using System.ComponentModel;
sl@0
     4
using System.Diagnostics;
sl@0
     5
using System.Linq;
sl@0
     6
using System.Text;
sl@0
     7
using System.Threading.Tasks;
sl@0
     8
//using System.Timers;
sl@0
     9
using System.Windows.Forms;
sl@0
    10
using System.Drawing;
sl@0
    11
sl@0
    12
namespace SharpDisplayManager
sl@0
    13
{
sl@0
    14
    class MarqueeLabel : Label
sl@0
    15
    {
sl@2
    16
        private bool iOwnTimer;
sl@0
    17
sl@0
    18
        [Category("Behavior")]
sl@0
    19
        [Description("How fast is our text scrolling, in pixels per second.")]
sl@0
    20
        [DefaultValue(32)]
sl@0
    21
        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
sl@0
    22
        public int PixelsPerSecond { get; set; }
sl@0
    23
sl@2
    24
        [Category("Behavior")]
sl@2
    25
        [Description("Use an internal or an external timer.")]
sl@2
    26
        [DefaultValue(true)]
sl@2
    27
        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
sl@2
    28
        public bool OwnTimer
sl@2
    29
        {
sl@2
    30
            get
sl@2
    31
            {
sl@2
    32
                return iOwnTimer;
sl@2
    33
            }
sl@2
    34
            set
sl@2
    35
            {
sl@2
    36
                iOwnTimer = value;
sl@2
    37
sl@2
    38
                if (iOwnTimer)
sl@2
    39
                {
sl@2
    40
                    Timer = new Timer();
sl@2
    41
                    Timer.Interval = 10;
sl@2
    42
                    Timer.Tick += new EventHandler(Timer_Tick);
sl@2
    43
                    Timer.Start();
sl@2
    44
                }
sl@2
    45
                else
sl@2
    46
                {
sl@2
    47
                    if (Timer != null)
sl@2
    48
                        Timer.Dispose();
sl@2
    49
                    Timer = null;
sl@2
    50
                }
sl@2
    51
sl@2
    52
            }
sl@2
    53
        }
sl@2
    54
        
sl@2
    55
        private int CurrentPosition { get; set; }
sl@2
    56
        private Timer Timer { get; set; }
sl@0
    57
        private DateTime LastTickTime { get; set; }
sl@0
    58
        private double PixelsLeft { get; set; }
sl@0
    59
        //DateTime a = new DateTime(2010, 05, 12, 13, 15, 00);
sl@2
    60
        //DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
sl@2
    61
        //Console.WriteLine(b.Subtract(a).TotalMinutes);
sl@0
    62
sl@0
    63
        public MarqueeLabel()
sl@0
    64
        {
sl@0
    65
            UseCompatibleTextRendering = true;
sl@0
    66
            //PixelsPerSecond = 32;
sl@0
    67
            LastTickTime = DateTime.Now;
sl@0
    68
            PixelsLeft = 0;
sl@0
    69
        }
sl@0
    70
sl@2
    71
        public void UpdateAnimation(DateTime aLastTickTime, DateTime aNewTickTime)
sl@0
    72
        {
sl@0
    73
            while (CurrentPosition > Width)
sl@0
    74
            {
sl@0
    75
                CurrentPosition = -Width;
sl@0
    76
            }
sl@0
    77
sl@2
    78
            PixelsLeft += aNewTickTime.Subtract(aLastTickTime).TotalSeconds * PixelsPerSecond;
sl@0
    79
sl@0
    80
            //Keep track of our pixels left over
sl@0
    81
            //PixelsLeft = offset - Math.Truncate(offset);
sl@0
    82
            double offset = Math.Truncate(PixelsLeft);
sl@0
    83
            PixelsLeft -= offset;
sl@0
    84
sl@0
    85
            CurrentPosition += Convert.ToInt32(offset);
sl@0
    86
sl@0
    87
            /*
sl@0
    88
            if (offset > 1.0)
sl@0
    89
            {
sl@0
    90
                BackColor = Color.Red;
sl@0
    91
            }
sl@0
    92
            else if (offset==1.0)
sl@0
    93
            {
sl@0
    94
                if (BackColor != Color.White)
sl@0
    95
                {
sl@0
    96
                    BackColor = Color.White;
sl@0
    97
                }
sl@0
    98
sl@0
    99
            }
sl@0
   100
            else
sl@0
   101
            {
sl@0
   102
                //Too slow
sl@0
   103
                //BackColor = Color.Green;
sl@0
   104
            }*/
sl@0
   105
sl@0
   106
            //Only redraw if something has changed
sl@0
   107
            if (offset != 0)
sl@0
   108
            {
sl@0
   109
                Invalidate();
sl@0
   110
            }
sl@2
   111
        }
sl@0
   112
sl@2
   113
        void Timer_Tick(object sender, EventArgs e)
sl@2
   114
        {
sl@2
   115
            DateTime NewTickTime = DateTime.Now;                       
sl@2
   116
            //
sl@2
   117
            UpdateAnimation(LastTickTime, NewTickTime);
sl@2
   118
            //
sl@2
   119
            LastTickTime = NewTickTime;
sl@0
   120
        }
sl@0
   121
sl@0
   122
        protected override void OnPaint(PaintEventArgs e)
sl@0
   123
        {
sl@0
   124
            //Disable anti-aliasing
sl@0
   125
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
sl@0
   126
            e.Graphics.TranslateTransform((float)CurrentPosition, 0);
sl@0
   127
            base.OnPaint(e);
sl@0
   128
        }
sl@0
   129
sl@0
   130
        protected override void Dispose(bool disposing)
sl@0
   131
        {
sl@0
   132
            if (disposing)
sl@0
   133
            {
sl@0
   134
                if (Timer != null)
sl@0
   135
                    Timer.Dispose();
sl@0
   136
            }
sl@0
   137
            Timer = null;
sl@0
   138
        }
sl@0
   139
    }
sl@0
   140
}