窗体滚动字幕

今天给大家分享一个实现滚动字幕的小程序,也是作为我学习的一种见证吧。(hh)

先来看看效果图吧。(我对界面要求不高,只要看着不吐就行)

在这里插入图片描述
在这里插入图片描述
以下是源码(需要就贴吧)

using System;
using System.Drawing;
using System.Windows.Forms;

namespace 窗体滚动字幕
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Label label_show = new Label() ;
        public PointF point { get; set; }
        public string text_Contents = "窗体滚动字幕";
        Color BackGround_Color = Color.White;
        Font text_font = new Font("黑体",50);

        private void Form1_Load(object sender, EventArgs e)
        {
            this.label_show.Location = new Point(200, 150);
            this.label_show.Size = new Size(200, 80);
            this.label_show.Text = "";
            this.Controls.Add(label_show);            
            this.timer1.Enabled = true;
            this.timer1.Interval = 500;
            point = new PointF(this.label_show .Width , 0);//初始化位置
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            Graphics graphics = this.label_show.CreateGraphics();//为标签创建绘图界面
            graphics.Clear(BackGround_Color);//清除背景色 
            SizeF sizeF = new SizeF();
            sizeF = graphics.MeasureString(text_Contents, text_font);//获取字符串长度
            Brush brush = Brushes.Red;//字体颜色
            point = new PointF(point.X -10, 0);//单次偏移量10
            if (point .X <=-sizeF.Width ) //判断条件重新循环
            {
                point = new PointF(this.label_show .Width , 0);
            }
            graphics.DrawString(text_Contents ,text_font ,brush, point);//绘制字符串
        }
    }
}

最先把显示顺序写反了(从左往右),别扭死了。。。。。

这个小程序个人觉得新手还是值得一试的,不难。

对了这个小程序用到了Timer控件,你建个窗体程序,拖这个控件,然后直接贴就行了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值