C#在窗体中设计滚动字幕的方法

 更新时间:2024年04月21日 10:24:21   作者:wenchm  
普通窗体中的文字位置都是固定的,但在一些窗体中需要让文字动起来,如一些广告性较强的界面中需要做一些滚动的字幕,所以本文给大家介绍了C#在窗体中设计滚动字幕的方法,需要的朋友可以参考下

1.涉及到的知识点

本实例主要是通过Timer组件控制Label控件的移动来实现的,而Label控件的移动主要是通过设置其与窗体左边距的相对位置来实现的。

(1)Timer组件

Timer计时器可以按用户定义的时间间隔来引发事件,引发的事件一般为周期性的,每隔若干秒或若干毫秒执行一次,其Interval属性用来获取或设置在相对于上一次发生的Tick事件引发Tick事件之前的时间(以毫秒为单位)。Interval属性的语法格式如下:

public int Interval {get;set;}
参数说明
属性值:指定在相对于上一次发生的Tick事件引发Tick事件之前的毫秒数,该值不能小于1。

Timer组件的Enabled属性用来获取或设置计时器是否正在运行。语法格式如下:

public virtual bool Enabled {get;set;}
参数说明
属性值:如果计时器当前处于启用状态,则为true;否则为false。默认为false。

(2)Label控件的Left属性

该属性用来获取或设置控件左边缘与其容器的工作区左边缘之间的距离(以像素为单位)。语法格式如下:

[BrowsableAttribute(false)]
public int Left{get;set;}
参数说明
属性值:表示控件左边缘与其容器的工作区左边缘之间的距离(以像素为单位)。

Left属性的值等效于Label控件的Location属性值的Point.X属性。

(3)启动和关闭Timer计时器

启动Timer计时器时,可以将其Enabled属性设置为true,或者调用其Start方法;

而关闭Timer计时器时,则需要将其Enabled属性设置为false,或者调用其Stop方法。

2. 实例

本实例实现了一个具有滚动字幕效果的窗体,运行本实例,单击“演示”按钮,将看到窗口中的文字开始滚动;单击“暂停”按钮,可以使字幕停止滚动。

(1)Resources.Designer.cs设计

//------------------------------------------------------------------------------
// <auto-generated>
//     此代码由工具生成。
//     运行时版本:4.0.30319.42000
//
//     对此文件的更改可能会导致不正确的行为,并且如果
//     重新生成代码,这些更改将会丢失。
// </auto-generated>
//------------------------------------------------------------------------------
 
namespace _188.Properties {
    using System;
    
    
    /// <summary>
    ///   一个强类型的资源类,用于查找本地化的字符串等。
    /// </summary>
    // 此类是由 StronglyTypedResourceBuilder
    // 类通过类似于 ResGen 或 Visual Studio 的工具自动生成的。
    // 若要添加或移除成员,请编辑 .ResX 文件,然后重新运行 ResGen
    // (以 /str 作为命令选项),或重新生成 VS 项目。
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
    internal class Resources {
        
        private static global::System.Resources.ResourceManager resourceMan;
        
        private static global::System.Globalization.CultureInfo resourceCulture;
        
        [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")]
        internal Resources() {
        }
        
        /// <summary>
        ///   返回此类使用的缓存的 ResourceManager 实例。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Resources.ResourceManager ResourceManager {
            get {
                if (object.ReferenceEquals(resourceMan, null)) {
                    global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("_188.Properties.Resources", typeof(Resources).Assembly);
                    resourceMan = temp;
                }
                return resourceMan;
            }
        }
        
        /// <summary>
        ///   重写当前线程的 CurrentUICulture 属性,对
        ///   使用此强类型资源类的所有资源查找执行重写。
        /// </summary>
        [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)]
        internal static global::System.Globalization.CultureInfo Culture {
            get {
                return resourceCulture;
            }
            set {
                resourceCulture = value;
            }
        }
        
        /// <summary>
        ///   查找 System.Drawing.Bitmap 类型的本地化资源。
        /// </summary>
        internal static System.Drawing.Bitmap _05 {
            get {
                object obj = ResourceManager.GetObject("_05", resourceCulture);
                return ((System.Drawing.Bitmap)(obj));
            }
        }
    }
}

(2) Form1.Designer.cs设计

namespace _188
{
    partial class Form1
    {
        /// <summary>
        ///  Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;
 
        /// <summary>
        ///  Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }
 
        #region Windows Form Designer generated code
 
