利用Winform实现文字滚动(仅供参考)

本人水平有限,如有写得不对的地方,望指正。为了简单化,做了一个简陋版的滚动控件。本文的内容仅供参考

测试环境:

visual studio 2017 

.net framework 4.0

原理非常简单:

1  先自定义一个继承UserControl的控件,再放置一个Label控件,加一个定时器定时更新Label的x轴的位置即可。

2  滚动原理:Label的位置是相对自定义控件的位置的。Label的位置在最右边时,位置的坐标为(自定义控件的Width,Label的y值);Label的位置在最左边时,位置坐标为(0,Label的y值),所以Label在X方向上的值只能在0到Width之间变化。

效果图如下:

目前只支持文本从左往右滚动或者从右到左滚动

步骤如下:

1  新建名为WinformDemo1的winform项目,.net framework选择4.0

2  新建名为TextScrollControl的类,并编辑如下:

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

namespace WinformDemo1
{
    public class TextScrollControl: System.Windows.Forms.UserControl
    {
        private System.Windows.Forms.Label label1;
        private string _labelText;
        private int _speed;
        private Timer timer = null;
        private int _speedParam = 1000;
        private int _maxSpeedParam = 100;
        private int _minSpeedParam = 1;
        private int _normalSpeed = 13;
        private DirectionEnum _direction;
        private int _gap = 5;

        #region 自定义属性
        [Browsable(true), Category("自定义属性"), Description("滚动文本设置")]
        public string LabelText { set { _labelText = value; Invalidate(); } get { return _labelText; } }
        [Browsable(true), Category("自定义属性"), Description("滚动速度设置,最大值为100,最少值为1")]
        public int Speed { set { _speed = value; Invalidate(); } get { return _speed; } }
        [Browsable(true), Category("自定义属性"), Description("文字滚动的方向")]
        public DirectionEnum Direction { set { _direction = value; Invalidate(); } get { return _direction; } }

        public enum DirectionEnum
        { 
            RightToLeft,
            LeftToRight
        }
        #endregion
        public TextScrollControl()
        {
            InitializeComponent();
            this.BorderStyle = BorderStyle.FixedSingle;
            this.label1.Location = new Point(this.Width, this.label1.Location.Y);
            timer = new Timer();
            Speed = _normalSpeed;
            timer.Interval = _speedParam/ Speed;
            timer.Tick += Timer_Tick;
            timer.Start();
            LabelText = this.label1.Text;
            Direction =DirectionEnum.RightToLeft;
        }
        private void Timer_Tick(object sender, EventArgs e)
        {
            SetControlProperty();
            SetTextPosition();
        }
        /// <summary>
        /// 设置文字的位置
        /// </summary>
        private void SetTextPosition()
        {
            if (Direction == DirectionEnum.RightToLeft)
            {
                if ((this.label1.Location.X - _gap) > 0)
                {
                    this.label1.Location = new Point(this.label1.Location.X - _gap, this.label1.Location.Y);
                }
                else
                {
                    this.label1.Location = new Point(this.Width, this.label1.Location.Y);
                }
            }
            else if (Direction == DirectionEnum.LeftToRight)
            {
                if ((this.label1.Location.X + _gap) < this.Width)
                {
                    this.label1.Location = new Point(this.label1.Location.X + _gap, this.label1.Location.Y);
                }
                else
                {
                    this.label1.Location = new Point(0, this.label1.Location.Y);
                }
            }
        }

        /// <summary>
        /// 设置控件的属性值
        /// </summary>
        private void SetControlProperty()
        {
            this.label1.Text = LabelText;
            if (Speed > _maxSpeedParam) Speed = _maxSpeedParam;
            if (Speed < _minSpeedParam) Speed = _minSpeedParam;
            timer.Interval = _speedParam / Speed;
        }


        private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(239, 10);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "label1";
            // 
            // TextRollUserControl
            // 
            this.Controls.Add(this.label1);
            this.Size = new System.Drawing.Size(283, 34);
            this.ResumeLayout(false);
            this.PerformLayout();

        }
        
    }
}

3  生成项目,在工具箱中就可以看到该自定义控件了,如下图:

把它拉进界面后,如下图:

可以设置它的属性值

Direction是滚动的方向,从右到左或者从左到右

LabelText是设置滚动的文本

Speed是滚动的速度,1到100,如果设置的值小于1,则会自动重置为1,如果设置的值大于100时,会自动重置为100

好了,本文到此结束。

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
要在C# Winform实现文字滚动效果,可以使用Label控件和Timer控件。首先,创建一个Label控件用于显示滚动文字,然后添加一个Timer控件用于控制滚动速度。在Timer的Tick事件中,通过改变Label控件的Left属性的值来实现文字滚动效果。具体的实现步骤如下: 1. 创建一个Winform项目,命名为Ex01_31,窗体默认为Form1。 2. 在窗体上添加一个Label控件用于显示消息,添加一个Button控件用于控制消息的运动,添加一个Timer控件用于控制滚动速度。 3. 在代码中,编写以下主要程序代码: ```csharp private void timer1_Tick(object sender, EventArgs e) { label1.Left -= 2; if (label1.Right < 0) { label1.Left = this.Width; } } private void button1_Click(object sender, EventArgs e) { timer1.Enabled = true; //开始滚动 } private void button2_Click(object sender, EventArgs e) { timer1.Enabled = false; //停止滚动 } ``` 这段代码中,timer1_Tick事件中的代码用于控制文字滚动速度。每次Timer触发Tick事件时,将Label控件的Left属性减去2,实现文字向左滚动的效果。当Label控件的Right属性小于0时,将Label控件的Left属性设置为窗体的宽度,使文字重新回到初始位置。button1_Click事件用于启动滚动,button2_Click事件用于停止滚动。 这样,当单击【演示】按钮时,文字就会开始滚动。单击【暂停】按钮可以停止滚动。 参考资料: \[1\] 引用\[1\] \[2\] 引用\[2\] \[3\] 引用\[3\] #### 引用[.reference_title] - *1* *2* [C# winform-窗体中的滚动字幕【案例+源码】](https://blog.csdn.net/m0_65636467/article/details/127869709)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [winform实现文字滚动](https://blog.csdn.net/qq_39569480/article/details/127571034)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zxy2847225301

测试使用

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值