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