sl@0: using System; sl@0: using System.Collections.Generic; sl@0: using System.ComponentModel; sl@0: using System.Diagnostics; sl@0: using System.Linq; sl@0: using System.Text; sl@0: using System.Threading.Tasks; sl@0: //using System.Timers; sl@0: using System.Windows.Forms; sl@0: using System.Drawing; sl@0: sl@0: namespace SharpDisplayManager sl@0: { sl@0: class MarqueeLabel : Label sl@0: { sl@2: private bool iOwnTimer; sl@0: sl@0: [Category("Behavior")] sl@0: [Description("How fast is our text scrolling, in pixels per second.")] sl@0: [DefaultValue(32)] sl@0: [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)] sl@0: public int PixelsPerSecond { get; set; } sl@0: sl@2: [Category("Behavior")] sl@2: [Description("Use an internal or an external timer.")] sl@2: [DefaultValue(true)] sl@2: [Browsable(true), EditorBrowsable(EditorBrowsableState.Always)] sl@2: public bool OwnTimer sl@2: { sl@2: get sl@2: { sl@2: return iOwnTimer; sl@2: } sl@2: set sl@2: { sl@2: iOwnTimer = value; sl@2: sl@2: if (iOwnTimer) sl@2: { sl@2: Timer = new Timer(); sl@2: Timer.Interval = 10; sl@2: Timer.Tick += new EventHandler(Timer_Tick); sl@2: Timer.Start(); sl@2: } sl@2: else sl@2: { sl@2: if (Timer != null) sl@2: Timer.Dispose(); sl@2: Timer = null; sl@2: } sl@2: sl@2: } sl@2: } sl@2: sl@2: private int CurrentPosition { get; set; } sl@2: private Timer Timer { get; set; } sl@0: private DateTime LastTickTime { get; set; } sl@0: private double PixelsLeft { get; set; } sl@0: //DateTime a = new DateTime(2010, 05, 12, 13, 15, 00); sl@2: //DateTime b = new DateTime(2010, 05, 12, 13, 45, 00); sl@2: //Console.WriteLine(b.Subtract(a).TotalMinutes); sl@0: sl@0: public MarqueeLabel() sl@0: { sl@0: UseCompatibleTextRendering = true; sl@0: //PixelsPerSecond = 32; sl@0: LastTickTime = DateTime.Now; sl@0: PixelsLeft = 0; sl@0: } sl@0: sl@2: public void UpdateAnimation(DateTime aLastTickTime, DateTime aNewTickTime) sl@0: { sl@0: while (CurrentPosition > Width) sl@0: { sl@0: CurrentPosition = -Width; sl@0: } sl@0: sl@2: PixelsLeft += aNewTickTime.Subtract(aLastTickTime).TotalSeconds * PixelsPerSecond; sl@0: sl@0: //Keep track of our pixels left over sl@0: //PixelsLeft = offset - Math.Truncate(offset); sl@0: double offset = Math.Truncate(PixelsLeft); sl@0: PixelsLeft -= offset; sl@0: sl@0: CurrentPosition += Convert.ToInt32(offset); sl@0: sl@0: /* sl@0: if (offset > 1.0) sl@0: { sl@0: BackColor = Color.Red; sl@0: } sl@0: else if (offset==1.0) sl@0: { sl@0: if (BackColor != Color.White) sl@0: { sl@0: BackColor = Color.White; sl@0: } sl@0: sl@0: } sl@0: else sl@0: { sl@0: //Too slow sl@0: //BackColor = Color.Green; sl@0: }*/ sl@0: sl@0: //Only redraw if something has changed sl@0: if (offset != 0) sl@0: { sl@0: Invalidate(); sl@0: } sl@2: } sl@0: sl@2: void Timer_Tick(object sender, EventArgs e) sl@2: { sl@2: DateTime NewTickTime = DateTime.Now; sl@2: // sl@2: UpdateAnimation(LastTickTime, NewTickTime); sl@2: // sl@2: LastTickTime = NewTickTime; sl@0: } sl@0: sl@0: protected override void OnPaint(PaintEventArgs e) sl@0: { sl@0: //Disable anti-aliasing sl@0: e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit; sl@0: e.Graphics.TranslateTransform((float)CurrentPosition, 0); sl@0: base.OnPaint(e); sl@0: } sl@0: sl@0: protected override void Dispose(bool disposing) sl@0: { sl@0: if (disposing) sl@0: { sl@0: if (Timer != null) sl@0: Timer.Dispose(); sl@0: } sl@0: Timer = null; sl@0: } sl@0: } sl@0: }