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

想放置一个透明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下面去了,鼠标不管用了。哈哈

posted on 2016-10-15 18:12  mol1995  阅读(1993)  评论(0编辑  收藏  举报

导航