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