C#中的多媒体技术:文字处理
文字处理
在C#中可以通过Label控件、TextBox控件、窗体和PictureBox控件来显示文字。窗体和PictureBox控件主要是通过DrawString方法实现,而TextBox控件和Label控件是专门用来显示文字信息的。要想实现文字的滚动,可以通过将Label控件的Left、Top属性和Timer控件相结合,使Label控件每隔一段时间就移动一次位置,如果时间间隔合适,就可以实现Label控件中显示的文字滚动的效果。
示例
由左向右滚动的文字
本示例利用Label控件来实现文字的滚动。要实现文字由左向右滚动,网站源代码可以设置Label控件的Left属性,例如,“label1.Left = label1.Left + 50;”。
程序运行结果,如图1所示。
图1 滚动文字
程序开发步骤如下所示。
(1)创建一个项目,命名为27_01,设置默认窗体的Text属性为“滚动文字”。
(2)在窗体上添加两个Label控件,一个Timer控件,设置Timer控件的Interval属性为200。
(3)程序代码如下。
private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Left < this.Width)
{
label1.Left = label1.Left + 50;
}
else if (label1.Left > -this.Width)
{
label1.Left = - label1.Width;
}
}
完整程序代码如下:
★ ★★★★Form1.cs窗体代码文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace _7_01
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
if (label1.Left < this.Width)
{
label1.Left = label1.Left + 50;
}
else if (label1.Left > -this.Width)
{
label1.Left = - label1.Width;
}
}
}
}
★ ★★★★Form1.Designer.cs窗体设计文件完整程序代码网站源代码★★★★★
namespace _7_01
{
partial class Form1
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{//本教程来自http://www.ISSTUDY.com
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 200;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(78, 36);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(137, 12);
this.label1.TabIndex = 0;
this.label1.Text = "吉林省明日科技欢迎您!";
//
// Form1
//本教程来自http://www.ISSTUDY.com
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(315, 96);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "滚动文字";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Timer timer1;
private System.Windows.Forms.Label label1;
}
}
★ ★★★★Program.cs主程序文件完整程序代码★★★★★
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace _7_01
{
static class Program
{
///
/// 应用程序的主入口点。
///
[STAThread]