MarqueeLabel.cs
author sl
Sat, 14 Jun 2014 15:05:04 +0200
changeset 1 9bf16f9b8f1f
child 2 f516c3f656bf
permissions -rw-r--r--
Removing SUO.
     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 int CurrentPosition { get; set; }
    17         private Timer Timer { get; set; }
    18 
    19 
    20         [Category("Behavior")]
    21         [Description("How fast is our text scrolling, in pixels per second.")]
    22         [DefaultValue(32)]
    23         [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
    24         public int PixelsPerSecond { get; set; }
    25 
    26         private DateTime LastTickTime { get; set; }
    27         private double PixelsLeft { get; set; }
    28         //DateTime a = new DateTime(2010, 05, 12, 13, 15, 00);
    29 //DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
    30 //Console.WriteLine(b.Subtract(a).TotalMinutes);
    31 
    32         public MarqueeLabel()
    33         {
    34             UseCompatibleTextRendering = true;
    35             Timer = new Timer();
    36             Timer.Interval = 10;
    37             Timer.Tick += new EventHandler(Timer_Tick);
    38             Timer.Start();
    39             //PixelsPerSecond = 32;
    40             LastTickTime = DateTime.Now;
    41             PixelsLeft = 0;
    42         }
    43 
    44         void Timer_Tick(object sender, EventArgs e)
    45         {
    46             while (CurrentPosition > Width)
    47             {
    48                 CurrentPosition = -Width;
    49             }
    50 
    51             DateTime NewTickTime=DateTime.Now;
    52             PixelsLeft += NewTickTime.Subtract(LastTickTime).TotalSeconds * PixelsPerSecond;
    53             LastTickTime = NewTickTime;
    54             //offset += PixelsLeft;
    55 
    56             //Keep track of our pixels left over
    57             //PixelsLeft = offset - Math.Truncate(offset);
    58             double offset = Math.Truncate(PixelsLeft);
    59             PixelsLeft -= offset;
    60 
    61             CurrentPosition += Convert.ToInt32(offset);
    62 
    63             /*
    64             if (offset > 1.0)
    65             {
    66                 BackColor = Color.Red;
    67             }
    68             else if (offset==1.0)
    69             {
    70                 if (BackColor != Color.White)
    71                 {
    72                     BackColor = Color.White;
    73                 }
    74 
    75             }
    76             else
    77             {
    78                 //Too slow
    79                 //BackColor = Color.Green;
    80             }*/
    81 
    82             //Only redraw if something has changed
    83             if (offset != 0)
    84             {
    85                 Invalidate();
    86             }
    87 
    88 
    89 
    90         }
    91 
    92         protected override void OnPaint(PaintEventArgs e)
    93         {
    94             //Disable anti-aliasing
    95             e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
    96             e.Graphics.TranslateTransform((float)CurrentPosition, 0);
    97             base.OnPaint(e);
    98         }
    99 
   100         protected override void Dispose(bool disposing)
   101         {
   102             if (disposing)
   103             {
   104                 if (Timer != null)
   105                     Timer.Dispose();
   106             }
   107             Timer = null;
   108         }
   109     }
   110 }