C# Winform 实现自定义半透明遮罩层介绍
在网页中通过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 实现自定义半透明遮罩层介绍的更多相关文章
- C# Winform 实现自定义半透明loading加载遮罩层
在网页中通过div+css实现半透明效果不难,今天我们看看一种在winfrom中实现的方法: 效果图如下,正常时: 显示遮罩层时: 自定义遮罩层控件的源码如下: View Row Code 1 usi ...
- iOS 自定义相机带拍摄区域边框及半透明遮罩层(含源码)
开始时准备封装成ViewController的相机,但是在不改我相机控件源码的情况下,使用者很难自定义UI.于是想到将相机核心功能封装到View里,暴露出功能方法给外面调用,调用者只需将LFCamer ...
- jq 弹半透明遮罩层
jquery制作点击按钮弹出遮罩半透明登陆窗口 // )[^>]*$|^#([\w-]+)$/,M=/^.[^:#\[\.,]*$/,ka=/\S/,$= /^(\s|\u00A0)+|(\s| ...
- 解决css3遮罩层挡住下面元素事件的方法
比如大家常看到的鼠标移入图片中,会有一个挡住图片的黑色半透明遮罩层,上面还有文字介绍,这时候就会遇到该层遮挡住下面图片的跳转链接事件,这时候怎么办呢?有个简单的css3属性可以快速解决该问题:poin ...
- 【特效】手机端仿美团下拉菜单带遮罩层html+css+jquery
写了一个手机端的下拉菜单,类似美团,用相对单位rem写的. 效果截图: 代码很简单,原理有点类似嵌套的选项卡,其中的难点在于弹出下拉菜单后,出现黑色半透明遮罩层,而且下层列表页面禁止滚动了.关键就是给 ...
- jQuery实现遮罩层
1.1 背景半透明遮罩层样式 需要一个黑色(当然也可以其他)背景,且须设置为绝对定位,以下是项目中用到的css样式: /* 半透明的遮罩层 */ #overlay { background: #000 ...
- 移动端优化 && 清除移动端网站点击a标签时闪现的边框或遮罩层(CSS) && 移动端点击 && 文字不可选择
在移动端网站,当你点击加了a标签的文字或图片时,该元素的周围会闪现一个蓝色的边框,在微信上的网站就是如此:而有的浏览器会闪现一个半透明遮罩层,比如移动端的Chrome浏览器,其实这些特效无非就是为 ...
- Winform应用程序实现通用遮罩层
在WEB上,我们在需要进行大数据或复杂逻辑处理时,由于耗时较长,一般我们会在处理过程中的页面上显示一个半透明的遮罩层,上面放个图标或提示:正在处理中...等字样,这样用户体验就比较好了,然而如果在Wi ...
- Android自定义遮罩层设计
在做网页设计时,前端设计人员会经常用到基于JS开发的遮罩层,并且背景半透明.这样的效果怎么样在Android上实现呢?这个实现并不困难,先来上效果图: <ignore_js_op> 201 ...
随机推荐
- HDU 2389 Rain on your Parade(二分匹配,Hopcroft-Carp算法)
Rain on your Parade Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 655350/165535 K (Java/Ot ...
- 【maven】maven项目移除Maven Dependencies后如何再添加进去
比较着急这几天弄一个项目,所以匆忙间把maven项目的Maven Dependencies给remove掉了 如下图: 这下可好,整个项目报错了 解决方法: 对比了有Maven Dependencie ...
- Android 卡顿优化 3 布局优化 工具 Hierarchy Viewer
欲善其事, 先利其器. 分析布局, 就不得不用到Hierarchy Viewer了. 本文工具使用皆以GithubApp的详情界面RepoDetailActivity为例说明. 为了不影响阅读体验, ...
- Python爬虫之一 PySpider 抓取淘宝MM的个人信息和图片
ySpider 是一个非常方便并且功能强大的爬虫框架,支持多线程爬取.JS动态解析,提供了可操作界面.出错重试.定时爬取等等的功能,使用非常人性化. 本篇通过做一个PySpider 项目,来理解 Py ...
- [Python爬虫] 之十五:Selenium +phantomjs根据微信公众号抓取微信文章
借助搜索微信搜索引擎进行抓取 抓取过程 1.首先在搜狗的微信搜索页面测试一下,这样能够让我们的思路更加清晰 在搜索引擎上使用微信公众号英文名进行“搜公众号”操作(因为公众号英文名是公众号唯一的,而中文 ...
- [ES6] 11. String Templates
ECMAscript 6 lets us use string templates to gain a lot more control over strings in JavaScript. var ...
- [ES6] 08. Destructuring Assignment -- 1
Here is the way you get value from an object: var obj = { color: "blue" } console.log(obj. ...
- 利用SmtpClient发送邮件
1 163邮箱 HOST:smtp.163.com public static string CreateTimeoutTestMessage(string server) { string Suc ...
- 拓扑排序的实现_TopoSort
拓扑排序是求一个AOV网(顶点代表活动, 各条边表示活动之间的率先关系的有向图)中各活动的一个拓扑序列的运算, 可用于測试AOV 网络的可行性. 整个算法包含三步: 1.计算每一个顶点的入度, 存入I ...
- 病毒木马查杀实战第015篇:U盘病毒之脱壳研究
前言 因为我们的终于目标是编写出针对于这次的U盘病毒的专杀工具.而通过上次的分析我们知道,病毒有可能在不同的计算机中会以不同的名称进行显示.假设真是如此,那么就有必要在此分析出病毒的命名规律等特征,然 ...