WinForm中使用Lable实现跑马灯的效果

1.首先看效果 

 开始制作

在窗体工具箱中拉入一个Lable,点击控件,选择属性重命名为 HelpText

属性

代码实现 

首先打开主窗体,按F7或者右建选择查看代码

1.在构造器(构造函数)外面,建立全局变量

public string text = "这里要替换的文字";
        PointF p;
        Font f = new Font("宋体", 10);
        Color c = Color.FromArgb(237,232,236);//这里RGP值是改变背景,可以根据需要更改
        string temp;

2.在窗体构造器里面输入以下代码 ,如果出现报错,引用对应的命名空间就好了。

               //TextForm是我的窗体名称,这里是无参构造
         public TextForm()
           {
            InitializeComponent();
            Timer HelpTime = new Timer();//实例化一个时间控件
            HelpTime.Enabled = true;//让时间控件可用
            HelpTime.Interval = 150;//时间间隔150毫秒
            p = new PointF(this.HelpText.Size.Width, 0);
            HelpTime.Tick+=new EventHandler(HelpTime_Tick);//注册时间控件的Tick事件
              }

3.建立事件, 就大功告成了。

private void HelpTime_Tick(object sender, EventArgs e)
        {
            Graphics g = this.HelpText.CreateGraphics();
            SizeF s = new SizeF();
            s = g.MeasureString(text, f);//测量文字长度
            Brush brush = Brushes.Blue;
            g.Clear(c);//清除背景
            if (temp != text)//文字改变时,重新显示
            {
                p = new PointF(this.HelpText.Size.Width, 0);
                temp = text;
            }
            else
                p = new PointF(p.X - 10, 0);//每次偏移10
            if (p.X <= -s.Width)
                p = new PointF(this.HelpText.Size.Width, 0);
            g.DrawString(text, f, brush, p);
        }

出自:https://blog.csdn.net/krenyelang/article/details/6795769 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是实现WinForm跑马灯效果的方法: 1. 使用Timer控件和Label控件实现跑马灯效果: ```csharp using System; using System.Windows.Forms; namespace WindowsFormsApp { public partial class Form1 : Form { private Timer timer; private Label label; public Form1() { InitializeComponent(); InitializeTimer(); InitializeLabel(); } private void InitializeTimer() { timer = new Timer(); timer.Interval = 100; // 设置定时器间隔时间,单位为毫秒 timer.Tick += Timer_Tick; timer.Start(); } private void InitializeLabel() { label = new Label(); label.Text = "这是一个跑马灯效果的Label"; label.Location = new System.Drawing.Point(10, 10); label.AutoSize = true; Controls.Add(label); } private void Timer_Tick(object sender, EventArgs e) { // 每次定时器触发时,将Label的位置向左移动一定距离 label.Left -= 5; // 当Label完全移出窗口时,将其重新放置到窗口右侧 if (label.Right < 0) { label.Left = Width; } } } } ``` 2. 使用PictureBox控件和Timer控件实现跑马灯效果: ```csharp using System; using System.Windows.Forms; namespace WindowsFormsApp { public partial class Form1 : Form { private Timer timer; private PictureBox pictureBox; public Form1() { InitializeComponent(); InitializeTimer(); InitializePictureBox(); } private void InitializeTimer() { timer = new Timer(); timer.Interval = 100; // 设置定时器间隔时间,单位为毫秒 timer.Tick += Timer_Tick; timer.Start(); } private void InitializePictureBox() { pictureBox = new PictureBox(); pictureBox.Image = Properties.Resources.marquee_image; // 设置跑马灯图片 pictureBox.SizeMode = PictureBoxSizeMode.AutoSize; pictureBox.Location = new System.Drawing.Point(10, 10); Controls.Add(pictureBox); } private void Timer_Tick(object sender, EventArgs e) { // 每次定时器触发时,将PictureBox的位置向左移动一定距离 pictureBox.Left -= 5; // 当PictureBox完全移出窗口时,将其重新放置到窗口右侧 if (pictureBox.Right < 0) { pictureBox.Left = Width; } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值