public partial class FrmLoading : Form
{
private static Image m_Image = null;
private EventHandler evtHandler = null;
private ParameterizedThreadStart workAction = null;
private object workActionArg = null;
private Thread workThread = null;
private TableLayoutPanel tableLayoutPanel1;
public Label lbMessage;
private Panel panImage;
public string Message
{
get
{
return lbMessage.Text;
}
set
{
lbMessage.Text = value;
}
}
public bool WorkCompleted = false;
public Exception WorkException
{ get; private set; }
public void SetWorkAction(ParameterizedThreadStart workAction, object arg)
{
this.workAction = workAction;
this.workActionArg = arg;
}
public FrmLoading(string msg)
{
InitializeComponent();
this.Message = msg;
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
if (m_Image != null)
{
//获得当前gif动画下一步要渲染的帧。
UpdateImage();
//将获得的当前gif动画需要渲染的帧显示在界面上的某个位置。
int x = (int)(panImage.ClientRectangle.Width - m_Image.Width) / 2;
int y = 0;
//e.Graphics.DrawImage(m_Image, new Rectangle(x, y, m_Image.Width, m_Image.Height));
panImage.CreateGraphics().DrawImage(m_Image, new Rectangle(x, y, m_Image.Width, m_Image.Height));
}
if (this.WorkCompleted)
{
this.Close();
}
}
private void FrmProcessing_Load(object sender, EventArgs e)
{
if (this.Owner != null)
{
this.StartPosition = FormStartPosition.Manual;
this.Location = new Point(this.Owner.Left, this.Owner.Top);
//MessageBox.Show(string.Format("X={0},Y={1}", this.Owner.Left, this.Owner.Top));
this.Width = this.Owner.Width;
this.Height = this.Owner.Height;
}
else
{
Rectangle screenRect = Screen.PrimaryScreen.WorkingArea;
this.Location = new Point((screenRect.Width - this.Width) / 2, (screenRect.Height - this.Height) / 2);
}
//为委托关联一个处理方法
evtHandler = new EventHandler(OnImageAnimate);
if (m_Image == null)
{
Assembly assy = Assembly.GetExecutingAssembly();
//获取要加载的gif动画文件
FileStream fs = File.OpenRead(Application.StartupPath + "//image//cc.jpg"); //OpenRead
int filelength = 0;
filelength = (int)fs.Length; //获得文件长度
Byte[] image = new Byte[filelength]; //建立一个字节数组
fs.Read(image, 0, filelength); //按字节流读取
m_Image = Image.FromStream(fs);
}
//调用开始动画方法
BeginAnimate();
}
//开始动画方法
private void BeginAnimate()
{
if (m_Image != null)
{
//当gif动画每隔一定时间后,都会变换一帧,那么就会触发一事件,该方法就是将当前image每变换一帧时,都会调用当前这个委托所关联的方法。
ImageAnimator.Animate(m_Image, evtHandler);
}
}
//委托所关联的方法
private void OnImageAnimate(Object sender, EventArgs e)
{
//该方法中,只是使得当前这个winform重绘,然后去调用该winform的OnPaint()方法进行重绘)
this.Invalidate();
}
//获得当前gif动画的下一步需要渲染的帧,当下一步任何对当前gif动画的操作都是对该帧进行操作)
private void UpdateImage()
{
ImageAnimator.UpdateFrames(m_Image);
}
//关闭显示动画,该方法可以在winform关闭时,或者某个按钮的触发事件中进行调用,以停止渲染当前gif动画。
private void StopAnimate()
{
m_Image = null;
ImageAnimator.StopAnimate(m_Image, evtHandler);
}
private void FrmProcessing_Shown(object sender, EventArgs e)
{
if (this.workAction != null)
{
workThread = new Thread(ExecWorkAction);
workThread.IsBackground = true;
workThread.Start();
}
}
public void ExecWorkAction()
{
try
{
var workTask = new Task((arg) =>
{
this.workAction(arg);
},
this.workActionArg);
workTask.Start();
Task.WaitAll(workTask);
}
catch (Exception ex)
{
this.WorkException = ex;
}
finally
{
this.WorkCompleted = true;
}
}
private void InitializeComponent()
{
this.tableLayoutPanel1 = new System.Windows.Forms.TableLayoutPanel();
this.panImage = new System.Windows.Forms.Panel();
this.lbMessage = new System.Windows.Forms.Label();
this.tableLayoutPanel1.SuspendLayout();
this.SuspendLayout();
//
// tableLayoutPanel1
//
this.tableLayoutPanel1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.tableLayoutPanel1.ColumnCount = 1;
this.tableLayoutPanel1.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Controls.Add(this.panImage, 0, 1);
this.tableLayoutPanel1.Controls.Add(this.lbMessage, 0, 0);
this.tableLayoutPanel1.Location = new System.Drawing.Point(0, 0);
this.tableLayoutPanel1.Name = "tableLayoutPanel1";
this.tableLayoutPanel1.RowCount = 2;
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
this.tableLayoutPanel1.Size = new System.Drawing.Size(1290, 812);
this.tableLayoutPanel1.TabIndex = 0;
//
// panImage
//
this.panImage.Dock = System.Windows.Forms.DockStyle.Fill;
this.panImage.Location = new System.Drawing.Point(3, 409);
this.panImage.Name = "panImage";
this.panImage.Size = new System.Drawing.Size(1284, 400);
this.panImage.TabIndex = 1;
//
// lbMessage
//
this.lbMessage.AutoSize = true;
this.lbMessage.Font = new System.Drawing.Font("宋体", 12F);
this.lbMessage.ForeColor = System.Drawing.Color.Black;
this.lbMessage.Location = new System.Drawing.Point(590, 350);
this.lbMessage.Margin = new System.Windows.Forms.Padding(590, 350, 3, 0);
this.lbMessage.Name = "lbMessage";
this.lbMessage.Size = new System.Drawing.Size(56, 16);
this.lbMessage.TabIndex = 0;
this.lbMessage.Text = "label1";
this.lbMessage.Visible = false;
this.ClientSize = new System.Drawing.Size(1290, 812);
this.Controls.Add(this.tableLayoutPanel1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Name = "FrmProcessing";
this.Opacity = 0.3D;//透明级别
this.ShowInTaskbar = false;
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Load += new System.EventHandler(this.FrmProcessing_Load);
this.Shown += new System.EventHandler(this.FrmProcessing_Shown);
this.tableLayoutPanel1.ResumeLayout(false);
this.tableLayoutPanel1.PerformLayout();
this.ResumeLayout(false);
}
//定义引用方法
public static void ShowLoading(string msg, UserControl owner, ParameterizedThreadStart work, object workArg = null)
{
FrmLoading processingForm = new FrmLoading(msg);
dynamic expObj = new ExpandoObject();
expObj.Form = processingForm;
expObj.WorkArg = workArg;
processingForm.SetWorkAction(work, expObj);
processingForm.ShowDialog(owner);
if (processingForm.WorkException != null)
{
throw processingForm.WorkException;
}
}
调用方式
ShowLoading("", this, (obj) =>
{
//执行操作
}, null);