用C#(Winform)的Timer控件让窗体左右飘动

来源:互联网 发布:减肥不反弹知乎 编辑:程序博客网 时间:2024/04/29 23:29

今天在公司做关于gis地图的事,做的头疼,一点都不想做了,然后研究了下Winform中的Timer控件,可以用这个控件让窗体左右浮动。

分享下代码:用了两个timer控件,和一个Label控件。

using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;namespace WindowsFormsApplication2{    public partial class Form3 : Form    {        private int t;        public Form3()        {            InitializeComponent();            Point p = new Point(0,240);            this.DesktopLocation = p;            this.timer1.Start();        }        private void timer1_Tick(object sender, EventArgs e)        {            Point p = new Point(this.DesktopLocation.X+1,this.DesktopLocation.Y);            this.DesktopLocation = p;            if(p.X==550)            {                this.timer1.Stop();                MessageBox.Show("跑的好累啊","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);                this.timer2.Start();            }        }        private void timer2_Tick(object sender, EventArgs e)        {            Point p = new Point(this.DesktopLocation.X-1,this.DesktopLocation.Y);            this.DesktopLocation = p;            if (p.X == 0)            {                t++;                this.timer2.Stop();                this.label1.Text = "劳资跑了"+t.ToString()+"圈了";                MessageBox.Show("加油,继续跑哦!","提示",MessageBoxButtons.OK,MessageBoxIcon.Warning);                this.timer1.Start();            }        }    }}


原创粉丝点击