Icebird

Delphi/C# - My favorite programming language

导航

[C# Control] 走马灯(Marquee)

本来打算自己用Graphics.DrawString的,但是发现效果没有直接利用Label的好
代码没什么可研究的,不过效果似乎还不错

///////////////////////////////////////////////////////////////////////////////
//  Marquee.cs
//  走马灯
//
//  @remarks For study purpose
//
//  @author Icebird @date 2006-06-22
///////////////////////////////////////////////////////////////////////////////
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace ControlsEx
{
    
/// <summary>
    
/// 走马灯
    
/// </summary>

    public class Marquee : System.Windows.Forms.Control
    
{
        
/// <summary>
        
/// 必需的设计器变量。
        
/// </summary>

        private System.ComponentModel.Container components = null;

        
/// <summary>
        
/// Constructor
        
/// </summary>

        public Marquee()
        
{
            InitializeComponent();
        }


        
/// <summary>
        
/// 清理所有正在使用的资源。
        
/// </summary>

        protected override void Dispose( bool disposing )
        
{
            
if( disposing )
            
{
                
if( components != null )
                
{
                    components.Dispose();
                    timer.Stop();
                    timer.Dispose();
                    lbl.Dispose();
                }

            }

            
base.Dispose( disposing );
        }


        
private Timer timer;
        
private Label lbl;

        
组件设计器生成的代码

        
/// <summary>
        
/// 字体
        
/// </summary>

        public override Font Font
        
{
            
get
            
{
                
return base.Font;
            }

            
set
            
{
                
base.Font = value;
                lbl.Font 
= value;
            }

        }


        
/// <summary>
        
/// 显示文字,用'|'分隔信息
        
/// </summary>

        public override string Text
        
{
            
get
            
{
                
return base.Text;
            }

            
set
            
{
                
base.Text = value;
                infos 
= value.Split(new char[] {'|'});
                
if (infos.Length > 0)
                
{
                    index 
= 0;
                    lbl.Text 
= infos[0];
                }

                
else
                    lbl.Text 
= value;
                offset 
= -2;
                lbl.Top 
= (ClientSize.Height - lbl.Height) / 2;
            }

        }


        
protected override void OnResize(EventArgs e)
        
{
            
base.OnResize (e);
            lbl.Top 
= (ClientSize.Height - lbl.Height) / 2;
        }


        
protected override void OnEnabledChanged(EventArgs e)
        
{
            
base.OnEnabledChanged (e);

            lbl.Visible 
= Enabled;
            
if (Enabled)
                timer.Start();
            
else
                timer.Stop();
        }


        
private int offset;
        
private int index;

        
private void timer_Tick(object sender, EventArgs e)
        
{
            
if (!Enabled)
                
return;
            offset
++;
            
int newLeft = Width - offset;
            lbl.Left 
= newLeft;
            
if (lbl.Text.Length > 0)
                lbl.Visible 
= true;
            lbl.Invalidate();

            
if (newLeft + lbl.Width < 0)
            
{
                
//显示下一条
                offset = -2;
                index
++;
                
if (index >= infos.Length)
                    index 
= 0;
                
if (Width > 4)
                    lbl.Left 
= Width;
                lbl.Text 
= infos[index];
            }

        }


        
private string[] infos;

        
/// <summary>
        
/// 滚动速度,范围1~10,1最快,10最慢
        
/// </summary>

        public int Speed
        
{
            
get
            
{
                
return timer.Interval / 20;
            }

            
set
            
{
                
if (value < 1 || value > 10)
                    
return;
                timer.Stop();
                timer.Interval 
= value * 20;
                timer.Start();
            }

        }

    }

}

posted on 2006-06-22 17:29  Icebird  阅读(3094)  评论(2编辑  收藏  举报