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