Server/MarqueeLabel.cs
author sl
Tue, 14 Oct 2014 19:32:09 +0200
changeset 70 dcd2bbb90d42
parent 41 1864e4fd1728
child 100 7e02ac8b13ba
permissions -rw-r--r--
Support row and column span.
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@15
    14
    [System.ComponentModel.DesignerCategory("Code")]
sl@17
    15
    public class MarqueeLabel : Label
sl@0
    16
    {
sl@2
    17
        private bool iOwnTimer;
sl@5
    18
        private StringFormat iStringFormat;
sl@5
    19
        private SolidBrush iBrush;
sl@6
    20
        private SizeF iTextSize;
sl@8
    21
        private SizeF iSeparatorSize;
sl@34
    22
        private SizeF iScrollSize;
sl@8
    23
sl@8
    24
        [Category("Appearance")]
sl@8
    25
        [Description("Separator in our scrolling loop.")]
sl@8
    26
        [DefaultValue(" | ")]
sl@8
    27
        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
sl@11
    28
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
sl@8
    29
        public string Separator { get; set; }
sl@0
    30
sl@0
    31
        [Category("Behavior")]
sl@0
    32
        [Description("How fast is our text scrolling, in pixels per second.")]
sl@0
    33
        [DefaultValue(32)]
sl@0
    34
        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
sl@11
    35
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
sl@0
    36
        public int PixelsPerSecond { get; set; }
sl@0
    37
sl@2
    38
        [Category("Behavior")]
sl@2
    39
        [Description("Use an internal or an external timer.")]
sl@2
    40
        [DefaultValue(true)]
sl@2
    41
        [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)]
sl@11
    42
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Visible)]
sl@2
    43
        public bool OwnTimer
sl@2
    44
        {
sl@2
    45
            get
sl@2
    46
            {
sl@2
    47
                return iOwnTimer;
sl@2
    48
            }
sl@2
    49
            set
sl@2
    50
            {
sl@2
    51
                iOwnTimer = value;
sl@2
    52
sl@2
    53
                if (iOwnTimer)
sl@2
    54
                {
sl@2
    55
                    Timer = new Timer();
sl@2
    56
                    Timer.Interval = 10;
sl@2
    57
                    Timer.Tick += new EventHandler(Timer_Tick);
sl@2
    58
                    Timer.Start();
sl@2
    59
                }
sl@2
    60
                else
sl@2
    61
                {
sl@2
    62
                    if (Timer != null)
sl@2
    63
                        Timer.Dispose();
sl@2
    64
                    Timer = null;
sl@2
    65
                }
sl@2
    66
sl@2
    67
            }
sl@2
    68
        }
sl@5
    69
sl@2
    70
        private int CurrentPosition { get; set; }
sl@2
    71
        private Timer Timer { get; set; }
sl@0
    72
        private DateTime LastTickTime { get; set; }
sl@0
    73
        private double PixelsLeft { get; set; }
sl@0
    74
        //DateTime a = new DateTime(2010, 05, 12, 13, 15, 00);
sl@2
    75
        //DateTime b = new DateTime(2010, 05, 12, 13, 45, 00);
sl@2
    76
        //Console.WriteLine(b.Subtract(a).TotalMinutes);
sl@0
    77
sl@0
    78
        public MarqueeLabel()
sl@0
    79
        {
sl@0
    80
            UseCompatibleTextRendering = true;
sl@0
    81
            //PixelsPerSecond = 32;
sl@0
    82
            LastTickTime = DateTime.Now;
sl@0
    83
            PixelsLeft = 0;
sl@33
    84
            CurrentPosition = 0;
sl@5
    85
            iBrush = new SolidBrush(ForeColor);
StephaneLenclud@35
    86
sl@41
    87
            //Following is needed if we ever switch from Label to Control base class.
StephaneLenclud@35
    88
            //Without it you get some pretty nasty flicker
StephaneLenclud@35
    89
            //SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
StephaneLenclud@35
    90
            //SetStyle(ControlStyles.UserPaint, true);
sl@41
    91
            //SetStyle(ControlStyles.AllPaintingInWmPaint, true);
StephaneLenclud@35
    92
            //SetStyle(ControlStyles.DoubleBuffer, true);
sl@0
    93
        }
