C#在窗體中設(shè)計(jì)滾動(dòng)字幕的方法
1.涉及到的知識(shí)點(diǎn)
本實(shí)例主要是通過Timer組件控制Label控件的移動(dòng)來實(shí)現(xiàn)的,而Label控件的移動(dòng)主要是通過設(shè)置其與窗體左邊距的相對(duì)位置來實(shí)現(xiàn)的。
(1)Timer組件
Timer計(jì)時(shí)器可以按用戶定義的時(shí)間間隔來引發(fā)事件,引發(fā)的事件一般為周期性的,每隔若干秒或若干毫秒執(zhí)行一次,其Interval屬性用來獲取或設(shè)置在相對(duì)于上一次發(fā)生的Tick事件引發(fā)Tick事件之前的時(shí)間(以毫秒為單位)。Interval屬性的語法格式如下:
public int Interval {get;set;}
參數(shù)說明
屬性值:指定在相對(duì)于上一次發(fā)生的Tick事件引發(fā)Tick事件之前的毫秒數(shù),該值不能小于1。Timer組件的Enabled屬性用來獲取或設(shè)置計(jì)時(shí)器是否正在運(yùn)行。語法格式如下:
public virtual bool Enabled {get;set;}
參數(shù)說明
屬性值:如果計(jì)時(shí)器當(dāng)前處于啟用狀態(tài),則為true;否則為false。默認(rèn)為false。(2)Label控件的Left屬性
該屬性用來獲取或設(shè)置控件左邊緣與其容器的工作區(qū)左邊緣之間的距離(以像素為單位)。語法格式如下:
[BrowsableAttribute(false)]
public int Left{get;set;}
參數(shù)說明
屬性值:表示控件左邊緣與其容器的工作區(qū)左邊緣之間的距離(以像素為單位)。Left屬性的值等效于Label控件的Location屬性值的Point.X屬性。
(3)啟動(dòng)和關(guān)閉Timer計(jì)時(shí)器
啟動(dòng)Timer計(jì)時(shí)器時(shí),可以將其Enabled屬性設(shè)置為true,或者調(diào)用其Start方法;
而關(guān)閉Timer計(jì)時(shí)器時(shí),則需要將其Enabled屬性設(shè)置為false,或者調(diào)用其Stop方法。
2. 實(shí)例
本實(shí)例實(shí)現(xiàn)了一個(gè)具有滾動(dòng)字幕效果的窗體,運(yùn)行本實(shí)例,單擊“演示”按鈕,將看到窗口中的文字開始滾動(dòng);單擊“暫停”按鈕,可以使字幕停止?jié)L動(dòng)。
(1)Resources.Designer.cs設(shè)計(jì)
//------------------------------------------------------------------------------
// <auto-generated>
// 此代碼由工具生成。
// 運(yùn)行時(shí)版本:4.0.30319.42000
//
// 對(duì)此文件的更改可能會(huì)導(dǎo)致不正確的行為,并且如果
// 重新生成代碼,這些更改將會(huì)丟失。
// </auto-generated>
//------------------------------------------------------------------------------
namespace _188.Properties {
using System;
/// <summary>
/// 一個(gè)強(qiáng)類型的資源類,用于查找本地化的字符串等。
/// </summary>
// 此類是由 StronglyTypedResourceBuilder
// 類通過類似于 ResGen 或 Visual Studio 的工具自動(dòng)生成的。
// 若要添加或移除成員,請(qǐng)編輯 .ResX 文件,然后重新運(yùn)行 ResGen
// (以 /str 作為命令選項(xiàng)),或重新生成 VS 項(xiàng)目。
[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 實(shí)例。
/// </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>
/// 重寫當(dāng)前線程的 CurrentUICulture 屬性,對(duì)
/// 使用此強(qiáng)類型資源類的所有資源查找執(zhí)行重寫。
/// </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設(shè)計(jì)
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 = "關(guān)閉";
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 = "窗體中滾動(dòng)字幕";
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設(shè)計(jì)
namespace _188
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 開始滾動(dòng)
/// </summary>
private void Button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
}
/// <summary>
/// 停止?jié)L動(dòng)
/// </summary>
private void Button2_Click(object sender, EventArgs e)
{
timer1.Enabled = false;
}
/// <summary>
/// 關(guān)閉窗體
/// </summary>
private void Button3_Click(object sender, EventArgs e)
{
Close();
}
/// <summary>
/// 用Timer來控制滾動(dòng)速度
/// </summary>
private void Timer1_Tick(object sender, EventArgs e)
{
label1.Left -= 2; //設(shè)置label1左邊緣與其容器的工作區(qū)左邊緣之間的距離
if (label1.Right < 0) //當(dāng)label1右邊緣與其容器的工作區(qū)左邊緣之間的距離小于0時(shí)
{
label1.Left = Width; //設(shè)置label1左邊緣與其容器的工作區(qū)左邊緣之間的距離為該窗體的寬度
}
}
}
}(4) 生成效果


以上就是C#在窗體中設(shè)計(jì)滾動(dòng)字幕的方法的詳細(xì)內(nèi)容,更多關(guān)于C#窗體滾動(dòng)字幕的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
unity實(shí)現(xiàn)多點(diǎn)觸控代碼
這篇文章主要介紹了unity實(shí)現(xiàn)多點(diǎn)觸控代碼,我最近在學(xué)習(xí)Unity游戲引擎。先從Unity平面開始,本章介紹Unity 平面上的多點(diǎn)觸摸。有需要的小伙伴參考下。2015-03-03
詳解C# parallel中并行計(jì)算的四種寫法總結(jié)
在C#中,parallel關(guān)鍵字可以用于并行計(jì)算。本文為大家總結(jié)了四種C# parallel并行計(jì)算的方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2022-11-11
C#事務(wù)處理(Execute Transaction)實(shí)例解析
這篇文章主要介紹了C#事務(wù)處理(Execute Transaction)實(shí)例解析,對(duì)于理解和學(xué)習(xí)事務(wù)處理有一定的幫助,需要的朋友可以參考下2014-08-08
C#連接藍(lán)牙設(shè)備的實(shí)現(xiàn)示例
本文主要介紹了C#連接藍(lán)牙設(shè)備的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01

