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 ( ) ; } } } |