Marquee Label Demo : Marquee Label « Components « C# / C Sharp






Marquee Label Demo

Marquee Label Demo

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

public class Form1 : Form
{
        private MarqueeLabel marqueeLabel1;
        private System.Windows.Forms.GroupBox GroupBox1;
        private System.Windows.Forms.Label Label2;
        private System.Windows.Forms.TrackBar tbInterval;
        private System.Windows.Forms.Label Label1;
        private System.Windows.Forms.TrackBar tbAmount;

      public Form1() {
            InitializeComponent();
            
      }
        private void tbAmount_ValueChanged(object sender, EventArgs e)
        {
            marqueeLabel1.ScrollPixelAmount = tbAmount.Value;
        }

        private void tbInterval_ValueChanged(object sender, EventArgs e)
        {
            marqueeLabel1.tmrScroll.Interval = tbInterval.Value;
        }

        private void InitializeComponent()
        {
            this.GroupBox1 = new System.Windows.Forms.GroupBox();
            this.Label2 = new System.Windows.Forms.Label();
            this.tbInterval = new System.Windows.Forms.TrackBar();
            this.Label1 = new System.Windows.Forms.Label();
            this.tbAmount = new System.Windows.Forms.TrackBar();
            this.marqueeLabel1 = new MarqueeLabel();
            this.GroupBox1.SuspendLayout();
            ((System.ComponentModel.ISupportInitialize)(this.tbInterval)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.tbAmount)).BeginInit();
            
            this.SuspendLayout();
            // 
            // GroupBox1
            // 
            this.GroupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                        | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.GroupBox1.Controls.Add(this.Label2);
            this.GroupBox1.Controls.Add(this.tbInterval);
            this.GroupBox1.Controls.Add(this.Label1);
            this.GroupBox1.Controls.Add(this.tbAmount);
            this.GroupBox1.FlatStyle = System.Windows.Forms.FlatStyle.System;
            this.GroupBox1.Location = new System.Drawing.Point(22, 177);
            this.GroupBox1.Name = "GroupBox1";
            this.GroupBox1.Size = new System.Drawing.Size(336, 132);
            this.GroupBox1.TabIndex = 6;
            this.GroupBox1.TabStop = false;
            // 
            // Label2
            // 
            this.Label2.Location = new System.Drawing.Point(12, 76);
            this.Label2.Name = "Label2";
            this.Label2.Size = new System.Drawing.Size(80, 23);
            this.Label2.TabIndex = 6;
            this.Label2.Text = "Scroll Interval:";
            // 
            // tbInterval
            // 
            this.tbInterval.Location = new System.Drawing.Point(96, 72);
            this.tbInterval.Maximum = 500;
            this.tbInterval.Minimum = 10;
            this.tbInterval.Name = "tbInterval";
            this.tbInterval.Size = new System.Drawing.Size(228, 45);
            this.tbInterval.TabIndex = 5;
            this.tbInterval.TickFrequency = 10;
            this.tbInterval.Value = 100;
            this.tbInterval.ValueChanged += new System.EventHandler(this.tbInterval_ValueChanged);
            // 
            // Label1
            // 
            this.Label1.Location = new System.Drawing.Point(12, 20);
            this.Label1.Name = "Label1";
            this.Label1.Size = new System.Drawing.Size(80, 23);
            this.Label1.TabIndex = 4;
            this.Label1.Text = "Scroll Amount:";
            // 
            // tbAmount
            // 
            this.tbAmount.Location = new System.Drawing.Point(96, 16);
            this.tbAmount.Maximum = 20;
            this.tbAmount.Name = "tbAmount";
            this.tbAmount.Size = new System.Drawing.Size(228, 45);
            this.tbAmount.TabIndex = 3;
            this.tbAmount.Value = 10;
            this.tbAmount.ValueChanged += new System.EventHandler(this.tbAmount_ValueChanged);
            // 
            // marqueeLabel1
            // 
            this.marqueeLabel1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.marqueeLabel1.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(192)))));
            this.marqueeLabel1.Font = new System.Drawing.Font("Verdana", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.marqueeLabel1.ForeColor = System.Drawing.Color.Navy;
            this.marqueeLabel1.Location = new System.Drawing.Point(-2, 7);
            this.marqueeLabel1.Name = "marqueeLabel1";
            this.marqueeLabel1.tmrScroll.Interval = 100;
            this.marqueeLabel1.Size = new System.Drawing.Size(384, 156);
            this.marqueeLabel1.TabIndex = 7;
            this.marqueeLabel1.Tag = "";
            this.marqueeLabel1.DisplayText = "This scrolls!";
            // 
            // MarqueeTestForm
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(381, 322);
            this.Controls.Add(this.marqueeLabel1);
            this.Controls.Add(this.GroupBox1);
            this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.Name = "MarqueeTestForm";
            this.Text = "MarqueeTestForm";
            this.GroupBox1.ResumeLayout(false);
            this.GroupBox1.PerformLayout();
            ((System.ComponentModel.ISupportInitialize)(this.tbInterval)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.tbAmount)).EndInit();
            
            this.ResumeLayout(false);

        }

  [STAThread]
  static void Main()
  {
    Application.EnableVisualStyles();
    Application.Run(new Form1());
  }

}

  public class MarqueeLabel : Control
  {
    public string DisplayText;
    public int ScrollPixelAmount = 10;
    public System.Windows.Forms.Timer tmrScroll;
    private int position = 0;

    public MarqueeLabel()
    {
      this.tmrScroll = new System.Windows.Forms.Timer(new System.ComponentModel.Container());
      this.tmrScroll.Tick += new System.EventHandler(this.tmrScroll_Tick);
      this.Size = new System.Drawing.Size(360, 104);
        
            DoubleBuffered = true;
            ResizeRedraw = true;

      tmrScroll.Enabled = true;
        }

    private void tmrScroll_Tick(object sender, System.EventArgs e)
    {
      position += ScrollPixelAmount;
      Invalidate();            
    }

    protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
    {
            base.OnPaint(e);
      if (position > Width)
      {
        position = -(int)e.Graphics.MeasureString(DisplayText, Font).Width;
      }
      e.Graphics.DrawString(DisplayText, Font, new SolidBrush(ForeColor), position, 0);
    }
    }
           
       








Related examples in the same category