sl@0
    94
sl@2
    95
        public void UpdateAnimation(DateTime aLastTickTime, DateTime aNewTickTime)
sl@0
    96
        {
sl@6
    97
            if (!NeedToScroll())
sl@0
    98
            {
sl@6
    99
                CurrentPosition = 0;
sl@6
   100
                return;
sl@6
   101
            }
sl@6
   102
sl@34
   103
            /*
sl@8
   104
            while (CurrentPosition > (iTextSize.Width + iSeparatorSize.Width))
sl@6
   105
            {
sl@8
   106
                CurrentPosition -= ((int)(iTextSize.Width + iSeparatorSize.Width));
sl@0
   107
            }
sl@34
   108
             */
sl@34
   109
sl@34
   110
            while (CurrentPosition > iScrollSize.Width)
sl@34
   111
            {
sl@34
   112
                CurrentPosition -= (int)iScrollSize.Width;
sl@34
   113
            }
sl@34
   114
sl@0
   115
sl@2
   116
            PixelsLeft += aNewTickTime.Subtract(aLastTickTime).TotalSeconds * PixelsPerSecond;
sl@0
   117
sl@0
   118
            //Keep track of our pixels left over
sl@0
   119
            //PixelsLeft = offset - Math.Truncate(offset);
sl@0
   120
            double offset = Math.Truncate(PixelsLeft);
sl@0
   121
            PixelsLeft -= offset;
sl@0
   122
sl@0
   123
            CurrentPosition += Convert.ToInt32(offset);
sl@0
   124
sl@0
   125
            /*
sl@0
   126
            if (offset > 1.0)
sl@0
   127
            {
sl@0
   128
                BackColor = Color.Red;
sl@0
   129
            }
sl@0
   130
            else if (offset==1.0)
sl@0
   131
            {
sl@0
   132
                if (BackColor != Color.White)
sl@0
   133
                {
sl@0
   134
                    BackColor = Color.White;
sl@0
   135
                }
sl@0
   136
sl@0
   137
            }
sl@0
   138
            else
sl@0
   139
            {
sl@0
   140
                //Too slow
sl@0
   141
                //BackColor = Color.Green;
sl@0
   142
            }*/
sl@0
   143
sl@0
   144
            //Only redraw if something has changed
sl@0
   145
            if (offset != 0)
sl@0
   146
            {
sl@0
   147
                Invalidate();
sl@0
   148
            }
sl@2
   149
        }
sl@0
   150
sl@2
   151
        void Timer_Tick(object sender, EventArgs e)
sl@2
   152
        {
sl@5
   153
            DateTime NewTickTime = DateTime.Now;
sl@2
   154
            //
sl@2
   155
            UpdateAnimation(LastTickTime, NewTickTime);
sl@2
   156
            //
sl@2
   157
            LastTickTime = NewTickTime;
sl@0
   158
        }
sl@0
   159
sl@5
   160
        private StringFormat GetStringFormatFromContentAllignment(ContentAlignment ca)
