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#窗体滚动字幕的资料请关注脚本之家其它相关文章!

相关文章

  • 在winform下实现左右布局多窗口界面的方法之续篇

    在winform下实现左右布局多窗口界面的方法之续篇

    这篇文章主要介绍了在winform下实现左右布局多窗口界面的方法之续篇 的相关资料,需要的朋友可以参考下
    2016-02-02
  • C#堆排序实现方法

    C#堆排序实现方法

    这篇文章主要介绍了C#堆排序实现方法,实例分析了C#对排序的实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下
    2015-04-04
  • C#读取xml节点数据方法小结

    C#读取xml节点数据方法小结

    这篇文章主要介绍了C#读取xml节点数据的方法,实例总结了C#针对XML文件节点操作的相关技巧,需要的朋友可以参考下
    2015-06-06
  • C# HttpClient上传文件并附带其它参数方式

    C# HttpClient上传文件并附带其它参数方式

    这篇文章主要介绍了C# HttpClient上传文件并附带其它参数方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-11-11
  • C#函数式编程中的标准高阶函数详解

    C#函数式编程中的标准高阶函数详解

    这篇文章主要介绍了C#函数式编程中的标准高阶函数详解,本文讲解了何为高阶函数、Map、 Filter、Fold等内容,需要的朋友可以参考下
    2015-01-01
  • 详解C#如何实现分割视频

    详解C#如何实现分割视频

    这篇文章主要为大家详细介绍了C#如何实现将视频文件分割成一帧帧图片的方法,文中的示例代码讲解详细,对我们学习C#有一定的帮助,感兴趣的小伙伴可以跟随小编一起了解一下
    2022-12-12
  • Unity3D动态生成平面网格

    Unity3D动态生成平面网格

    这篇文章主要为大家详细介绍了Unity3D动态生成平面网格,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2019-02-02
  • Unity中的PostProcessBuild实用案例深入解析

    Unity中的PostProcessBuild实用案例深入解析

    这篇文章主要为大家介绍了Unity中的PostProcessBuild实用案例深入解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • C#中四种定时器的用法详解

    C#中四种定时器的用法详解

    日常项目开发中,很多时候都需要用到定时器来处理一些问题,那么c#中各种定时器应该怎么用呢?下面来简单介绍下C#中4种定时器的使用方法说明,感兴趣的朋友可以参考下
    2024-04-04
  • C#禁用双击窗体图标关闭窗体的方法

    C#禁用双击窗体图标关闭窗体的方法

    这篇文章主要介绍了C#禁用双击窗体图标关闭窗体的方法,通过对窗体参数的简单设置实现C#禁用双击窗体图标关闭窗体的功能,非常简单实用,需要的朋友可以参考下
    2015-08-08

最新评论