前面有朋友介绍了Timer组件的应用,发现有一些不详细的地方。为此特意上传了这段代码,希望能给各位一点启发! 其详细的介绍可阅前面一位朋友提供的文章,在此不再重复。 using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data;namespace WindowsAp { /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form { public int x1=0,y1=0; private System.Timers.Timer timer1; private System.Windows.Forms.PictureBox pictureBox1; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.Container components = null; public Form1() { // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /// <summary> /// Clean up any resources being used. /// </summary> protected override void Dispose( bool disposing ) { if( disposing ) { if (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() { System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1)); this.timer1 = new System.Timers.Timer(); this.pictureBox1 = new System.Windows.Forms.PictureBox(); ((System.ComponentModel.ISupportInitialize)(this.timer1)).BeginInit(); this.SuspendLayout(); // // timer1 // this.timer1.Enabled = true; this.timer1.SynchronizingObject = this; this.timer1.Elapsed += new System.Timers.ElapsedEventHandler(this.timer1_Elapsed); // // pictureBox1 // this.pictureBox1.BackgroundImage = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.BackgroundImage"))); this.pictureBox1.Image = ((System.Drawing.Bitmap)(resources.GetObject("pictureBox1.Image"))); this.pictureBox1.Name = "pictureBox1"; this.pictureBox1.Size = new System.Drawing.Size(144, 128); this.pictureBox1.TabIndex = 0; this.pictureBox1.TabStop = false; // // Form1 // this.AutoScaleBaseSize = new System.Drawing.Size(5, 13); this.ClientSize = new System.Drawing.Size(142, 123); this.Controls.AddRange(new System.Windows.Forms.Control[] { this.pictureBox1}); this.MaximizeBox = false; this.Name = "Form1"; this.Text = "Form1"; this.TopMost = true; this.Load += new System.EventHandler(this.Form1_Load); ((System.ComponentModel.ISupportInitialize)(this.timer1)).EndInit(); this.ResumeLayout(false); } #endregion /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.Run(new Form1()); } private void Form1_Load(object sender, System.EventArgs e) { this.timer1.Enabled =true; this.timer1.Interval =100; }
private void timer1_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { x1+=1; y1+=1; if (x1+10>=700) x1=0; if(y1+10>=500) y1=0; Point newxy=new Point ( x1,y1 ); this.DesktopLocation =newxy; } private void label1_Click(object sender, System.EventArgs e) { } } }
|