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#窗体滚动字幕的资料请关注代码网其它相关文章!
发表评论