效果
点击按钮1显示窗口 窗口从右向左 缓慢水平移动出现
点击按钮2窗口从左向右 缓慢水平移动消失
用法
源代码
命名空间
using System;
using System.Threading;
using System.Windows.Forms;
using System.Drawing;
using System.ComponentModel;
代码
public class MyPanel : Panel
{
#region 窗口移动系统代码
int upmar;
int beginops;
int finalops;
string status = "NULL";
BackgroundWorker BackgroundWorker1 = new BackgroundWorker();
public void showWin()
{
if (status == "NULL")
{
status = "show";
this.BringToFront();
BackgroundWorker1.RunWorkerAsync();
}
}
public void closeWin()
{
if (status == "NULL")
{
status = "close";
this.BringToFront();
BackgroundWorker1.RunWorkerAsync();
}
}
private void BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
int kk = e.ProgressPercentage*50;
if(status == "show")
{
int x = beginops - kk;
int y = upmar;
if (x <= 0)
{
this.Location = new Point(finalops, y);
status = "NULL";
BackgroundWorker1.CancelAsync();
}
else
{
this.Location = new Point(x, y);
}
}
else if(status == "close")
{
int x = kk;
int y = upmar;
if (x > beginops)
{
this.Location = new Point(beginops, y);
status = "NULL";
BackgroundWorker1.CancelAsync();
}
else
{
this.Location = new Point(x, y);
}
}
}
private void BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker bg = sender as BackgroundWorker;
for (int i = 0; true; i++)
{
Thread.Sleep(1);
bg.ReportProgress(i);
if(bg.CancellationPending == true)
{
e.Cancel = true;
break;
}
}
}
#endregion
public MyPanel(int upMar,int beginOps,int finalOps)
{
#region 窗口移动系统代码
upmar = upMar;
beginops = beginOps;
finalops = finalOps;
this.Location = new Point(beginOps, upMar);
BackgroundWorker1.DoWork += BackgroundWorker_DoWork;
BackgroundWorker1.WorkerReportsProgress = true;
BackgroundWorker1.ProgressChanged += BackgroundWorker_ProgressChanged;
BackgroundWorker1.WorkerSupportsCancellation = true;
#endregion
this.Size = new Size(1000, 800);
this.BackColor = Color.White;
//下面是其他代码编辑区
}
}