winform Loading效果
做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效果的更多相关文章
- 页面loading效果
当网页太大,打开太慢的时候,为了增加良好的用户体验(不让用户眼巴巴的等,心中暗骂c,这么慢),我们需要加一个等待动画. 只需把以下代码加入页面中即可,图片可以根据自己的需求更换,更换图片之后需要改变l ...
- 网页Loading效果
问题描述:由于项目要求在页面提交以及加载的时候,有短暂的卡顿,需要用loading过渡. 1.下一个页面加载的时候实现: base-loading.js //获取浏览器页面可见高度和宽度 var _P ...
- 一个不错的loading效果--IT蓝豹
一个不错的loading效果 介绍:一个不错的loading加载效果,弹性收缩,效果不错,学习android动画的朋友可以下载来研究研究本例子其实由SeekBar实现,由MetaballView,Me ...
- 【转】 CSS3实现10种Loading效果
昨晚用CSS3实现了几种常见的Loading效果,虽然很简单,但还是分享一下,顺便也当是做做笔记…… PS:如需转载,请注明出处! 第1种效果: 代码如下: <div class="l ...
- jQuery8种不同的瀑布流懒加载loading效果
优化图片加载插件jQuery8种不同的瀑布流懒加载loading效果 在线预览 下载地址 实例代码 <ul class="grid effect-1" id="g ...
- HTML5 Canvas 实现的9个 Loading 效果
Sonic.js 是一个很小的 JavaScript 类,用于创建基于 HTML5 画布的加载图像.更强大的是 Sonic.js 还提供了基于现成的例子的创建工具,可以帮助你实现更多自定义的(Load ...
- 加载状态为complete时移除loading效果
一.JS代码: //获取浏览器页面可见高度和宽度 var _PageHeight = document.documentElement.clientHeight, _PageWidth = docum ...
- CSS3轻松实现清新 Loading 效果
至今HTML5中国已经为大家分享过几百种基于 CSS3 的Loading加载动画,效果酷炫代码简洁,非常值得学习借鉴;今天就先给大家分享两个常用的CSS3的Loading的案例. 第一种效果: HTM ...
- 一个很酷的加载loading效果--IT蓝豹
一个很酷的加载loading效果,自定义LeafLoadingView实现,LeafLoadingView继承view, 本例子主要由以下几点构成 (1):RotateAnimation实现叶子旋转 ...
随机推荐
- python学习_运算
1.数据类型 1.1数字 整型int,如2 浮点型float,如3.14和314E-2 复数complex,如(-5+4) 1.2布尔值 真或假 1或0 1.3字符串 'hello world' 2. ...
- 【LeetCode】Merge Two Sorted Lists(合并两个有序链表)
这道题是LeetCode里的第21道题. 题目描述: 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1-&g ...
- algorithm 头文件
非修改性序列操作(12个) 循环 对序列中的每个元素执行某操作 for_each() 查找 在序列中找出某个值的第一次出现的位置 find() 在序列中找出符合某谓词的第一个元素 find_if() ...
- Struts2 标签库与OGNL的使用
- ZOJ 3940 Modulo Query(YY+二分)
Modulo Query Time Limit: 2 Seconds Memory Limit: 65536 KB One day, Peter came across a function ...
- /bin , /sbin , /usr/sbin , /usr/local/sbin 的区别
usr 是 UNIX Software Resource 的缩写,也就是 Unix操作系统软件资源 所放置的目录. 一 /bin:Essential user command binaries(for ...
- python(4)-- 日期 & 时间
1. Python 提供了一个 time 和 calendar 模块可以用于格式化日期和时间. 2. 时间间隔是以秒为单位的浮点小数. 3. 每个时间戳都以自从1970年1月1日午夜(历元)经过了多长 ...
- 问题:viewController不会调用dealloc()不会销毁
问题 在调试程序时,我从ViewController A push进 ViewController B,在从B back时发现程序不会执行B里面的dealloc(),很诡异的问题,因为按理说此时点击b ...
- Java数据结构-------Map
常用Map:Hashtable.HashMap.LinkedHashMap.TreeMap 类继承关系: HashMap 1)无序: 2)访问速度快: 3)key不允许重复(只允许存在一个null ...
- 自定义JS类,并扩展其方法和属性
function CT() { } CT.prototype.P = "TTT"; CT.Test = function () { alert(arguments[0]); }; ...