2016.5.30实现透明Panel及控件置顶的方法

本文介绍了一种在C#中创建透明Panel的方法,通过自定义Panel类并设置其背景为透明,使得可以透过该Panel看到下方的控件,同时保持不可交互特性。文章详细展示了如何创建自定义Panel类,并将其置于其他控件之上。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

想放置一个透明Panel在某控件上端,实现效果是可透过此Panel看见下面控件,但鼠标点击却无任何反应。

 

1、新建置自定义Panel类

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Drawing;

 

namespace NavDataManager

{

    public class MyTransparentPanel : Panel

    {

        public MyTransparentPanel() 

        {   

            this.SetStyle(ControlStyles.SupportsTransparentBackColor | ControlStyles.Opaque, true); 

            this.BackColor = Color.Transparent; 

        } 

 

        protected override CreateParams CreateParams 

        { 

            get 

            { 

                CreateParams cp = base.CreateParams; 

                cp.ExStyle = 0x20; 

                return cp; 

            } 

        } 

    }

}

 

2、窗体中添加此自定义Panel控件

MyTransparentPanel pan = new MyTransparentPanel();           

pan.Size = uC_EditLeg1.Size;

pan.Location = new Point(uC_EditLeg1.Left, uC_EditLeg1.Top);

this.Controls.Add(pan);

此时并没有效果,因为此Panel是置于控件底层的,没有覆盖别的控件

 

3、将此控件置于顶层,遮挡其它控件

最简单的方法:mypan.BringToFront();  //前提是先执行this.Controls.Add(pan);

 

此外还可以用 this.Controls.SetChildIndex(mypan,0);//索引越小,控件位置越靠上。

 

还有一种笨办法

int index = this.Controls.GetChildIndex((Control)pan);//取得要置顶控件的index

ArrayList AL = new ArrayList();//用来装入控件的容器

for (int i = 0; i < index; i++)//把要置顶控件上面的控件都装入容器

    AL.Add(this.Controls[i]);

for (int i = 0; i < AL.Count; i++)

{

     //用一次删除和一次添加操作,让它上面的控件排到下面去.

     this.Controls.Remove((Control)AL[i]);

       this.Controls.Add((Control)AL[i]);

}

此时这些控件全到panel下面去了,鼠标不管用了。哈哈

转载于:https://www.cnblogs.com/mol1995/p/5965002.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值