Winform实现透明窗体/蒙版效果

5 篇文章 0 订阅

[前言]
最近客户项目部分UI要求有蒙版效果,研究多种方法还是达不到预期,某度他们的实现方法都是双窗体设计,在使用过程中特别不方便(比如要实现蒙版后切换线程后不能操作父窗体,即使使用两个ShowDialog显然还是不行).通过比较几种方法发现下面这篇博客中的实现比较有用:
https://www.cnblogs.com/dfcy/p/11586066.html

[样式展示]
蒙版效果

[功能原理]
1.画一块透明窗体,截Form后的背景
2.添加Panel,将Panel的Dock属性设置为Fill,再将Panel的背景色使用ARGB填充,如Color.FromArgb(150, 211, 211, 211)为样式展示的效果

[代码]

    public partial class Form1 : Form
    {
        private Color tr_color = Color.Transparent;

        private Color maskColor = Color.FromArgb(150, 211, 211, 211);//蒙版颜色100是透明度
        Point ParentLocation;//蒙版位置
        Size ParentSize;//蒙版大小
        private bool b_start = false;
        bool[] b_visible = null;
        public Form1(Point parentLocation, Size parentSize)
        {
            ParentLocation = parentLocation;
            ParentSize = parentSize;
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            //设置蒙版区域大小(即Form窗体大小随启动窗体大小一致)
            this.Location = ParentLocation;
            this.Size = ParentSize;

            maskPanel.BackColor = maskColor;//设置主Planel为蒙版颜色

            //设置主控件显示位置为居中
            parentPanel.Location = new Point((ParentSize.Width - parentPanel.Width) / 2, (ParentSize.Height - parentPanel.Height) / 2);

            SetBackgroundImageTransparent();
            BeginSet();
        }
        private void SetBackgroundImageTransparent()
        {
            Point pt = this.PointToScreen(new Point(0, 0));
            Bitmap b = new Bitmap(this.Width, this.Height);
            using (Graphics g = Graphics.FromImage(b))
            {
                g.CopyFromScreen(pt, new Point(), new Size(this.Width, this.Height));
            }
            this.BackgroundImage = b;
        }
        private void BeginSet()
        {
            tr_color = this.TransparencyKey;
            b_start = true;
        }
        private void Setting()
        {
            if (b_start)
            {
                b_visible = new bool[Controls.Count];
                for (int i = 0; i < Controls.Count; i++)
                {
                    b_visible[i] = Controls[i].Visible;
                    Controls[i].Visible = false;
                }
                BackgroundImage = null;
                BackColor = Color.White;
                b_start = false;
                this.TransparencyKey = Color.White;
            }
        }
        private void EndSet()
        {
            SetBackgroundImageTransparent();
            this.TransparencyKey = tr_color;
            for (int i = 0; i < Controls.Count; i++)
            {
                Controls[i].Visible = b_visible[i];
            }
            b_start = false;
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            Setting();
        }
        private void Form1_ResizeBegin(object sender, EventArgs e)
        {
            BeginSet();
        }
        private void Form1_ResizeEnd(object sender, EventArgs e)
        {
            EndSet();
        }
        private void Form1_Move(object sender, EventArgs e)
        {
            Setting();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }

[后语]
本章源码地址:https://download.csdn.net/download/qq_38469552/13087194
由于工作原因,没有过多时间来排版和优化控件,控件较为粗糙,内部bug可能存在很多,各位朋友同仁如发现异常请轻喷并与我及时联系,以免误导他人.
本章完

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值