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实现叶子旋转 ...
随机推荐
- 『编写高质量代码Web前端开发修炼手册』读书笔记--高质量的CSS
1.怪异模式和DTD 标准模式:浏览器根据规范表现页面 怪异模式:模拟老浏览器行为防止老站点无法工作(为了兼容老式浏览器的代码),如果漏写DTD(Document Type Definition文档定 ...
- Leetcode 521.最长特殊序列I
最长特殊序列 I 给定两个字符串,你需要从这两个字符串中找出最长的特殊序列.最长特殊序列定义如下:该序列为某字符串独有的最长子序列(即不能是其他字符串的子序列). 子序列可以通过删去字符串中的某些字符 ...
- 聊聊、Spring 数据源
平时开发中我们每天都会跟数据库打交道,页面上显示的数字,图片,语音,等等都存在某个地方,而我们就是要从那个地方拿到我们想要的.现在存储数据的方式越来越多,多种多样,但用的最多的还是关系数据库.Spri ...
- [oldboy-django][深入 rest framework] restframewok 教程: 分页功能
http://www.django-rest-framework.org/api-guide/pagination/ https://stackoverflow.com/questions/31785 ...
- [oldboy-django][2深入django]学生管理(Form)--查看(分页)
1 需求: 查看所有学生的信息,(分页功能) 2 前端:bootstrap美化前端 <!DOCTYPE html> <html lang="en"> < ...
- TOJ 3974: Region n条直线m个圆最多将圆分为几个区域
3974: Region Time Limit(Common/Java):1000MS/3000MS Memory Limit:65536KByteTotal Submit: 33 ...
- 【转】Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)
本篇文章主要介绍了"Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)",主要涉及到Unity3D 场景切换与持久化简单数据储存(PlayerPrefs类)方 ...
- 回文串(bzoj 3676)
Description 考虑一个只包含小写拉丁字母的字符串s.我们定义s的一个子串t的“出 现值”为t在s中的出现次数乘以t的长度.请你求出s的所有回文子串中的最 大出现值. Input 输入只有一行 ...
- HDU [P5015] 233 Matrix
矩阵快速幂 按列递推 #include <iostream> #include <cstdio> #include <cstdlib> #include <a ...
- BZOJ 3043: IncDec Sequence
3043: IncDec Sequence Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 578 Solved: 325[Submit][Statu ...