sl@5
   161
        {
sl@41
   162
            StringFormat format = new StringFormat(StringFormat.GenericTypographic);
sl@5
   163
            switch (ca)
sl@5
   164
            {
sl@5
   165
                case ContentAlignment.TopCenter:
sl@34
   166
                    format.Alignment = StringAlignment.Center;
sl@34
   167
                    format.LineAlignment = StringAlignment.Near;
sl@5
   168
                    break;
sl@5
   169
                case ContentAlignment.TopLeft:
sl@5
   170
                    format.Alignment = StringAlignment.Near;
sl@5
   171
                    format.LineAlignment = StringAlignment.Near;
sl@5
   172
                    break;
sl@5
   173
                case ContentAlignment.TopRight:
sl@34
   174
                    format.Alignment = StringAlignment.Far;
sl@34
   175
                    format.LineAlignment = StringAlignment.Near;
sl@5
   176
                    break;
sl@5
   177
                case ContentAlignment.MiddleCenter:
sl@5
   178
                    format.Alignment = StringAlignment.Center;
sl@5
   179
                    format.LineAlignment = StringAlignment.Center;
sl@5
   180
                    break;
sl@5
   181
                case ContentAlignment.MiddleLeft:
sl@34
   182
                    format.Alignment = StringAlignment.Near;
sl@34
   183
                    format.LineAlignment = StringAlignment.Center;
sl@5
   184
                    break;
sl@5
   185
                case ContentAlignment.MiddleRight:
sl@34
   186
                    format.Alignment = StringAlignment.Far;
sl@34
   187
                    format.LineAlignment = StringAlignment.Center;
sl@34
   188
                    break;
sl@34
   189
                case ContentAlignment.BottomCenter:
sl@5
   190
                    format.Alignment = StringAlignment.Center;
sl@5
   191
                    format.LineAlignment = StringAlignment.Far;
sl@5
   192
                    break;
sl@5
   193
                case ContentAlignment.BottomLeft:
sl@34
   194
                    format.Alignment = StringAlignment.Near;
sl@34
   195
                    format.LineAlignment = StringAlignment.Far;
sl@5
   196
                    break;
sl@5
   197
                case ContentAlignment.BottomRight:
sl@5
   198
                    format.Alignment = StringAlignment.Far;
sl@5
   199
                    format.LineAlignment = StringAlignment.Far;
sl@5
   200
                    break;
sl@5
   201
            }
sl@5
   202
sl@5
   203
            format.FormatFlags |= StringFormatFlags.NoWrap;
sl@5
   204
            format.FormatFlags |= StringFormatFlags.NoClip;
sl@11
   205
            format.FormatFlags |= StringFormatFlags.MeasureTrailingSpaces;
sl@5
   206
            format.Trimming = StringTrimming.None;
sl@5
   207
sl@5
   208
            return format;
sl@5
   209
        }
sl@5
   210
sl@5
   211
        protected override void OnForeColorChanged(EventArgs e)
sl@5
   212
        {
sl@6
   213
            //Color has changed recreate our brush
sl@5
   214
            iBrush = new SolidBrush(ForeColor);
sl@5
   215
sl@5
   216
            base.OnForeColorChanged(e);
sl@5
   217
        }
sl@5
   218
sl@6
   219
sl@6
   220
        private void HandleTextSizeChange()
sl@6
   221
        {
sl@33
   222
            //Reset our timer whenever our text changes
sl@30
   223
            CurrentPosition = 0;
sl@33
   224
            LastTickTime = DateTime.Now;
sl@33
   225
            PixelsLeft = 0;
sl@33
   226
sl@11
   227
            //For all string measurements and drawing issues refer to the following article:
sl@11
   228
            // http://stackoverflow.com/questions/1203087/why-is-graphics-measurestring-returning-a-higher-than-expected-number
sl@6
   229
            //Update text size according to text and font
sl@6
   230
            Graphics g = this.CreateGraphics();
sl@6
   231
            g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
sl@6
   232
            iStringFormat = GetStringFormatFromContentAllignment(TextAlign);
sl@11
   233
            iTextSize = g.MeasureString(Text, Font, Int32.MaxValue, iStringFormat);
sl@11
   234
            iSeparatorSize = g.MeasureString(Separator, Font, Int32.MaxValue, iStringFormat);
sl@34
   235
            //Scroll width is the width of our text and our separator without taking kerning into account since
sl@34
   236
            //both text and separator are drawn independently from each other.
sl@34
   237
            iScrollSize.Width = iSeparatorSize.Width + iTextSize.Width;
sl@34
   238
            iScrollSize.Height = Math.Max(iSeparatorSize.Height, iTextSize.Height); //Not relevant for now
sl@34
   239
            //We don't want scroll with to take kerning into account so we don't use the following
sl@34
   240
            //iScrollSize = g.MeasureString(Text + Separator, Font, Int32.MaxValue, iStringFormat);
sl@6
   241
sl@6
   242
            if (NeedToScroll())
sl@6
   243
            {
sl@6
   244
                //Always align left when scrolling
sl@42
   245
                iStringFormat.Alignment = StringAlignment.Near;
sl@41
   246
            }
sl@42
   247
sl@6
   248
        }
