FrmProcessing.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.Reflection;
  10. using System.Threading;
  11. using System.Threading.Tasks;
  12. namespace WCS_Client
  13. {
  14. public partial class FrmProcessing : Form
  15. {
  16. private static Image m_Image = null;
  17. private EventHandler evtHandler = null;
  18. private ParameterizedThreadStart workAction = null;
  19. private object workActionArg = null;
  20. private Thread workThread = null;
  21. public string Message
  22. {
  23. get
  24. {
  25. return lbMessage.Text;
  26. }
  27. set
  28. {
  29. lbMessage.Text = value;
  30. }
  31. }
  32. public bool WorkCompleted = false;
  33. public Exception WorkException
  34. { get; private set; }
  35. public void SetWorkAction(ParameterizedThreadStart workAction, object arg)
  36. {
  37. this.workAction = workAction;
  38. this.workActionArg = arg;
  39. }
  40. public FrmProcessing(string msg)
  41. {
  42. InitializeComponent();
  43. this.Message = msg;
  44. }
  45. protected override void OnPaint(PaintEventArgs e)
  46. {
  47. base.OnPaint(e);
  48. if (m_Image != null)
  49. {
  50. //获得当前gif动画下一步要渲染的帧。
  51. UpdateImage();
  52. //将获得的当前gif动画需要渲染的帧显示在界面上的某个位置。
  53. int x = (int)(panImage.ClientRectangle.Width - m_Image.Width) / 2;
  54. int y = 0;
  55. //e.Graphics.DrawImage(m_Image, new Rectangle(x, y, m_Image.Width, m_Image.Height));
  56. panImage.CreateGraphics().DrawImage(m_Image, new Rectangle(x, y, m_Image.Width, m_Image.Height));
  57. }
  58. if (this.WorkCompleted)
  59. {
  60. this.Close();
  61. }
  62. }
  63. private void FrmProcessing_Load(object sender, EventArgs e)
  64. {
  65. if (this.Owner != null)
  66. {
  67. this.StartPosition = FormStartPosition.Manual;
  68. this.Location = new Point(this.Owner.Left, this.Owner.Top);
  69. //MessageBox.Show(string.Format("X={0},Y={1}", this.Owner.Left, this.Owner.Top));
  70. this.Width = this.Owner.Width;
  71. this.Height = this.Owner.Height;
  72. }
  73. else
  74. {
  75. Rectangle screenRect = Screen.PrimaryScreen.WorkingArea;
  76. this.Location = new Point((screenRect.Width - this.Width) / 2, (screenRect.Height - this.Height) / 2);
  77. }
  78. //为委托关联一个处理方法
  79. evtHandler = new EventHandler(OnImageAnimate);
  80. if (m_Image == null)
  81. {
  82. Assembly assy = Assembly.GetExecutingAssembly();
  83. //获取要加载的gif动画文件
  84. // m_Image = Image.FromStream(assy.GetManifestResourceStream(assy.GetName().Name + ".Resources.loding.gif"));
  85. m_Image = global::WCS_Client.Properties.Resources.loading;
  86. }
  87. //调用开始动画方法
  88. BeginAnimate();
  89. }
  90. //开始动画方法
  91. private void BeginAnimate()
  92. {
  93. if (m_Image != null)
  94. {
  95. //当gif动画每隔一定时间后,都会变换一帧,那么就会触发一事件,该方法就是将当前image每变换一帧时,都会调用当前这个委托所关联的方法。
  96. ImageAnimator.Animate(m_Image, evtHandler);
  97. }
  98. }
  99. //委托所关联的方法
  100. private void OnImageAnimate(Object sender, EventArgs e)
  101. {
  102. //该方法中,只是使得当前这个winform重绘,然后去调用该winform的OnPaint()方法进行重绘)
  103. this.Invalidate();
  104. }
  105. //获得当前gif动画的下一步需要渲染的帧,当下一步任何对当前gif动画的操作都是对该帧进行操作)
  106. private void UpdateImage()
  107. {
  108. ImageAnimator.UpdateFrames(m_Image);
  109. }
  110. //关闭显示动画,该方法可以在winform关闭时,或者某个按钮的触发事件中进行调用,以停止渲染当前gif动画。
  111. private void StopAnimate()
  112. {
  113. m_Image = null;
  114. ImageAnimator.StopAnimate(m_Image, evtHandler);
  115. }
  116. private void FrmProcessing_Shown(object sender, EventArgs e)
  117. {
  118. if (this.workAction != null)
  119. {
  120. workThread = new Thread(ExecWorkAction);
  121. workThread.IsBackground = true;
  122. workThread.Start();
  123. }
  124. }
  125. private void ExecWorkAction()
  126. {
  127. try
  128. {
  129. var workTask = new Task((arg) =>
  130. {
  131. this.workAction(arg);
  132. },
  133. this.workActionArg);
  134. workTask.Start();
  135. Task.WaitAll(workTask);
  136. }
  137. catch (Exception ex)
  138. {
  139. this.WorkException = ex;
  140. }
  141. finally
  142. {
  143. //this.Close();
  144. this.WorkCompleted = true;
  145. }
  146. }
  147. }
  148. }