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