sl@6
   249
sl@6
   250
        protected override void OnTextChanged(EventArgs e)
sl@6
   251
        {
sl@6
   252
            HandleTextSizeChange();
sl@6
   253
sl@6
   254
            base.OnTextChanged(e);
sl@6
   255
        }
sl@6
   256
sl@6
   257
        protected override void OnFontChanged(EventArgs e)
sl@6
   258
        {
sl@6
   259
            HandleTextSizeChange();
sl@6
   260
sl@6
   261
            base.OnFontChanged(e);
sl@6
   262
        }
sl@6
   263
sl@5
   264
        protected override void OnTextAlignChanged(EventArgs e)
sl@5
   265
        {
sl@42
   266
            iStringFormat = GetStringFormatFromContentAllignment(TextAlign);
sl@41
   267
            if (NeedToScroll())
sl@41
   268
            {
sl@41
   269
                //Always align left when scrolling to avoid bugs
sl@42
   270
                iStringFormat.Alignment = StringAlignment.Near;
sl@41
   271
            }
sl@42
   272
sl@41
   273
            Invalidate();
sl@41
   274
            //
sl@5
   275
            base.OnTextAlignChanged(e);
sl@5
   276
        }
sl@5
   277
sl@0
   278
        protected override void OnPaint(PaintEventArgs e)
sl@0
   279
        {
sl@0
   280
            //Disable anti-aliasing
sl@0
   281
            e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
sl@6
   282
            if (NeedToScroll())
sl@6
   283
            {
sl@34
   284
                //Draw it all in a single call would take kerning into account
sl@34
   285
                //e.Graphics.TranslateTransform(-(float)CurrentPosition, 0);
sl@41
   286
                //e.Graphics.DrawString(Text + Separator + Text, Font, iBrush, ClientRectangle, StringFormat);
sl@34
   287
sl@34
   288
                //Doing separate draw operation allows us not to take kerning into account between separator and string
sl@34
   289
                //Draw the first one
sl@33
   290
                e.Graphics.TranslateTransform(-(float)CurrentPosition, 0);
sl@34
   291
                e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
sl@8
   292
                //Draw separator
sl@34
   293
                e.Graphics.TranslateTransform(iTextSize.Width, 0);
sl@34
   294
                e.Graphics.DrawString(Separator, Font, iBrush, ClientRectangle, iStringFormat);
sl@6
   295
                //Draw the last one
sl@34
   296
                e.Graphics.TranslateTransform(iSeparatorSize.Width, 0);
sl@34
   297
                e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
sl@6
   298
            }
sl@6
   299
            else
sl@6
   300
            {
sl@6
   301
                e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
sl@6
   302
            }
sl@6
   303
sl@6
   304
sl@5
   305
sl@5
   306
            //DrawText is not working without anti-aliasing. See: stackoverflow.com/questions/8283631/graphics-drawstring-vs-textrenderer-drawtextwhich-can-deliver-better-quality
sl@5
   307
            //TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor, BackColor, iTextFormatFlags);
sl@5
   308
sl@5
   309
            //base.OnPaint(e);
sl@0
   310
        }
sl@0
   311
sl@6
   312
        public bool NeedToScroll()
sl@6
   313
        {
sl@6
   314
            //if (Width < e.Graphics.MeasureString(Text, Font).Width)
sl@6
   315
            if (Width < iTextSize.Width)
sl@6
   316
            {
sl@6
   317
                return true;
sl@6
   318
            }
sl@6
   319
            return false;
sl@6
   320
        }
sl@6
   321
sl@0
   322
        protected override void Dispose(bool disposing)
sl@0
   323
        {
sl@0
   324
            if (disposing)
sl@0
   325
            {
sl@0
   326
                if (Timer != null)
sl@0
   327
                    Timer.Dispose();
sl@0
   328
            }
sl@0
   329
            Timer = null;
sl@0
   330
        }
sl@0
   331
    }
sl@0
   332
}