        /// <summary>
        ///  Required method for Designer support - do not modify
        ///  the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            components = new System.ComponentModel.Container();
            button1 = new Button();
            button2 = new Button();
            button3 = new Button();
            timer1 = new System.Windows.Forms.Timer(components);
            label1 = new Label();
            SuspendLayout();
            // 
            // button1
            // 
            button1.Location = new Point(267, 73);
            button1.Name = "button1";
            button1.Size = new Size(75, 23);
            button1.TabIndex = 0;
            button1.Text = "演示";
            button1.UseVisualStyleBackColor = true;
            button1.Click += Button1_Click;
            // 
            // button2
            // 
            button2.Location = new Point(267, 102);
            button2.Name = "button2";
            button2.Size = new Size(75, 23);
            button2.TabIndex = 1;
            button2.Text = "停止";
            button2.UseVisualStyleBackColor = true;
            button2.Click += Button2_Click;
            // 
            // button3
            // 
            button3.Location = new Point(267, 131);
            button3.Name = "button3";
            button3.Size = new Size(75, 23);
            button3.TabIndex = 2;
            button3.Text = "关闭";
            button3.UseVisualStyleBackColor = true;
            button3.Click += Button3_Click;
            // 
            // timer1
            // 
            timer1.Tick += Timer1_Tick;
            // 
            // label1
            // 
            label1.AutoSize = true;
            label1.Font = new Font("Microsoft YaHei UI", 18F);
            label1.ForeColor = Color.Red;
            label1.Location = new Point(1, 221);
            label1.Name = "label1";
            label1.Size = new Size(470, 31);
            label1.TabIndex = 3;
            label1.Text = "好消息:本店让利大酬宾,所有商品八折。";
            // 
            // Form1
            // 
            AutoScaleDimensions = new SizeF(7F, 17F);
            AutoScaleMode = AutoScaleMode.Font;
            BackgroundImage = Properties.Resources._05;
            BackgroundImageLayout = ImageLayout.Stretch;
            ClientSize = new Size(354, 267);
            Controls.Add(label1);
            Controls.Add(button3);
            Controls.Add(button2);
            Controls.Add(button1);
            Name = "Form1";
            StartPosition = FormStartPosition.CenterScreen;
            Text = "窗体中滚动字幕";
            ResumeLayout(false);
            PerformLayout();
        }
 
        #endregion
 
        private Button button1;
        private Button button2;
        private Button button3;
        private System.Windows.Forms.Timer timer1;
        private Label label1;
    }
}

(3)Form1.cs设计

namespace _188
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();  
        }
        /// <summary>
        /// 开始滚动
        /// </summary>
        private void Button1_Click(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }
        /// <summary>
        /// 停止滚动
        /// </summary>
        private void Button2_Click(object sender, EventArgs e)
        {
            timer1.Enabled = false;
        }
        /// <summary>
        /// 关闭窗体
        /// </summary>
        private void Button3_Click(object sender, EventArgs e)
        {
            Close();
        }
        /// <summary>
        /// 用Timer来控制滚动速度
        /// </summary>
        private void Timer1_Tick(object sender, EventArgs e)
        {
            label1.Left -= 2;        //设置label1左边缘与其容器的工作区左边缘之间的距离
            if (label1.Right < 0)    //当label1右边缘与其容器的工作区左边缘之间的距离小于0时
            {
                label1.Left = Width; //设置label1左边缘与其容器的工作区左边缘之间的距离为该窗体的宽度
            }
        }
    }
}

(4) 生成效果

以上就是C#在窗体中设计滚动字幕的方法的详细内容,更多关于C#窗体滚动字幕的资料请关注脚本之家其它相关文章!

相关文章

  • C# 枚举类型的声明和使用

    C# 枚举类型的声明和使用

    如果一种变量只有几种可能的值,可以定义为枚举类型。所谓“枚举类型”是将变量的值一一列举出来,变量的值只能在列举出来的值的范围内
    2021-07-07
  • C#多线程及同步示例简析

    C#多线程及同步示例简析

    这篇文章主要为大家详细介绍了C#多线程及同步示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08
  • C# 枚举的使用简介

    C# 枚举的使用简介

    这篇文章主要介绍了C# 枚举的简单使用,帮助大家更好的理解和学习使用c#,感兴趣的朋友可以了解下
    2021-03-03
  • C# 实现截图软件功能实例代码

    C# 实现截图软件功能实例代码

    这篇文章主要介绍了C# 实现截图软件功能实例代码,需要的朋友可以参考下
    2017-06-06
  • .NET6实现分布式定时任务的完整方案

    .NET6实现分布式定时任务的完整方案

    这篇文章主要为大家详细介绍了.NET6实现分布式定时任务的完整方案,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下
    2025-04-04
  • C#自定义的字符串操作增强类实例

    C#自定义的字符串操作增强类实例

    这篇文章主要介绍了C#自定义的字符串操作增强类,涉及C#操作字符串实现分割、转换、去重等常用技巧,非常具有实用价值,需要的朋友可以参考下
    2015-03-03
  • C# using()的使用方法

    C# using()的使用方法

    本文主要介绍了C# using()的使用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • C#中类与接口的区别个人总结

    C#中类与接口的区别个人总结

    这篇文章主要介绍了C#中类与接口的区别个人总结,本文讲解了类与接口的区别、接口的用处主要体现在下面几个方面、一些接口的疑问等内容,需要的朋友可以参考下
    2015-06-06
  • c#可以创建任意控件的拖动方法

    c#可以创建任意控件的拖动方法

    下面小编就为大家分享一篇c#可以创建任意控件的拖动方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-03-03
  • C#借助Free Spire.PDF for .NET实现轻松提取PDF文本

    C#借助Free Spire.PDF for .NET实现轻松提取PDF文本

    在日常办公和开发中,从 PDF 文件中提取文本是一项高频需求,本文将介绍如何使用免费库 Free Spire.PDF for .NET 轻松实现 PDF 文本提取,感兴趣的小伙伴可以了解下
    2026-03-03

最新评论