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 ; public Form1 ( ) { file://初始化窗体中的各个组件 InitializeComponent ( ) ; } file://清除在程序中使用过的资源 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 ( ) ) ; } file://设定窗体起初飘动的位置 private void Form1_Load ( object sender , System.EventArgs e ) { Point p = new Point ( 0 , 240 ) ; this.DesktopLocation = p ; } file://当窗体左上角位置的横坐标为550时,timer1停止,timer2启动 private void timer1_Tick(object sender, System.EventArgs e) { file://窗体的左上角横坐标随着timer1不断加一 Point p = new Point ( this.DesktopLocation.X + 1 , this.DesktopLocation.Y ) ; this.DesktopLocation = p ; if ( p.X == 550 ) { timer1.Enabled = false ; timer2.Enabled = true ; } } file://当窗体左上角位置的横坐标为-150时,timer2停止,timer1启动 private void timer2_Tick(object sender, System.EventArgs e) { file://窗体的左上角横坐标随着timer2不断减一 Point p = new Point ( this.DesktopLocation.X - 1 , this.DesktopLocation.Y ) ; this.DesktopLocation = p ; if ( p.X == - 150 ) { timer1.Enabled = true ; timer2.Enabled = false ; } } file://停止所有的timer private void button1_Click(object sender, System.EventArgs e) { timer1.Stop ( ) ; timer2.Stop ( ) ; } } } |