[C# Control] 走马灯(Marquee)

本文介绍了一种基于Windows Forms的走马灯效果实现方法,通过自定义控件Marquee来展示滚动的文字信息。该控件支持多条信息轮播,并可通过管道符('|')分隔不同的消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

ExpandedBlockStart.gifContractedBlock.gif/**////////////////////////////////////////////////////////////////////////////////
None.gif//  Marquee.cs
None.gif
//  走马灯
None.gif
//
None.gif
//  @remarks For study purpose
None.gif
//
None.gif
//  @author Icebird @date 2006-06-22
ExpandedBlockStart.gifContractedBlock.gif
/**////////////////////////////////////////////////////////////////////////////////
None.gifusing System;
None.gif
using System.Collections;
None.gif
using System.ComponentModel;
None.gif
using System.Drawing;
None.gif
using System.Data;
None.gif
using System.Windows.Forms;
None.gif
using System.Runtime.InteropServices;
None.gif
None.gif
namespace ControlsEx
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// 走马灯
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Marquee : System.Windows.Forms.Control
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// Constructor
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public Marquee()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if( components != null )
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
InBlock.gif                    timer.Stop();
InBlock.gif                    timer.Dispose();
InBlock.gif                    lbl.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private Timer timer;
InBlock.gif        
private Label lbl;
InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
组件设计器生成的代码#region 组件设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器 
InBlock.gif        
/// 修改此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            components 
= new System.ComponentModel.Container();
InBlock.gif            lbl 
= new Label();
InBlock.gif            lbl.Parent 
= this;
InBlock.gif            lbl.AutoSize 
= true;
InBlock.gif            lbl.Visible 
= false;
InBlock.gif
InBlock.gif            timer 
= new Timer(components);
InBlock.gif            timer.Tick 
+=new EventHandler(timer_Tick);
InBlock.gif            Speed 
= 1;
InBlock.gif            timer.Start();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 字体
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override Font Font
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.Font;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
base.Font = value;
InBlock.gif                lbl.Font 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 显示文字,用'|'分隔信息
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public override string Text
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return base.Text;
ExpandedSubBlockEnd.gif            }

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

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

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnResize(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnResize (e);
InBlock.gif            lbl.Top 
= (ClientSize.Height - lbl.Height) / 2;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnEnabledChanged(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnEnabledChanged (e);
InBlock.gif
InBlock.gif            lbl.Visible 
= Enabled;
InBlock.gif            
if (Enabled)
InBlock.gif                timer.Start();
InBlock.gif            
else
InBlock.gif                timer.Stop();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private int offset;
InBlock.gif        
private int index;
InBlock.gif
InBlock.gif        
private void timer_Tick(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!Enabled)
InBlock.gif                
return;
InBlock.gif            offset
++;
InBlock.gif            
int newLeft = Width - offset;
InBlock.gif            lbl.Left 
= newLeft;
InBlock.gif            
if (lbl.Text.Length > 0)
InBlock.gif                lbl.Visible 
= true;
InBlock.gif            lbl.Invalidate();
InBlock.gif
InBlock.gif            
if (newLeft + lbl.Width < 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
//显示下一条
InBlock.gif
                offset = -2;
InBlock.gif                index
++;
InBlock.gif                
if (index >= infos.Length)
InBlock.gif                    index 
= 0;
InBlock.gif                
if (Width > 4)
InBlock.gif                    lbl.Left 
= Width;
InBlock.gif                lbl.Text 
= infos[index];
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private string[] infos;
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 滚动速度,范围1~10,1最快,10最慢
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        public int Speed
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return timer.Interval / 20;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (value < 1 || value > 10)
InBlock.gif                    
return;
InBlock.gif                timer.Stop();
InBlock.gif                timer.Interval 
= value * 20;
InBlock.gif                timer.Start();
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值