Mr.King's的专栏

.net疯狂学习中...... 一切从基础开始.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

让窗体飘动起来--C#中Timer组件用法

Posted on 2005-04-28 22:21  探索  阅读(656)  评论(0)    收藏  举报
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ; 
namespace floatingForm
{
    
public class Form1 : Form
    
{
        
private Timer timer1 ;
        
private Timer timer2 ;
        
private Label label1 ;
        
private Button button1 ;
        
private System.ComponentModel.IContainer components ;
        
private int i=1;
        
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 ( ) ;
            
this.timer1 = new Timer ( this.components ) ;
            
this.timer2 = new Timer ( this.components ) ;
            
this.label1 = new Label ( ) ;
            
this.button1 = new Button ( ) ;
            
this.SuspendLayout ( ) ;

            
this.timer1.Enabled = true ;
            
this.timer1.Interval = 10 ;
            
this.timer1.Tick += new System.EventHandler ( this.timer1_Tick ) ;

            
this.timer2.Enabled = false ;
            
this.timer2.Interval = 10 ;
            
this.timer2.Tick += new System.EventHandler ( this.timer2_Tick ) ;

            
this.button1.Font = new Font ( "宋体" , 10 ) ;
            
this.button1.Location = new Point ( 1 , 8 ) ;
            
this.button1.Name = "button1" ;
            
this.button1.Size = new Size ( 80 , 25 ) ;
            
this.button1.TabIndex = 0 ;
            
this.button1.Text = "停止飘动" ;
            
this.button1.Click += new System.EventHandler ( this.button1_Click ) ;

            
this.label1.Font = new Font ( "宋体" , 22F , FontStyle.Bold , GraphicsUnit.Point , ( ( System.Byte ) ( 0 ) ) ) ;
            
this.label1.Location = new Point ( 8 , 38 ) ;
            
this.label1.Name = "label1" ;
            
this.label1.Size = new Size ( 344 , 40 ) ;
            
this.label1.TabIndex = 1 ;
            
this.label1.Text = "用Visual C#做的飘动的窗体!" ;

            
this.AutoScaleBaseSize = new Size ( 5 , 13 ) ;
            
this.ClientSize = new Size ( 352 , 70 ) ;
            
this.Controls.Add (this.label1 ) ;
            
this.Controls.Add (this.button1 ) ;
            
this.Name = "Form1" ;
            
this.Text = "用Visual 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(i==1)
            
{
                timer1.Stop ( ) ;
                timer2.Stop ( ) ;
                button1.Text
="开始飘动";
                i
=0;
            }

            
else if(i==0)
            
{
                timer1.Start();
                
//timer2.Start();
                button1.Text="停止飘动";
                i
=1;
            }

            
        }

    }

}

Google