Custom drawing of our Marquee text to avoid warpping, clipping and ellipsis.
1.1 --- a/MarqueeLabel.cs Mon Jul 07 22:29:26 2014 +0200
1.2 +++ b/MarqueeLabel.cs Tue Jul 08 11:48:00 2014 +0200
1.3 @@ -14,6 +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
1.11 [Category("Behavior")]
1.12 [Description("How fast is our text scrolling, in pixels per second.")]
1.13 @@ -51,7 +54,7 @@
1.14
1.15 }
1.16 }
1.17 -
1.18 +
1.19 private int CurrentPosition { get; set; }
1.20 private Timer Timer { get; set; }
1.21 private DateTime LastTickTime { get; set; }
1.22 @@ -66,6 +69,7 @@
1.23 //PixelsPerSecond = 32;
1.24 LastTickTime = DateTime.Now;
1.25 PixelsLeft = 0;
1.26 + iBrush = new SolidBrush(ForeColor);
1.27 }
1.28
1.29 public void UpdateAnimation(DateTime aLastTickTime, DateTime aNewTickTime)
1.30 @@ -112,19 +116,136 @@
1.31
1.32 void Timer_Tick(object sender, EventArgs e)
1.33 {
1.34 - DateTime NewTickTime = DateTime.Now;
1.35 + DateTime NewTickTime = DateTime.Now;
1.36 //
1.37 UpdateAnimation(LastTickTime, NewTickTime);
1.38 //
1.39 LastTickTime = NewTickTime;
1.40 }
1.41
1.42 + private StringFormat GetStringFormatFromContentAllignment(ContentAlignment ca)
1.43 + {
1.44 + StringFormat format = new StringFormat();
1.45 + switch (ca)
1.46 + {
1.47 + case ContentAlignment.TopCenter:
1.48 + format.Alignment = StringAlignment.Near;
1.49 + format.LineAlignment = StringAlignment.Center;
1.50 + break;
1.51 + case ContentAlignment.TopLeft:
1.52 + format.Alignment = StringAlignment.Near;
1.53 + format.LineAlignment = StringAlignment.Near;
1.54 + break;
1.55 + case ContentAlignment.TopRight:
1.56 + format.Alignment = StringAlignment.Near;
1.57 + format.LineAlignment = StringAlignment.Far;
1.58 + break;
1.59 + case ContentAlignment.MiddleCenter:
1.60 + format.Alignment = StringAlignment.Center;
1.61 + format.LineAlignment = StringAlignment.Center;
1.62 + break;
1.63 + case ContentAlignment.MiddleLeft:
1.64 + format.Alignment = StringAlignment.Center;
1.65 + format.LineAlignment = StringAlignment.Near;
1.66 + break;
1.67 + case ContentAlignment.MiddleRight:
1.68 + format.Alignment = StringAlignment.Center;
1.69 + format.LineAlignment = StringAlignment.Far;
1.70 + break;
1.71 + case ContentAlignment.BottomCenter:
1.72 + format.Alignment = StringAlignment.Far;
1.73 + format.LineAlignment = StringAlignment.Center;
1.74 + break;
1.75 + case ContentAlignment.BottomLeft:
1.76 + format.Alignment = StringAlignment.Far;
1.77 + format.LineAlignment = StringAlignment.Near;
1.78 + break;
1.79 + case ContentAlignment.BottomRight:
1.80 + format.Alignment = StringAlignment.Far;
1.81 + format.LineAlignment = StringAlignment.Far;
1.82 + break;
1.83 + }
1.84 +
1.85 + format.FormatFlags |= StringFormatFlags.NoWrap;
1.86 + format.FormatFlags |= StringFormatFlags.NoClip;
1.87 + format.Trimming = StringTrimming.None;
1.88 +
1.89 + return format;
1.90 + }
1.91 +
1.92 + protected override void OnForeColorChanged(EventArgs e)
1.93 + {
1.94 + iBrush = new SolidBrush(ForeColor);
1.95 +
1.96 + base.OnForeColorChanged(e);
1.97 + }
1.98 +
1.99 + protected override void OnTextAlignChanged(EventArgs e)
1.100 + {
1.101 + iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
1.102 +
1.103 + switch (TextAlign)
1.104 + {
1.105 + case ContentAlignment.BottomCenter:
1.106 + iTextFormatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.Bottom;
1.107 + break;
1.108 +
1.109 + case ContentAlignment.BottomLeft:
1.110 + iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.Bottom;
1.111 + break;
1.112 +
1.113 + case ContentAlignment.BottomRight:
1.114 + iTextFormatFlags = TextFormatFlags.Right | TextFormatFlags.Bottom;
1.115 + break;
1.116 +
1.117 + case ContentAlignment.MiddleCenter:
1.118 + iTextFormatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter;
1.119 + break;
1.120 +
1.121 + case ContentAlignment.MiddleLeft:
1.122 + iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.VerticalCenter;
1.123 + break;
1.124 +
1.125 + case ContentAlignment.MiddleRight:
1.126 + iTextFormatFlags = TextFormatFlags.Right | TextFormatFlags.VerticalCenter;
1.127 + break;
1.128 +
1.129 + case ContentAlignment.TopCenter:
1.130 + iTextFormatFlags = TextFormatFlags.HorizontalCenter | TextFormatFlags.Top;
1.131 + break;
1.132 +
1.133 + case ContentAlignment.TopLeft:
1.134 + iTextFormatFlags = TextFormatFlags.Left | TextFormatFlags.Top;
1.135 + break;
1.136 +
1.137 + case ContentAlignment.TopRight:
1.138 + iTextFormatFlags = TextFormatFlags.Right | TextFormatFlags.Top;
1.139 + break;
1.140 + }
1.141 +
1.142 +
1.143 + iTextFormatFlags |= TextFormatFlags.PreserveGraphicsTranslateTransform;
1.144 + //format |= TextFormatFlags.PreserveGraphicsClipping;
1.145 + iTextFormatFlags |= TextFormatFlags.NoClipping;
1.146 +
1.147 + iStringFormat = GetStringFormatFromContentAllignment(TextAlign);
1.148 +
1.149 +
1.150 + base.OnTextAlignChanged(e);
1.151 +
1.152 + }
1.153 +
1.154 protected override void OnPaint(PaintEventArgs e)
1.155 {
1.156 //Disable anti-aliasing
1.157 e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
1.158 e.Graphics.TranslateTransform((float)CurrentPosition, 0);
1.159 - base.OnPaint(e);
1.160 + e.Graphics.DrawString(Text, Font, iBrush, ClientRectangle, iStringFormat);
1.161 +
1.162 + //DrawText is not working without anti-aliasing. See: stackoverflow.com/questions/8283631/graphics-drawstring-vs-textrenderer-drawtextwhich-can-deliver-better-quality
1.163 + //TextRenderer.DrawText(e.Graphics, Text, Font, ClientRectangle, ForeColor, BackColor, iTextFormatFlags);
1.164 +
1.165 + //base.OnPaint(e);
1.166 }
1.167
1.168 protected override void Dispose(bool disposing)