Smooth scrolling text en c#

1. Create a C# windows form application
2. In the Solution Explorer window, right click the project node and click Add->New Item button.
3. Select the Component Class template, rename it to MarqueeLabel.cs and click Add button
4. Replace the MarqueeLabel implementation with Fernando's codes
5. Delete the default MarqueeLabel.designer.cs file
6. Build you solution
7. In your Form designer, you will find the MarqueeLabel component in the tool box window.
8. Drag it onto your form, it will work correctly.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DeteccionPuertos
{
    class MarqueeLabel : Label
    {
        private int CurrentPosition { get; set; }
        private Timer Timer { get; set; }

        public MarqueeLabel()
        {
            UseCompatibleTextRendering = true;
            Timer = new Timer();
            Timer.Interval = 25;
            Timer.Tick += new EventHandler(Timer_Tick);
            //Timer.Start();
        }

        public void Start()
        {
            Timer.Start();
        }

        public void Stop()
        {
            Timer.Stop();
        }
        void Timer_Tick(object sender, EventArgs e)
        {
            if (CurrentPosition > Width)
                CurrentPosition = -Width;
            else
                CurrentPosition += 2;

            Invalidate();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.TranslateTransform((float)CurrentPosition, 0);
            base.OnPaint(e);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (Timer != null)
                    Timer.Dispose();
            }
            Timer = null;
        }
    }
}



No hay comentarios:

Publicar un comentario