分享

C#中Timer控件的运用实例

 秋寒月 2011-05-29

本文使用Timer控件制作了一个窗体飘动的程序。

程序设计的思路以及关键步骤的解决方法:

定义两个Timer控件,一个命名为timer1,另外一个命名为timer2。  timer1的作用是控制窗体从左往右飘动,timer2控制窗体从右往左飘动。且两个Timer控件不能同时启动。这里先设定timer1组件启动,当timer1启动后,每隔0.01秒,都会在触发的事件中给窗体的左上角的横坐标都加上"1",这时我们看到的结果是窗体从左往右不断移动,当移动到一定的位置后,timer1停止。timer2启动,每隔0.01秒,在触发定义的事件中给窗体的左上角的横坐标都减去"1",这时我们看到的结果是窗体从右往左不断移动。当移动到一定位置后,timer1启动,timer2停止,如此反覆,这样窗体也就飘动起来了。

(1)窗体的初始位置

设定窗体的初始位置,是在事件Form1_Load()中进行的。此事件是当窗体加载的时候触发的。Form有一个DesktopLocation属性,这个属性是设定窗体的左上角的二维位置。在程序中是通过Point结构变量来设定此属性的值,具体如下:

//设定窗体起初飘动的位置,位置为屏幕的坐标的(0,240)

private void Form1_Load ( object sender , System.EventArgs e )
        {
                Point p = new Point ( 0 , 240 ) ; 
                this.DesktopLocation = p ; 
        }

(2)实现窗体从左往右飘动

设定timer1的Interval值为"10",就是当timer1启动后,每隔0.01秒触发的事件是timer1_Tick(),在这个事件中编写给窗体左上角的横坐标不断加"1"的代码,就可以了,具体如下:

private void timer1_Tick(object sender, System.EventArgs e) 
        { 
                Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ; 
                this.DesktopLocation = p ; 
                if ( p.X == 550 ) 
                { 
                        timer1.Enabled = false ; 
                        timer2.Enabled = true ; 
                } 
        }

(3)实现窗体从右往左飘动

代码设计和从左往右飘动差不多,主要的区别是减"1"而不是加"1"了,具体如下:

//当窗体左上角位置的横坐标为-150时,timer2停止,timer1启动

private void timer2_Tick(object sender, System.EventArgs e) 
        {
                Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ; 
                this.DesktopLocation = p ; 
                if ( p.X == - 150 ) 
                { 
                        timer1.Enabled = true ; 
                        timer2.Enabled = false ; 
                } 
        } 

全部源代码如下:

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace WindowsApplication1
{
///<summary>
/// Form1 的摘要说明。
/// 作者: Shadow
/// 2006-8-3
///</summary>
public class Form1 : System.Windows.Forms.Form
{

private Timer timer1 ;

private Timer timer2 ;

private Label label1 ;

private Button button1 ;
private System.Windows.Forms.Button button2;

private System.ComponentModel.IContainer components ;

private string remarkStatus = "";

public Form1 ( )

{
InitializeComponent ( ) ;
}

protected override void Dispose ( bool disposing )

{

if ( disposing )

{

if ( components != null )

{

components.Dispose ( ) ;

}

}

base.Dispose( disposing ) ;

}


private void InitializeComponent ( )

{
this.components = new System.ComponentModel.Container();
System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.timer2 = new System.Windows.Forms.Timer(this.components);
this.label1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 10;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// timer2
//
this.timer2.Interval = 10;
this.timer2.Tick += new System.EventHandler(this.timer2_Tick);
//
// label1
//
this.label1.BackColor = System.Drawing.Color.Transparent;
this.label1.Font = new System.Drawing.Font("华文行楷", 21.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(134)));
this.label1.ForeColor = System.Drawing.Color.FromArgb(((System.Byte)(0)), ((System.Byte)(0)), ((System.Byte)(192)));
this.label1.Location = new System.Drawing.Point(16, 88);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(438, 39);
this.label1.TabIndex = 1;
this.label1.Text = "窗 体 动 起 来 喽!";
this.label1.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// button1
//
this.button1.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.button1.Font = new System.Drawing.Font("宋体", 10F);
this.button1.ForeColor = System.Drawing.Color.Blue;
this.button1.Location = new System.Drawing.Point(96, 24);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(96, 27);
this.button1.TabIndex = 0;
this.button1.Text = "停止飘动";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.BackColor = System.Drawing.Color.FromArgb(((System.Byte)(192)), ((System.Byte)(192)), ((System.Byte)(255)));
this.button2.Font = new System.Drawing.Font("宋体", 10F);
this.button2.ForeColor = System.Drawing.Color.Blue;
this.button2.Location = new System.Drawing.Point(368, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(96, 27);
this.button2.TabIndex = 2;
this.button2.Text = "开始飘动";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.BackColor = System.Drawing.Color.Silver;
this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
this.ClientSize = new System.Drawing.Size(488, 165);
this.Controls.Add(this.button2);
this.Controls.Add(this.label1);
this.Controls.Add(this.button1);
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.Name = "Form1";
this.Text = " Shadow用C#做的飘动的窗体!";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);

}


static void Main ( )

{

Application.Run ( new Form1 ( ) ) ;
}


private void Form1_Load ( object sender , System.EventArgs e )

{
Point p = new Point ( 0 , 240 ) ;

this.DesktopLocation = p ;

}


private void timer1_Tick(object sender, System.EventArgs e)

{
Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ;

this.DesktopLocation = p ;

if ( p.X == 550 )

{
timer1.Enabled = false ;

timer2.Enabled = true ;
}
}
private void timer2_Tick(object sender, System.EventArgs e)

{

Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ;

this.DesktopLocation = p ;

if ( p.X == - 150 )

{

timer1.Enabled = true ;

timer2.Enabled = false ;

}

}
private void button1_Click(object sender, System.EventArgs e)

{
if(timer1.Enabled==true)
remarkStatus = "timer1";
else if(timer2.Enabled == true)
remarkStatus = "timer2";

timer1.Stop ( ) ;

timer2.Stop ( ) ;

}

private void button2_Click(object sender, System.EventArgs e)
{
if(remarkStatus == "timer1")
{
timer1.Start( ) ;

timer2.Stop ( ) ;
}
else
{
timer1.Stop( ) ;

timer2.Start ( ) ;
}
}
}
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多