1.1 --- a/MarqueeLabel.cs Tue Jul 08 11:48:00 2014 +0200
1.2 +++ b/MarqueeLabel.cs Tue Jul 08 15:49:34 2014 +0200
1.3 @@ -14,9 +14,9 @@
1.4 class MarqueeLabel : Label
1.5 {
1.6 private bool iOwnTimer;
1.7 - private TextFormatFlags iTextFormatFlags;
1.8 private StringFormat iStringFormat;
1.9 private SolidBrush iBrush;
1.10 + private SizeF iTextSize;
1.11
1.12 [Category("Behavior")]
1.13 [Description("How fast is our text scrolling, in pixels per second.")]
1.14 @@ -74,9 +74,15 @@
1.15
1.16 public void UpdateAnimation(DateTime aLastTickTime, DateTime aNewTickTime)
1.17 {
1.18 - while (CurrentPosition > Width)
1.19 + if (!NeedToScroll())
1.20 {
1.21 - CurrentPosition = -Width;
1.22 + CurrentPosition = 0;
1.23 + return;
1.24 + }
1.25 +
1.26 + while (CurrentPosition > (iTextSize.Width))
1.27 + {
1.28 + CurrentPosition -= ((int)iTextSize.Width);
1.29 }
1.30
1.31 PixelsLeft += aNewTickTime.Subtract(aLastTickTime).TotalSeconds * PixelsPerSecond;
1.32 @@ -175,62 +181,46 @@
1.33
1.34 protected override void OnForeColorChanged(EventArgs e)
1.35 {
1.36 + //Color has changed recreate our brush
1.37 iBrush = new SolidBrush(ForeColor);
1.38
1.39 base.OnForeColorChanged(e);
1.40 }
1.41
1.42 +
1.43 + private void HandleTextSizeChange()
1.44 + {
1.45 + //Update text size according to text and font
1.46 + Graphics g = this.CreateGraphics();
1.47 + g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
1.48 + iTextSize = g.MeasureString(Text, Font);
1.49 + iStringFormat = GetStringFormatFromContentAllignment(TextAlign);
1.50 +
1.51 + if (NeedToScroll())
1.52 + {
1.53 + //Always align left when scrolling
1.54 + iStringFormat.Alignment = StringAlignment.Near;
1.55 + }
1.56 + }
1.57 +
1.58 + protected override void OnTextChanged(EventArgs e)
1.59 + {
1.60 + HandleTextSizeChange();
1.61 +
1.62 + base.OnTextChanged(e);
1.63 + }
1.64 +
1.65 + protected override void OnFontChanged(EventArgs e)
1.66 + {
1.67 + HandleTextSizeChange();
1.68 +
1.69 + base.OnFontChanged(e);
1.70 + }
1.71 +
1.72 protected override void OnTextAlignChanged(EventArgs e)
1.73 {
1.74 - iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
1.75 -
1.76 - switch (TextAlign)
1.77 - {
1.78 - case ContentAlignment.BottomCenter:
1.79 - iTextFormatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.Bottom;
1.80 - break;
1.81 -
1.82 - case ContentAlignment.BottomLeft:
1.83 - iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.Bottom;
1.84 - break;
1.85 -
1.86 - case ContentAlignment.BottomRight:
1.87 - iTextFormatFlags = TextFormatFlags.Right | TextFormatFlags.Bottom;
1.88 - break;
1.89 -
1.90 - case ContentAlignment.MiddleCenter:
1.91 - iTextFormatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
1.92 - break;
1.93 -
1.94 - case ContentAlignment.MiddleLeft:
1.95 - iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
1.96 - break;
1.97 -
1.98 - case ContentAlignment.MiddleRight:
1.99 - iTextFormatFlags = TextFormatFlags.Right | TextFormatFlags.VerticalCenter;
1.100 - break;
1.101 -
1.102 - case ContentAlignment.TopCenter:
1.103 - iTextFormatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.Top;
1.104 - break;
1.105 -
1.106 - case ContentAlignment.TopLeft:
1.107 - iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.Top;
1.108 - break;
1.109 -
1.110 - case ContentAlignment.TopRight:
1.111 - iTextFormatFlags = TextFormatFlags.Right | TextFormatFlags.Top;
1.112 - break;
1.113 - }
1.114 -
1.115 -
1.116 - iTextFormatFlags |= TextFormatFlags.PreserveGraphicsTranslateTransform;
1.117 - //format |= TextFormatFlags.PreserveGraphicsClipping;
1.118 - iTextFormatFlags |= TextFormatFlags.NoClipping;
1.119 -
1.120 iStringFormat = GetStringFormatFromContentAllignment(TextAlign);
1.121
1.122 -
1.123 base.OnTextAlignChanged(e);
1.124
1.125 }
1.126 @@ -239,8 +229,21 @@
1.127 {
1.128 //Disable anti-aliasing
1.129 e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
1.130 - e.Graphics.TranslateTransform((float)CurrentPosition, 0);
1.131 - e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
1.132 + if (NeedToScroll())
1.133 + {
1.134 + //Draw the first one
1.135 + e.Graphics.TranslateTransform(-(float)CurrentPosition, 0);
1.136 + e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
1.137 + //Draw the last one
1.138 + e.Graphics.TranslateTransform(iTextSize.Width, 0);
1.139 + e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
1.140 + }
1.141 + else
1.142 + {
1.143 + e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
1.144 + }
1.145 +
1.146 +
1.147
1.148 //DrawText is not working without anti-aliasing. See: stackoverflow.com/questions/8283631/graphics-drawstring-vs-textrenderer-drawtextwhich-can-deliver-better-quality
1.149 //TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor, BackColor, iTextFormatFlags);
1.150 @@ -248,6 +251,16 @@
1.151 //base.OnPaint(e);
1.152 }
1.153
1.154 + public bool NeedToScroll()
1.155 + {
1.156 + //if (Width < e.Graphics.MeasureString(Text, Font).Width)
1.157 + if (Width < iTextSize.Width)
1.158 + {
1.159 + return true;
1.160 + }
1.161 + return false;
1.162 + }
1.163 +
1.164 protected override void Dispose(bool disposing)
1.165 {
1.166 if (disposing)