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

  • 相关阅读:
    计算机知识
    试题:论需求分析方法及应用
    试题:论信息系统开发方法及应用
    爬虫数据存储——安装docker和ElasticSearch(基于Centos7)
    go并发版爬虫
    go单任务版爬虫
    可变类型与不可变类型
    基本数据类型内置方法
    @submit.native.prevent作用
    获取当月第一天,今天的日期的方法
  • 原文地址:https://www.cnblogs.com/mol1995/p/5965002.html
Copyright © 2011-2022 走看看