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下面去了,鼠標不管用了。哈哈


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM