C# Winform 实现自定义半透明loading加载遮罩层
在网页中通过div+css实现半透明效果不难,今天我们看看一种在winfrom中实现的方法:
效果图如下,正常时:

显示遮罩层时:

自定义遮罩层控件的源码如下:
| 1 | using System; |
| 2 | using System.Drawing; |
| 3 | using System.Windows.Forms; |
| 4 | using System.ComponentModel; |
| 5 | |
| 6 | namespace MyOpaqueLayer |
| 7 | { |
| 8 | /// <summary> |
| 9 | /// 自定义控件:半透明控件 |
| 10 | /// </summary> |
| 11 | /* |
| 12 | * [ToolboxBitmap(typeof(MyOpaqueLayer))] |
| 13 | * 用于指定当把你做好的自定义控件添加到工具栏时,工具栏显示的图标。 |
| 14 | * 正确写法应该是 |
| 15 | * [ToolboxBitmap(typeof(XXXXControl),"xxx.bmp")] |
| 16 | * 其中XXXXControl是你的自定义控件,"xxx.bmp"是你要用的图标名称。 |
| 17 | */ |
| 18 | [ToolboxBitmap(typeof(MyOpaqueLayer))] |
| 19 | public class MyOpaqueLayer : System.Windows.Forms.Control |
| 20 | { |
| 21 | private bool _transparentBG = true;//是否使用透明 |
| 22 | private int _alpha = 125;//设置透明度 |
| 23 | |
| 24 | private System.ComponentModel.Container components = new System.ComponentModel.Container(); |
| 25 | |
| 26 | public MyOpaqueLayer() |
| 27 | : this(125, true) |
| 28 | { |
| 29 | } |
| 30 | |
| 31 | public MyOpaqueLayer(int Alpha, bool IsShowLoadingImage) |
| 32 | { |
| 33 | SetStyle(System.Windows.Forms.ControlStyles.Opaque, true); |
| 34 | base.CreateControl(); |
| 35 | |
| 36 | this._alpha = Alpha; |
| 37 | if (IsShowLoadingImage) |
| 38 | { |
| 39 | PictureBox pictureBox_Loading = new PictureBox(); |
| 40 | pictureBox_Loading.BackColor = System.Drawing.Color.White; |
| 41 | pictureBox_Loading.Image = 加载中.Properties.Resources.loading; |
| 42 | pictureBox_Loading.Name = "pictureBox_Loading"; |
| 43 | pictureBox_Loading.Size = new System.Drawing.Size(48, 48); |
| 44 | pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize; |
| 45 | Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);//居中 |
| 46 | pictureBox_Loading.Location = Location; |
| 47 | pictureBox_Loading.Anchor = AnchorStyles.None; |
| 48 | this.Controls.Add(pictureBox_Loading); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | |
| 53 | protected override void Dispose(bool disposing) |
| 54 | { |
| 55 | if (disposing) |
| 56 | { |
| 57 | if (!((components == null))) |
| 58 | { |
| 59 | components.Dispose(); |
| 60 | } |
| 61 | } |
| 62 | base.Dispose(disposing); |
| 63 | } |
| 64 | |
| 65 | /// <summary> |
| 66 | /// 自定义绘制窗体 |
| 67 | /// </summary> |
| 68 | /// <param name="e"></param> |
| 69 | protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) |
| 70 | { |
| 71 | float vlblControlWidth; |
| 72 | float vlblControlHeight; |
| 73 | |
| 74 | Pen labelBorderPen; |
| 75 | SolidBrush labelBackColorBrush; |
| 76 | |
| 77 | if (_transparentBG) |
| 78 | { |
| 79 | Color drawColor = Color.FromArgb(this._alpha, this.BackColor); |
| 80 | labelBorderPen = new Pen(drawColor, 0); |
| 81 | labelBackColorBrush = new SolidBrush(drawColor); |
| 82 | } |
| 83 | else |
| 84 | { |
| 85 | labelBorderPen = new Pen(this.BackColor, 0); |
| 86 | labelBackColorBrush = new SolidBrush(this.BackColor); |
| 87 | } |
| 88 | base.OnPaint(e); |
| 89 | vlblControlWidth = this.Size.Width; |
| 90 | vlblControlHeight = this.Size.Height; |
| 91 | e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight); |
| 92 | e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight); |
| 93 | } |
| 94 | |
| 95 | |
| 96 | protected override CreateParams CreateParams//v1.10 |
| 97 | { |
| 98 | get |
| 99 | { |
| 100 | CreateParams cp = base.CreateParams; |
| 101 | cp.ExStyle |= 0x00000020; //0x20; // 开启 WS_EX_TRANSPARENT,使控件支持透明 |
| 102 | return cp; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * [Category("myOpaqueLayer"), Description("是否使用透明,默认为True")] |
| 108 | * 一般用于说明你自定义控件的属性(Property)。 |
| 109 | * Category用于说明该属性属于哪个分类,Description自然就是该属性的含义解释。 |
| 110 | */ |
| 111 | [Category("MyOpaqueLayer"), Description("是否使用透明,默认为True")] |
| 112 | public bool TransparentBG |
| 113 | { |
| 114 | get |
| 115 | { |
| 116 | return _transparentBG; |
| 117 | } |
| 118 | set |
| 119 | { |
| 120 | _transparentBG = value; |
| 121 | this.Invalidate(); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | [Category("MyOpaqueLayer"), Description("设置透明度")] |
| 126 | public int Alpha |
| 127 | { |
| 128 | get |
| 129 | { |
| 130 | return _alpha; |
| 131 | } |
| 132 | set |
| 133 | { |
| 134 | _alpha = value; |
| 135 | this.Invalidate(); |
| 136 | } |
| 137 | } |
| 138 | } |
| 139 | } |
OpaqueCommand的方法:ShowOpaqueLayer(显示遮罩层)和HideOpaqueLayer(隐藏遮罩层)
| 1 | using System; |
| 2 | using System.Windows.Forms; |
| 3 | |
| 4 | namespace 加载中 |
| 5 | { |
| 6 | class OpaqueCommand |
| 7 | { |
| 8 | private MyOpaqueLayer.MyOpaqueLayer m_OpaqueLayer = null;//半透明蒙板层 |
| 9 | |
| 10 | /// <summary> |
| 11 | /// 显示遮罩层 |
| 12 | /// </summary> |
| 13 | /// <param name="control">控件</param> |
| 14 | /// <param name="alpha">透明度</param> |
| 15 | /// <param name="isShowLoadingImage">是否显示图标</param> |
| 16 | public void ShowOpaqueLayer(Control control, int alpha, bool isShowLoadingImage) |
| 17 | { |
| 18 | try |
| 19 | { |
| 20 | if (this.m_OpaqueLayer == null) |
| 21 | { |
| 22 | this.m_OpaqueLayer = new MyOpaqueLayer.MyOpaqueLayer(alpha, isShowLoadingImage); |
| 23 | control.Controls.Add(this.m_OpaqueLayer); |
| 24 | this.m_OpaqueLayer.Dock = DockStyle.Fill; |
| 25 | this.m_OpaqueLayer.BringToFront(); |
| 26 | } |
| 27 | this.m_OpaqueLayer.Enabled = true; |
| 28 | this.m_OpaqueLayer.Visible = true; |
| 29 | } |
| 30 | catch { } |
| 31 | } |
| 32 | |
| 33 | /// <summary> |
| 34 | /// 隐藏遮罩层 |
| 35 | /// </summary> |
| 36 | public void HideOpaqueLayer() |
| 37 | { |
| 38 | try |
| 39 | { |
| 40 | if (this.m_OpaqueLayer != null) |
| 41 | { |
| 42 | this.m_OpaqueLayer.Visible = false; |
| 43 | this.m_OpaqueLayer.Enabled = false; |
| 44 | } |
| 45 | } |
| 46 | catch(Exception ex) |
| 47 | { |
| 48 | //MessageBox.Show(ex.Message); |
| 49 | } |
| 50 | } |
| 51 | } |
| 52 | } |
自定义半透明遮罩层源码下载:
files.cnblogs.com/JuneZhang/%E8%87%AA%E5%AE%9A%E4%B9%89%E5%8D%8A%E9%80%8F%E6%98%8E%E9%81%AE%E7%BD%A9%E5%B1%82-%E6%BA%90%E7%A0%81.rar
C# Winform 实现自定义半透明loading加载遮罩层的更多相关文章
- ajax异步加载遮罩层特效
<!doctype html> <html> <head> <title>遮罩层(正在加载中)</title> <meta chars ...
- echarts 设置数据加载遮罩层
//显示加载数据的loading chart.showLoading({ text: "图表数据正在努力加载...", x ...
- vue2 自定义全局组件(Loading加载效果)
vue2 自定义全局组件(Loading加载效果) github地址: https://github.com/ccyinghua/custom-global-component 一.构建项目 vue ...
- HTML5 五彩圆环Loading加载动画实现教程
原文:HTML5 五彩圆环Loading加载动画实现教程 今天我们要来介绍一款效果很特别的HTML5 Loading加载动画,不像其他的Loading动画,这款Loading动画颜色很丰富,并且在转圈 ...
- 使用Dialog实现全局Loading加载框
Dialog实现全局Loading加载框 很多人在实现Loading加载框的时候,都是在当前的页面隐藏一个Loading布局,需要加载的时候,显示出来,加载完再隐藏 使用Dialog实现Loading ...
- vue+elementUI+axios实现的全局loading加载动画
在项目中,很多时候都需要loading加载动画来缓解用户的焦虑等待,比如说,我打开了一个页面,而这个页面有很多接口请求,但浏览器的请求并发数就那么几个,再加上如果网速不行的话,那么这时候,用户很可能就 ...
- WPF 客户端浏览器 添加Loading加载进度
在windows开发界面时,使用浏览器来请求和显示网页内容,是比较常见的. 但是在请求网页内容时,因网速或者前端功能复杂加载较慢,亦或者加载时遇到各种问题,如空白/黑屏/加载不完整/证书问题等. 因此 ...
- QT自定义控件系列(二) --- Loading加载动画控件
本系列主要使用Qt painter来实现一些基础控件.主要是对平时自行编写的一些自定义控件的总结. 为了简洁.低耦合,我们尽量不使用图片,qrc,ui等文件,而只使用c++的.h和.cpp文件. 由于 ...
- JVM自定义类加载器加载指定classPath下的所有class及jar
一.JVM中的类加载器类型 从Java虚拟机的角度讲,只有两种不同的类加载器:启动类加载器和其他类加载器. 1.启动类加载器(Boostrap ClassLoader):这个是由c++实现的,主要负责 ...
随机推荐
- APP评价(星星点赞)很简单
1.用代码或者storyboard创建5个button(现在一般都是5个星星) 我用的是storyboard 记得一定要设置button的tag值 在.h中 @property (weak, nona ...
- xcode编译运行报错纪录(持续更新)
---恢复内容开始--- 1. Undefined symbols for architecture i386: "_deflate", referenced from: -[NS ...
- 10.13_extjs,combox,效率为什么这么低
(1)今天很累!经过三天的奋斗,终于完成了完全最优界面的快捷选址功能.今天是最后一天,最紧张,也最累. 为何我的效率不高呢?!我的时间都花费到哪儿呢?①阅读代码理解过去的逻辑:②关键技术上的再复习再巩 ...
- Codeforces Round #299 (Div. 1)
Problem A: 实际上对于一段数字假设和为k,每次取较大的m个进行t次减一操作,最多减去的是min(m*t,k). 明白了这个结论就可以直接二分答案了. #include <bits/st ...
- 【CODECHEF】【phollard rho + miller_rabin】The First Cube
All submissions for this problem are available. Read problems statements in Mandarin Chinese and Rus ...
- 九度OJ 1066 字符串排序
题目地址:http://ac.jobdu.com/problem.php?pid=1066 题目描述: 输入一个长度不超过20的字符串,对所输入的字符串,按照ASCII码的大小从小到大进行排序,请输出 ...
- IBUS-WARNING **: Process Key Event failed: Timeout was reached
在gvim中ibus敲字时,偶尔会在n秒之后才显示到屏幕,反应死慢.控制台会看到下面的错误信息. (gvim:): IBUS-WARNING **: Process Key Event failed: ...
- localstorage 使用
localstorage作为HTML5的一个特殊属性,在发布时就备受关注:最近总结了其一些小的用法,希望可以抛砖引玉. 因HTML5本地存储只能存字符串,所以所有数据存储的话,都要转化成字符串:而js ...
- [C#]『Barrier』任务并行库使用小计
Barrier 是一个对象,它可以在并行操作中的所有任务都达到相应的关卡之前,阻止各个任务继续执行. 如果并行操作是分阶段执行的,并且每一阶段要求各任务之间进行同步,则可以使用该对象. --MSDN ...
- JavaScript语言用10张图
JavaScript 语言基础知识点总结,用图片树形结构说明.包括Windows对象.JavaScriptDOM基本操作.JavaScript变量.JavaScript数据类型.JavaScript运 ...