Marquee now implementing scroll loop properly.
1.1 --- a/Display.cs Tue Jul 08 11:48:00 2014 +0200
1.2 +++ b/Display.cs Tue Jul 08 15:49:34 2014 +0200
1.3 @@ -5,7 +5,6 @@
1.4 using System.Threading.Tasks;
1.5 //
1.6 using System.Runtime.InteropServices;
1.7 -using System.Text;
1.8
1.9 namespace SharpDisplayManager
1.10 {
1.11 @@ -23,7 +22,7 @@
1.12 {
1.13 if (iDevice == IntPtr.Zero)
1.14 {
1.15 - iDevice = MiniDisplayOpen();
1.16 + iDevice = MiniDisplayOpen();
1.17 }
1.18 return iDevice != IntPtr.Zero;
1.19 }
2.1 --- a/MarqueeLabel.cs Tue Jul 08 11:48:00 2014 +0200
2.2 +++ b/MarqueeLabel.cs Tue Jul 08 15:49:34 2014 +0200
2.3 @@ -14,9 +14,9 @@
2.4 class MarqueeLabel : Label
2.5 {
2.6 private bool iOwnTimer;
2.7 - private TextFormatFlags iTextFormatFlags;
2.8 private StringFormat iStringFormat;
2.9 private SolidBrush iBrush;
2.10 + private SizeF iTextSize;
2.11
2.12 [Category("Behavior")]
2.13 [Description("How fast is our text scrolling, in pixels per second.")]
2.14 @@ -74,9 +74,15 @@
2.15
2.16 public void UpdateAnimation(DateTime aLastTickTime, DateTime aNewTickTime)
2.17 {
2.18 - while (CurrentPosition > Width)
2.19 + if (!NeedToScroll())
2.20 {
2.21 - CurrentPosition = -Width;
2.22 + CurrentPosition = 0;
2.23 + return;
2.24 + }
2.25 +
2.26 + while (CurrentPosition > (iTextSize.Width))
2.27 + {
2.28 + CurrentPosition -= ((int)iTextSize.Width);
2.29 }
2.30
2.31 PixelsLeft += aNewTickTime.Subtract(aLastTickTime).TotalSeconds * PixelsPerSecond;
2.32 @@ -175,62 +181,46 @@
2.33
2.34 protected override void OnForeColorChanged(EventArgs e)
2.35 {
2.36 + //Color has changed recreate our brush
2.37 iBrush = new SolidBrush(ForeColor);
2.38
2.39 base.OnForeColorChanged(e);
2.40 }
2.41
2.42 +
2.43 + private void HandleTextSizeChange()
2.44 + {
2.45 + //Update text size according to text and font
2.46 + Graphics g = this.CreateGraphics();
2.47 + g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
2.48 + iTextSize = g.MeasureString(Text, Font);
2.49 + iStringFormat = GetStringFormatFromContentAllignment(TextAlign);
2.50 +
2.51 + if (NeedToScroll())
2.52 + {
2.53 + //Always align left when scrolling
2.54 + iStringFormat.Alignment = StringAlignment.Near;
2.55 + }
2.56 + }
2.57 +
2.58 + protected override void OnTextChanged(EventArgs e)
2.59 + {
2.60 + HandleTextSizeChange();
2.61 +
2.62 + base.OnTextChanged(e);
2.63 + }
2.64 +
2.65 + protected override void OnFontChanged(EventArgs e)
2.66 + {
2.67 + HandleTextSizeChange();
2.68 +
2.69 + base.OnFontChanged(e);
2.70 + }
2.71 +
2.72 protected override void OnTextAlignChanged(EventArgs e)
2.73 {
2.74 - iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
2.75 -
2.76 - switch (TextAlign)
2.77 - {
2.78 - case ContentAlignment.BottomCenter:
2.79 - iTextFormatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.Bottom;
2.80 - break;
2.81 -
2.82 - case ContentAlignment.BottomLeft:
2.83 - iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.Bottom;
2.84 - break;
2.85 -
2.86 - case ContentAlignment.BottomRight:
2.87 - iTextFormatFlags = TextFormatFlags.Right | TextFormatFlags.Bottom;
2.88 - break;
2.89 -
2.90 - case ContentAlignment.MiddleCenter:
2.91 - iTextFormatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
2.92 - break;
2.93 -
2.94 - case ContentAlignment.MiddleLeft:
2.95 - iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
2.96 - break;
2.97 -
2.98 - case ContentAlignment.MiddleRight:
2.99 - iTextFormatFlags = TextFormatFlags.Right | TextFormatFlags.VerticalCenter;
2.100 - break;
2.101 -
2.102 - case ContentAlignment.TopCenter:
2.103 - iTextFormatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.Top;
2.104 - break;
2.105 -
2.106 - case ContentAlignment.TopLeft:
2.107 - iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.Top;
2.108 - break;
2.109 -
2.110 - case ContentAlignment.TopRight:
2.111 - iTextFormatFlags = TextFormatFlags.Right | TextFormatFlags.Top;
2.112 - break;
2.113 - }
2.114 -
2.115 -
2.116 - iTextFormatFlags |= TextFormatFlags.PreserveGraphicsTranslateTransform;
2.117 - //format |= TextFormatFlags.PreserveGraphicsClipping;
2.118 - iTextFormatFlags |= TextFormatFlags.NoClipping;
2.119 -
2.120 iStringFormat = GetStringFormatFromContentAllignment(TextAlign);
2.121
2.122 -
2.123 base.OnTextAlignChanged(e);
2.124
2.125 }
2.126 @@ -239,8 +229,21 @@
2.127 {
2.128 //Disable anti-aliasing
2.129 e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
2.130 - e.Graphics.TranslateTransform((float)CurrentPosition, 0);
2.131 - e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
2.132 + if (NeedToScroll())
2.133 + {
2.134 + //Draw the first one
2.135 + e.Graphics.TranslateTransform(-(float)CurrentPosition, 0);
2.136 + e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
2.137 + //Draw the last one
2.138 + e.Graphics.TranslateTransform(iTextSize.Width, 0);
2.139 + e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
2.140 + }
2.141 + else
2.142 + {
2.143 + e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
2.144 + }
2.145 +
2.146 +
2.147
2.148 //DrawText is not working without anti-aliasing. See: stackoverflow.com/questions/8283631/graphics-drawstring-vs-textrenderer-drawtextwhich-can-deliver-better-quality
2.149 //TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor, BackColor, iTextFormatFlags);
2.150 @@ -248,6 +251,16 @@
2.151 //base.OnPaint(e);
2.152 }
2.153
2.154 + public bool NeedToScroll()
2.155 + {
2.156 + //if (Width < e.Graphics.MeasureString(Text, Font).Width)
2.157 + if (Width < iTextSize.Width)
2.158 + {
2.159 + return true;
2.160 + }
2.161 + return false;
2.162 + }
2.163 +
2.164 protected override void Dispose(bool disposing)
2.165 {
2.166 if (disposing)