using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Threading; namespace WindowsApplication3 { public partial class ThisShow : Form { public static BackgroundWorker worker; public ThisShow() { if (worker == null) { worker = new BackgroundWorker(); } InitializeComponent(); worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; //正式做事情的地方 worker.DoWork += new DoWorkEventHandler(DoWork); //任务完称时要做的,比如提示等等 worker.ProgressChanged += new ProgressChangedEventHandler(ProgessChanged); //任务进行时,报告进度 worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(CompleteWork); } //调用 RunWorkerAsync 时发生 public void DoWork(object sender, DoWorkEventArgs e) { e.Result = SetFromPosition(worker, e); //获取异步操作结果的值,当ComputeFibonacci(worker, e)返回时,异步过程结束 } //调用 ReportProgress 时发生 public void ProgessChanged(object sender, ProgressChangedEventArgs e) { this.Location = (Point)e.UserState; //this.progressBar1.Value = e.ProgressPercentage; //将异步任务进度的百分比赋给进度条 } //当后台操作已完成、被取消或引发异常时发生 public void CompleteWork(object sender, RunWorkerCompletedEventArgs e) { this.Close(); } private int SetFromPosition(object sender, DoWorkEventArgs e) { //判断应用程序是否取消后台操作 if (worker.CancellationPending) { e.Cancel = true; } else { worker.ReportProgress(0, new Point(Screen.PrimaryScreen.Bounds.Width - this.Width, Screen.PrimaryScreen.Bounds.Height)); Thread.Sleep(10); for (int i = 0; i <= this.Height + 1; i++) { worker.ReportProgress(0 , new Point(this.Location.X, this.Location.Y - 1)); Thread.Sleep(5); } Thread.Sleep(5000); for (int i = 0; i <= this.Height + 1; i++) { worker.ReportProgress(0, new Point(this.Location.X, this.Location.Y + 1)); Thread.Sleep(5); } this.Close(); } return -1; } public void Start() { if (!worker.IsBusy) { this.WindowState = FormWindowState.Normal; worker.RunWorkerAsync(); this.Show(); } } public void Stop() { if (worker.IsBusy) { worker.CancelAsync(); } } private void ThisShow_Load(object sender, EventArgs e) { } internal void SetFromPosition() { this.Location = new System.Drawing.Point(Screen.PrimaryScreen.Bounds.Width - this.Width, Screen.PrimaryScreen.Bounds.Height); for (int i = 0; i < this.Height + 30; i++) { this.Location = new Point(this.Location.X, this.Location.Y - 1); Thread.Sleep(5); } for (int i = 0; i < this.Height + 30; i++) { this.Location = new Point(this.Location.X, this.Location.Y + 1); Thread.Sleep(5); } this.Close(); } private void ThisShow_FormClosed(object sender, FormClosedEventArgs e) { Stop(); } } }
private void button3_Click(object sender, EventArgs e) { ThisShow fm = new ThisShow(); fm.Start(); }