private static void CloseForm(AnimateWaitForm animateImage, Form form) { //停止动画 //lock (animateImage.image) //{ // if (animateImage != null) // { // animateImage.Stop(); // animateImage.DisposeImage(); // } //} try { if (animateImage != null) { animateImage.Stop(); animateImage.DisposeImage(); } } catch (Exception) { } lock (new object()) { //关闭窗体 //if (form != null && form.Created && form.IsHandleCreated && !form.IsDisposed) if (form != null) { try { form.Invoke((EventHandler) delegate { form.Close(); form.Dispose(); }); } catch (Exception) { } } } }
/// <summary> /// 注意鼠标会穿透,需要父窗体拦截点击事件 /// <summary> public static void AnimatingWait(WaitAction waitAct, Control parent, GifType type = GifType.Default, bool isInMainThread = false, bool isSame = true, string content = "") { // 避免1秒内重复触发 if (isSame && (DateTime.Now.Ticks - CurrentTimeTick) < 10000000) { return; } CurrentTimeTick = DateTime.Now.Ticks; WaitForm form = null; AnimateWaitForm animateImage = null; bool isFinish = false; //Gif动画展示 Thread drawThread = new Thread(() => { form = new WaitForm(parent, isInMainThread); Point drawPoint; Image _image; switch (type) { case GifType.Default: _image = FishyuSelfControl.Properties.Resources.Spinner; break; case GifType.Reload: _image = FishyuSelfControl.Properties.Resources.Reload; break; case GifType.LongSpin: _image = FishyuSelfControl.Properties.Resources.LongSpin; break; case GifType.OriginRotation: _image = FishyuSelfControl.Properties.Resources.OriginRotation; break; case GifType.OriginSizeRotation: _image = FishyuSelfControl.Properties.Resources.OriginSizeRotation; break; case GifType.StripLoading: _image = FishyuSelfControl.Properties.Resources.StripLoading; break; default: _image = FishyuSelfControl.Properties.Resources.Reload; break; } animateImage = new AnimateWaitForm(_image); drawPoint = new Point((form.Width - _image.Width) / 2, (form.Height - _image.Height) / 2); form.Paint += ((obj, e) => { Graphics g = e.Graphics; g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.PixelOffsetMode = PixelOffsetMode.Default; //高像素偏移质量 g.CompositingQuality = CompositingQuality.HighQuality; //InterpolationMode不能使用High或者HighQualityBicubic,如果是灰色或者部分浅色的图像是会在边缘处出一白色透明的线 //用HighQualityBilinear却会使图片比其他两种模式模糊(需要肉眼仔细对比才可以看出) g.InterpolationMode = InterpolationMode.Default; g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; lock (animateImage.image) { try { g.DrawImage(animateImage.Image, drawPoint); } catch (Exception) { } //g.DrawString() } //try //{ // g.DrawImage(animateImage.Image, drawPoint); //} //catch (Exception) //{ //} }); animateImage.Location = form.Location; animateImage.Rect = new Rectangle(animateImage.Location, new Size(_image.Width, _image.Height)); animateImage.OnFrameChanged = ((image, evg) => { try { form.Invalidate(new Rectangle(drawPoint, new Size(_image.Width, _image.Height))); } catch (Exception) { } //form.Invalidate(); }); //lock (_obj) //{ //} if (form != null && !form.IsDisposed && !isFinish) { animateImage.Play(); form.Focus(); form.ShowDialog(); //isOnShow = true; } }); drawThread.IsBackground = true; drawThread.Start(); //如果选择主线程阻塞 if (isInMainThread) { waitAct(); isFinish = true; CloseForm(animateImage, form); } else { Thread thread = new Thread(() => { waitAct(); isFinish = true; CloseForm(animateImage, form); }); thread.IsBackground = true; thread.Start(); } }