博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#实现Winform自定义半透明遮罩层
阅读量:7287 次
发布时间:2019-06-30

本文共 5669 字,大约阅读时间需要 18 分钟。

在网页中通过div+css实现半透明效果不难,今天我们看看一种在winfrom中实现的方法:

效果图如下,正常时:

显示遮罩层时:

自定义遮罩层控件的源码如下:

View Code
using System;using System.Drawing;using System.Windows.Forms;using System.ComponentModel;namespace MyOpaqueLayer{    ///     /// 自定义控件:半透明控件    ///     /*      * [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, true);            base.CreateControl();            this._alpha = Alpha;            if (IsShowLoadingImage)            {                PictureBox pictureBox_Loading = new PictureBox();                pictureBox_Loading.BackColor = System.Drawing.Color.White;                pictureBox_Loading.Image = 加载中.Properties.Resources.loading;                  pictureBox_Loading.Name = "pictureBox_Loading";                pictureBox_Loading.Size = new System.Drawing.Size(48, 48);                pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;                Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);//居中                pictureBox_Loading.Location = Location;                pictureBox_Loading.Anchor = AnchorStyles.None;                this.Controls.Add(pictureBox_Loading);            }        }        protected override void Dispose(bool disposing)        {            if (disposing)            {                if (!((components == null)))                {                    components.Dispose();                }            }            base.Dispose(disposing);        }        ///         /// 自定义绘制窗体        ///         ///         protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)        {            float vlblControlWidth;            float vlblControlHeight;            Pen labelBorderPen;            SolidBrush labelBackColorBrush;            if (_transparentBG)            {                Color drawColor = Color.FromArgb(this._alpha, this.BackColor);                labelBorderPen = new Pen(drawColor, 0);                labelBackColorBrush = new SolidBrush(drawColor);            }            else            {                labelBorderPen = new Pen(this.BackColor, 0);                labelBackColorBrush = new SolidBrush(this.BackColor);            }            base.OnPaint(e);            vlblControlWidth = this.Size.Width;            vlblControlHeight = this.Size.Height;            e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);            e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);        }        protected override CreateParams CreateParams//v1.10         {            get            {                CreateParams cp = base.CreateParams;                cp.ExStyle |= 0x00000020; //0x20;  // 开启 WS_EX_TRANSPARENT,使控件支持透明                return cp;            }        }        /*         * [Category("myOpaqueLayer"), Description("是否使用透明,默认为True")]         * 一般用于说明你自定义控件的属性(Property)。         * Category用于说明该属性属于哪个分类,Description自然就是该属性的含义解释。         */        [Category("MyOpaqueLayer"), Description("是否使用透明,默认为True")]        public bool TransparentBG        {            get            {                return _transparentBG;            }            set            {                _transparentBG = value;                this.Invalidate();            }        }        [Category("MyOpaqueLayer"), Description("设置透明度")]        public int Alpha        {            get            {                return _alpha;            }            set            {                _alpha = value;                this.Invalidate();            }        }    }}

OpaqueCommand的方法:ShowOpaqueLayer(显示遮罩层)和HideOpaqueLayer(隐藏遮罩层)

View Code
using System;using System.Windows.Forms;namespace 加载中{    class OpaqueCommand    {        private MyOpaqueLayer.MyOpaqueLayer m_OpaqueLayer = null;//半透明蒙板层        ///         /// 显示遮罩层        ///         /// 控件        /// 透明度        /// 是否显示图标        public void ShowOpaqueLayer(Control control, int alpha, bool isShowLoadingImage)        {            try            {                if (this.m_OpaqueLayer == null)                {                    this.m_OpaqueLayer = new MyOpaqueLayer.MyOpaqueLayer(alpha, isShowLoadingImage);                    control.Controls.Add(this.m_OpaqueLayer);                    this.m_OpaqueLayer.Dock = DockStyle.Fill;                    this.m_OpaqueLayer.BringToFront();                }                this.m_OpaqueLayer.Enabled = true;                this.m_OpaqueLayer.Visible = true;            }            catch { }        }        ///         /// 隐藏遮罩层        ///         public void HideOpaqueLayer()        {            try            {                if (this.m_OpaqueLayer != null)                {                    this.m_OpaqueLayer.Visible = false;                    this.m_OpaqueLayer.Enabled = false;                }            }            catch(Exception ex)            {                //MessageBox.Show(ex.Message);            }        }    }}

源码下载:

 

转载地址:http://wbcjm.baihongyu.com/

你可能感兴趣的文章
将非工程下的图片显示到前端jsp的方法
查看>>
jQuery 常用工具函数
查看>>
TreeSet实现排序接口
查看>>
scws分词 自定义词库的方法
查看>>
Qt开发环境如何部署Mac OS(1)
查看>>
10大iOS开发者最喜爱的类库
查看>>
合并数组去重
查看>>
Windows使用MinGW_w64安装gcc
查看>>
Java URLClassLoader实现插件功能开发
查看>>
Thread线程的停止与Timer定时器的停止方法
查看>>
spring MVC自定义action名称
查看>>
python如何识别字符串有多少个中文字符
查看>>
MyEclipse10安装checkStyle与findBugs插件及基本使用
查看>>
MAC 安装更新 ANT
查看>>
微软新ML框架 interpret-尝试
查看>>
创建可序列化的自定义数据结构
查看>>
ios SDWebImage
查看>>
innobackupex部分备份
查看>>
mongodb查询速度慢是什么原因?
查看>>
shell top解析
查看>>