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