zoukankan      html  css  js  c++  java
  • 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下面去了,鼠标不管用了。哈哈

  • 相关阅读:
    Python中的结构化数据分析利器-Pandas简介
    A great tutorial with Jupyter notebook for ML beginners
    快速修改Matlab默认启动路径(Windows/Mac)
    十大opengl教程
    vtk 基础概念
    OpenGL入门学习
    glut glew区别
    测试程序
    说说C语言运算符的“优先级”与“结合性”
    c++ ACM常用函数
  • 原文地址:https://www.cnblogs.com/mol1995/p/5965002.html
Copyright © 2011-2022 走看看