效果图如下,正常时:

遮罩时:

控件代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.ComponentModel;

namespace MyOpaqueLayer
{

[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 showLoadingImage)
        {
            SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
            base.CreateControl();
            this._alpha = Alpha;
            ;
            if (showLoadingImage)
            {
                PictureBox pictureBox_Loading = new PictureBox();
                pictureBox_Loading.BackColor = System.Drawing.Color.White;
                pictureBox_Loading.Image = global::MyOpaqueLayer.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);
        }

/// <summary>
        /// 自定义绘制窗体
        /// </summary>
        /// <param name="e"></param>
        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);

}
        /// <summary>
        /// 
        /// </summary>
        protected override CreateParams CreateParams//v1.10 
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x20;  // 开启 WS_EX_TRANSPARENT,使控件支持透明
                return cp;
            }
        }

[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();
            }
        }

}

}

源码下载:

遮罩层源码

转载于:https://www.cnblogs.com/zhaobl/archive/2010/01/08/1642145.html

在Winform中实现半透明遮罩层相关推荐

  1. C#实现Winform自定义半透明遮罩层

    在网页中通过div+css实现半透明效果不难,今天我们看看一种在winfrom中实现的方法: 效果图如下,正常时: 显示遮罩层时: 自定义遮罩层控件的源码如下: View Code using Sys ...

  2. web页在微信中访问增加遮罩层 右上角弹出在浏览器中打开

    web页在微信中访问增加遮罩层 右上角弹出在浏览器中打开 <style type="text/css"> * {margin: 0;padding: 0; }a {te ...

  3. html背景图片半透明遮罩,BufferedImage在图片添加半透明遮罩层

    利用BufferedImage处理图片时,例如生成海报往添加图片加文字,由于背景图颜色差异不统一,需要加入纯色半透明遮罩层后,再往上面添加文字. 以下代码演示:在图片底部加入一条高度20的半透明遮罩层 ...

  4. iOS自定义相机:带拍摄区域边框、半透明遮罩层、点击屏幕对焦、自动裁剪(含demo源码)

    文章目录 前言 I.案例1:加一个长方形的框框并裁剪身份证照片(无半透明遮罩层) 1.1 demo 源码 1.2 控制屏幕旋转方向 1.3 封装富文本API 1.4 设置相机预览层和证件框框的fram ...

  5. 网页制作 HTML实现遮罩层的方法 HTML中如何使用遮罩层

    网页制作 HTML实现遮罩层的方法 HTML中如何使用遮罩层 Web页面中使用遮罩层,可防止重复操作,提示loading:也可以模拟弹出模态窗口. 实现思路:一个DIV作为遮罩层,一个DIV显示loa ...

  6. CSS半透明遮罩层 and 定位

    html <!-- 半透明遮罩层 --> <div class="mask" style="display: none;"></d ...

  7. css-实现鼠标移至图片上显示黑色半透明遮罩层,且不至于遮住图片上的超链接的方法

    作为一个刚接触html和css的小白,总会遇到一些细节问题,在这里写下一个我遇到的小问题的解决方法,方便今后加深理解,更高效地学习. 一般情况下,让一个图片有超链接,可以用a标签把 img包住,但是要 ...

  8. 在弹窗中新建一个遮罩层

    一.mask-div 在原来页面弹出一个弹窗后,鼠标还是可以点击页面其他地方,所以我要给页面弹出一个遮罩层,把页面所有东西都遮住,不给用户点击除了弹窗之外的东西. 实现后的效果: 当我点击" ...

  9. HTML实现遮罩层的方法 HTML中如何使用遮罩层

    Web页面中使用遮罩层,可防止重复操作,提示loading:也可以模拟弹出模态窗口. 实现思路:一个DIV作为遮罩层,一个DIV显示loading动态GIF图片.在下面的示例代码中,同时展示了如何在i ...

  10. 半透明遮罩层覆盖整个可视区域

    我们经常会遇到点击一个按钮弹出一个对话框和一个变暗的遮罩层,简单的写法只能让遮罩层覆盖浏览器的大小,那么怎么让遮罩层覆盖全部区域呢? css代码如下: 1 html,body { 2 height: ...

最新文章

  1. 程序员如何判断是否到了该辞职的时候?以及常用的辞职借口总结
  2. powershell创建iis站点、应用程序及应用程序池
  3. python 调试命令
  4. Sitecore 9 为什么数据驱动的组织选择它
  5. 遨博机器人执行线程_智造洞察 | 遨博机器人推出国内首条“用机器人生产机器人”产线;服务机器人等助力8月经济回升;浙江企业研发飞机发动机榫槽数控拉床...
  6. 怎么用php配合js编写动态页面_JavaScript_JavaScript教程:用JS脚本实现Web页面信息交互范例,要实现动态交互,必须掌握有 - phpStudy...
  7. GridControl 选择列、复选框全选(上)
  8. php如何用session做查询条件,php如何使用session?
  9. java : jstack 显示虚拟机的线程快照
  10. [01] 四大组件之Activity
  11. (摘要)100个伟大的商业理念:理念35:引爆流行
  12. 《微软:DirectShow开发指南》第三章 Programming DirectShow Applications
  13. SAS (Statistics Analysis System) 统计分析系统软件
  14. 【教程】Edraw Max(亿图图示)基本操作图文详解
  15. linux终端命令大全(完善中)
  16. 记录一次pl2303串口驱动解决故障
  17. libmodbus 封装成dll_Windows Visual Studio 2017 编译 libmodbus
  18. 论文排版一步搞定之公式——(公式居中,编号居右)
  19. win7映射网络驱动器消失了_win7映射网络驱动器 找不到 - 卡饭网
  20. test 4:假币问题

热门文章

  1. 报错:Avoid adding reactive properties to a Vue instance or its root $data at runtime - declare it upfr
  2. 提交表单上传照片预览
  3. SetConsoleTextAttribute 设置Windows控制台字体背景颜色(转载)
  4. 为什么使用close()关闭所打开文件
  5. Hibernate之集合映射
  6. FISCO BCOS(二)———配置及使用控制台
  7. linux命令应用大词典.pdf,Linux命令应用大词典
  8. sqlserver安装介质上文件的长路径名称失败_SQL Server 2012 软件安装教程
  9. mybatis-plus主键生成策略
  10. 内存中的 html 网页,网页制作使用html-webpack-plugin'入再内存中生成 html 页面插件...