做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("我是消息");
}
}

最新文章

  1. 说说JavaScriptCore
  2. 学习 opencv---(2) 图像的载入,显示和输出
  3. jquey与javascript相通运用查找(全)
  4. Ubuntu 配置 Tomcat
  5. 虚拟机下玩DXF
  6. position之absolute与relative 详解
  7. MSSQL CharIndex()用法
  8. System.Diagnostics命名空间里的Debug类和Trace类的用途
  9. 关于编译Lambda时报告返回的为void的错误
  10. git 配置文件
  11. 第m个全排列
  12. 20155237 2016-2017-2 《Java程序设计》第5周学习总结
  13. 在Linux上如何查看Python3自带的帮助文档?
  14. arguments.callee.caller
  15. AIOps 平台的误解,挑战及建议(中)— AIOps常见的误解
  16. 20165235 祁瑛 2018-4 《Java程序设计》第六周学习总结
  17. 从文本中读取字符——feof函数问题
  18. Sprint 冲刺第三阶段第一天
  19. python粘包分析与解决
  20. 卸载阿里云自带svn

热门文章

  1. 设计模式之第13章-职责链模式(Java实现)
  2. 直接选择排序(java)
  3. 团队Alpha版本冲刺(二)
  4. 谷歌插件请求ci 解决CI框架的Disallowed Key Characters错误提示
  5. php开启子进程处理
  6. AngularJs 特性 之 双向数据绑定
  7. 节点流——FileReaderWriter
  8. [luogu1357] 花园 [dp+矩阵快速幂]
  9. Hall 定理
  10. linux系统——日志文件系统及性能分析