在网页中通过div+css实现半透明效果不难,今天我们看看一种在winfrom中实现的方法:
效果图如下,正常时:
显示遮罩层时:
自定义遮罩层控件的源码如下:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Drawing; using System.Windows.Forms; using System.ComponentModel; namespace MyOpaqueLayer { /// <summary> /// 自定义控件:半透明控件 /// </summary> /* * [ToolboxBitmap(typeof(MyOpaqueLayer))] * 用于指定当把你做好的自定义控件添加到工具栏时,工具栏显示的图标。 * 正确写法应该是 * [ToolboxBitmap(typeof(XXXXControl),"xxx.bmp")] * 其中XXXXControl是你的自定义控件,"xxx.bmp"是你要用的图标名称。 */ [ToolboxBitmap(typeof(MyOpaqueLayer))] public class MyOpaqueLayer : System.Windows.Forms.Control { private bool _transparentBG = true;//是否使用透明 private int _alpha = 125;//设置透明度 private System.ComponentModel.Container components = new System.ComponentModel.Container(); public MyOpaqueLayer() : this(125, true) { } public MyOpaqueLayer(int Alpha, bool IsShowLoadingImage) { SetStyle(System.Windows.Forms.ControlStyles.Opaque, <