做winform项目时,有可能用到异步耗时加载数据啥的,这个时候就需要我们封装一个正在加载的效果。下面是实现思路:

步骤一:把当前form界面使用句柄的方式截图出一个图片,这个图片会在下面用到,使用句柄获取截图的方式会在最后代码展示里附录上。

步骤二:定义一个和当前form一样大小的panel,让这个panel带到当前form的Z顺序的最前面,把当前界面覆盖住,这样当前form的控件就不可点击。

步骤三:把从步骤一获取的图片设置为步骤而定义的panel的背景,这样让这个panel看起来是和界面一样的。

步骤四:在panel中间定义一个新的panel放置加载Loading图片和文字。

下面是封装的代码:

 public partial class customForm : Form
{
#region properties /// <summary>
/// 显示的等待框
/// </summary>
private System.Windows.Forms.Panel waitingBox; Panel waitingBoxInnerPanel; Label waitingBoxLab; private PictureBox _waitPicBox; private bool _IsWaitingBoxCreated = false; private bool _isOnWaiting = false; #endregion public baseLogin()
{
InitializeComponent(); //设置程序图标
Icon = Icon.FromHandle(Properties.Resources.icon.GetHicon()); //Task.Delay(1000).ContinueWith((t) => { CreateWaitingBox(); });
} public void ShowProgress(string message = "", int second = )
{
if (_isOnWaiting)
{
return;
}
_isOnWaiting = true;
this.Invoke(new Action(() =>
{
CreateWaitingBox();
SetWaitingMessage(message);
waitingBox.Visible = true;
waitingBox.BringToFront();
}));
} public void ShowMessage(string message = "", int second = )
{
if (_isOnWaiting)
{
return;
}
_isOnWaiting = true;
CreateWaitingBox();
SetWaitingMessage(message);
if (IsHandleCreated)
{
this.Invoke(new Action(() =>
{
waitingBox.Visible = true;
waitingBox.BringToFront();
}));
Task.Delay(second * ).ContinueWith((t) =>
{
DisMissMessage();
});
}
} public void DisMissMessage()
{
if (waitingBox == null)
{
return;
}
if (!waitingBox.Visible)
{
return;
}
else
{
this.Invoke(new Action(() =>
{
waitingBox.Visible = false;
this._isOnWaiting = false;
}));
}
} #region private private void CreateWaitingBox()
{ if (this.IsHandleCreated)
{
this.Invoke(new Action(() =>
{
//Image backImg = this.CreateBacgroundImage();
Control frm = this;
Image backImg = CaptureImage(ref frm);
if (!_IsWaitingBoxCreated)
{
waitingBox = new Panel()
{
Visible = false,
};
waitingBox.BackColor = Color.FromArgb(, , ); waitingBoxInnerPanel = new Panel();
waitingBoxInnerPanel.Width = ;
waitingBoxInnerPanel.Height = ;
waitingBoxInnerPanel.BackColor = Color.Gray;
waitingBoxInnerPanel.Padding = new Padding(, , , ); waitingBoxLab = new Label();
waitingBoxLab.TextAlign = ContentAlignment.MiddleLeft;
waitingBoxLab.AutoEllipsis = true;
waitingBoxLab.Dock = DockStyle.Fill; waitingBoxInnerPanel.Controls.Add(waitingBoxLab); PictureBox pb = new PictureBox();
pb.Dock = DockStyle.Left;
pb.Size = new System.Drawing.Size(, );
pb.Image = Properties.Resources.loading;
pb.Margin = new System.Windows.Forms.Padding(, , , );
pb.SizeMode = PictureBoxSizeMode.StretchImage;
this._waitPicBox = pb;
waitingBoxInnerPanel.Controls.Add(pb); waitingBox.Controls.Add(waitingBoxInnerPanel);
//waitingBox.BringToFront();
if (!this.Controls.Contains(waitingBox))
{
this.Controls.Add(waitingBox);
}
//waitingBox.Show(); this._IsWaitingBoxCreated = true; } Rectangle rect = this.ClientRectangle;
waitingBox.Width = rect.Width;
waitingBox.Height = rect.Height;
waitingBox.Location = new Point(rect.X, rect.Y); waitingBox.BackgroundImage = backImg;
waitingBox.BackgroundImageLayout = ImageLayout.Stretch;
}));
}
} /// <summary>
/// 设置等待显示的信息
/// </summary>
/// <param name="message">The message.</param>
/// User:Ryan CreateTime:2012-8-5 16:22.
private void SetWaitingMessage(string message)
{
if (this.IsHandleCreated)
{
this.Invoke(new Action(() =>
{
message = " " + message.Trim();
if (this.waitingBoxLab != null && this.waitingBoxInnerPanel != null)
{
using (Graphics g = this.CreateGraphics())
{
int w = Convert.ToInt32(g.MeasureString(message, this.waitingBoxLab.Font).Width);
w = w >= ? w : ;
w = this.Width - >= w ? w : this.Width - ;
this.waitingBoxInnerPanel.Width = w + ;
waitingBoxInnerPanel.Location = new Point(waitingBox.Bounds.X + waitingBox.Width / - waitingBoxInnerPanel.Width / ,
waitingBox.Bounds.Y + waitingBox.Height / - waitingBoxInnerPanel.Height);
} this.waitingBoxLab.Text = message;
}
}));
}
} private Bitmap CreateBacgroundImage()
{
Rectangle rect = this.ClientRectangle;
int w = rect.Width;
int h = rect.Height;
try
{
Bitmap img = new Bitmap(w, h);
using (Graphics g = Graphics.FromImage(img))
{
g.CopyFromScreen(new Point(this.Location.X, this.Location.Y), new Point(, ), new Size(w, h));
}
//img.Save("a.jpg");
return img;
}
catch (Exception ex)
{
return null;
}
} public Bitmap CaptureImage(ref Control c)
{
int hDC;
int sh;
int sw;
if (c == null)
{
hDC = GetDC();
sw = Screen.PrimaryScreen.Bounds.Width;
sh = Screen.PrimaryScreen.Bounds.Height;
}
else
{
hDC = GetDC((int)c.Handle);
sw = c.Width;
sh = c.Height;
}
int hMDC = CreateCompatibleDC(hDC);
int hBMP = CreateCompatibleBitmap(hDC, sw, sh);
int hBMPOld = SelectObject(hMDC, hBMP);
BitBlt(hMDC, , , sw, sh, hDC, , , 0xcc0020);
hBMP = SelectObject(hMDC, hBMPOld);
Bitmap result = Image.FromHbitmap(new IntPtr(hBMP));
DeleteDC(hDC);
DeleteDC(hMDC);
DeleteObject(hBMP);
return result;
}
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleBitmap")]
public static extern int CreateCompatibleBitmap(int hdc, int nWidth, int nHeight);
[DllImport("gdi32.dll", EntryPoint = "CreateCompatibleDC")]
public static extern int CreateCompatibleDC(int hdc);
[DllImport("user32.dll", EntryPoint = "GetDC")]
public static extern int GetDC(int hwnd);
[DllImport("gdi32.dll", EntryPoint = "DeleteDC")]
public static extern int DeleteDC(int hdc);
[DllImport("gdi32.dll", EntryPoint = "SelectObject")]
public static extern int SelectObject(int hdc, int hObject);
[DllImport("gdi32.dll", EntryPoint = "DeleteObject")]
public static extern int DeleteObject(int hObject);
[DllImport("gdi32.dll", EntryPoint = "BitBlt")]
public static extern int BitBlt(int hDestDC, int x, int y, int nWidth, int nHeight, int hSrcDC, int xSrc, int ySrc, int dwRop); #endregion }

封装代码

下面是示例代码:

  public partial class Form1:customForm
{
private void button1_Click(object sender,EventArgs e)
{
ShowMessage("我是消息");
}
}

winform Loading效果的更多相关文章

  1. 页面loading效果

    当网页太大,打开太慢的时候,为了增加良好的用户体验(不让用户眼巴巴的等,心中暗骂c,这么慢),我们需要加一个等待动画. 只需把以下代码加入页面中即可,图片可以根据自己的需求更换,更换图片之后需要改变l ...

  2. 网页Loading效果

    问题描述:由于项目要求在页面提交以及加载的时候,有短暂的卡顿,需要用loading过渡. 1.下一个页面加载的时候实现: base-loading.js //获取浏览器页面可见高度和宽度 var _P ...

  3. 一个不错的loading效果--IT蓝豹

    一个不错的loading效果 介绍:一个不错的loading加载效果,弹性收缩,效果不错,学习android动画的朋友可以下载来研究研究本例子其实由SeekBar实现,由MetaballView,Me ...

  4. 【转】 CSS3实现10种Loading效果

    昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… PS:如需转载,请注明出处! 第1种效果: 代码如下: <div class="l ...

  5. jQuery8种不同的瀑布流懒加载loading效果

    优化图片加载插件jQuery8种不同的瀑布流懒加载loading效果  在线预览 下载地址 实例代码 <ul class="grid effect-1" id="g ...

  6. HTML5 Canvas 实现的9个 Loading 效果

    Sonic.js 是一个很小的 JavaScript 类,用于创建基于 HTML5 画布的加载图像.更强大的是 Sonic.js 还提供了基于现成的例子的创建工具,可以帮助你实现更多自定义的(Load ...

  7. 加载状态为complete时移除loading效果

    一.JS代码: //获取浏览器页面可见高度和宽度 var _PageHeight = document.documentElement.clientHeight, _PageWidth = docum ...

  8. CSS3轻松实现清新 Loading 效果

    至今HTML5中国已经为大家分享过几百种基于 CSS3 的Loading加载动画,效果酷炫代码简洁,非常值得学习借鉴;今天就先给大家分享两个常用的CSS3的Loading的案例. 第一种效果: HTM ...

  9. 一个很酷的加载loading效果--IT蓝豹

    一个很酷的加载loading效果,自定义LeafLoadingView实现,LeafLoadingView继承view, 本例子主要由以下几点构成 (1):RotateAnimation实现叶子旋转 ...

随机推荐

  1. 【Combination Sum II 】cpp

    题目: Given a collection of candidate numbers (C) and a target number (T), find all unique combination ...

  2. IOS开发学习笔记037-九宫格代码实现

    九宫格布局,用手机输入法时经常见到.先按3行3列写. 代码的实现主要是计算插入图片的位置. 每一张图片的位置和所在的行列密切相关.分析过程如下: 界面: 代码实现 1.把需要的图片资源添加进来 然后给 ...

  3. Python+Selenium练习篇之20-处理Alert弹窗

    本文来介绍如何通过Selenium方法去处理网页Alert弹窗,和处理iframe类似,都是通过switch_to方法.这里还是没有找到合适的alert弹窗网站,我们就自己创建一个吧,前面文章介绍了如 ...

  4. springmvc支持跨域的请求(复制)

    Spring MVC 新增跨域支持 发表于2017/5/8 22:01:24  48人阅读 分类: SpringMVC Spring MVC 4.2 增加 CORS 支持 跨站 HTTP 请求(Cro ...

  5. iOS-绘制UIView之drawCGRect

    写在前面 UIView对于iOS开发来讲,再熟悉不过了.也正是因为这一点,我们可能会忽略UIView一些特有方法的理解和使用.今天,笔者主要整理一下对drawRect方法的理解和使用. 默认情况下,该 ...

  6. 通过Url网络编程实现下载

    import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputS ...

  7. C# 泛型的入门理解(来自网络)

    using System.Collections; class Program { //做个比较 static void Main(string[] args) { //new对象 Cls a1 = ...

  8. mysql里制造一个错误

    最近突然想到的,由于在触发器中执行失败事务性表会自动回滚. 所以就想制造一个错误,在群里问了问最后还真得到一个制造错误的方法,或者可以叫做自定义异常 SIGNAL SQLSTATE ' SET MES ...

  9. [六省联考2017]组合数问题 (矩阵优化$dp$)

    题目链接 Solution 矩阵优化 \(dp\). 题中给出的式子的意思就是: 求 nk 个物品中选出 mod k 为 r 的个数的物品的方案数. 考虑朴素 \(dp\) ,定义状态 \(f[i][ ...

  10. 配置 L3 agent

    上一节我们介绍了路由服务(Routing)的基本功能,今天教大家如何配置. Neutron 的路由服务是由 l3 agent 提供的. 除此之外,l3 agent 通过 iptables 提供 fir ...