1,需要一个动态的londing文件;在项目中我们新建一个文件夹来存放它;

2,在需要出现londing状态的窗体上加上一个Panel;

黄色区域是Panel,灰色的是需要被加载的区域。当需要触发londing,我们就把Panel显示出来,不让用户在这个区域能做任何修改;

下面的两个button事件就是触发londing的;在这里实例化的 OpaqueCommand对象;我们需要创建一个OpaqueCommand类

        OpaqueCommand cmd = new OpaqueCommand();
        /// <summary>
        /// 启动加载功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void show_Click(object sender, EventArgs e)
        {
            cmd.ShowOpaqueLayer(panelLonding, 125, true);
        }
        /// <summary>
        /// 关闭加载功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void hide_Click(object sender, EventArgs e)
        {
            cmd.HideOpaqueLayer();
        }

3,OpaqueCommand类里面的内容

public class OpaqueCommand
    {
        private OpaqueLayer m_OpaqueLayer = null;

        public void ShowOpaqueLayer(Control control, int alpha, bool isShowLoadingImage)
        {
            try
            {
                if (this.m_OpaqueLayer == null)
                {
                    this.m_OpaqueLayer = new OpaqueLayer(alpha, isShowLoadingImage);
                    control.Controls.Add(this.m_OpaqueLayer);
                    this.m_OpaqueLayer.Dock = DockStyle.Fill;
                    this.m_OpaqueLayer.BringToFront();
                }
                this.m_OpaqueLayer.Enabled = true;
                this.m_OpaqueLayer.Visible = true;
            }
            catch { }
        }

        public void HideOpaqueLayer()
        {
            try
            {
                if (this.m_OpaqueLayer != null)
                {
                    this.m_OpaqueLayer.Visible = false;
                    this.m_OpaqueLayer.Enabled = false;
                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.Message);
            }
        }
    }

 4,创建 OpaqueLayer 类,这里面是重新绘制了窗体,内容如下

[ToolboxBitmap(typeof(OpaqueLayer))]
    public class OpaqueLayer:Control
    {
        private bool transparentBG = true;//是否使用透明
        private int alpha = 125;//设置透明度
        private Container components = new Container();
        public OpaqueLayer()
            : this(125, true)
        {
        }

        public OpaqueLayer(int Alpha, bool IsShowLoadingImage)
        {
            SetStyle(System.Windows.Forms.ControlStyles.Opaque, true);
            base.CreateControl();

            this.alpha = Alpha;
            if (IsShowLoadingImage)
            {
                PictureBox pictureBox_Loading = new PictureBox();
                pictureBox_Loading.BackColor = System.Drawing.Color.White;
                pictureBox_Loading.Image = WinLonding.Properties.Resources.Loading;
                pictureBox_Loading.Name = "pictureBox_Loading";
                pictureBox_Loading.Size = new System.Drawing.Size(48, 48);
                pictureBox_Loading.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
                Point Location = new Point(this.Location.X + (this.Width - pictureBox_Loading.Width) / 2, this.Location.Y + (this.Height - pictureBox_Loading.Height) / 2);//居中
                pictureBox_Loading.Location = Location;
                pictureBox_Loading.Anchor = AnchorStyles.None;
                this.Controls.Add(pictureBox_Loading);
            }
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (!((components == null)))
                {
                    components.Dispose();
                }
            }
            base.Dispose(disposing);
        }

        /// <summary>
        /// 自定义绘制窗体
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            float vlblControlWidth;
            float vlblControlHeight;

            Pen labelBorderPen;
            SolidBrush labelBackColorBrush;

            if (transparentBG)
            {
                Color drawColor = Color.FromArgb(this.alpha, this.BackColor);
                labelBorderPen = new Pen(drawColor, 0);
                labelBackColorBrush = new SolidBrush(drawColor);
            }
            else
            {
                labelBorderPen = new Pen(this.BackColor, 0);
                labelBackColorBrush = new SolidBrush(this.BackColor);
            }
            base.OnPaint(e);
            vlblControlWidth = this.Size.Width;
            vlblControlHeight = this.Size.Height;
            e.Graphics.DrawRectangle(labelBorderPen, 0, 0, vlblControlWidth, vlblControlHeight);
            e.Graphics.FillRectangle(labelBackColorBrush, 0, 0, vlblControlWidth, vlblControlHeight);
        }

        protected override CreateParams CreateParams//v1.10
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x00000020; //0x20;  // 开启 WS_EX_TRANSPARENT,使控件支持透明
                return cp;
            }
        }

        [Category("OpaqueLayer"), Description("Whether to use transparent, the default is true")]
        public bool TransparentBG
        {
            get
            {
                return transparentBG;
            }
            set
            {
                transparentBG = value;
                this.Invalidate();
            }
        }

        [Category("OpaqueLayer"), Description("Set the transparency")]
        public int Alpha
        {
            get
            {
                return alpha;
            }
            set
            {
                alpha = value;
                this.Invalidate();
            }
        }
    }

这时候再点击show button 就能看到效果啦!

最新文章

  1. JS入门学习,写一个时钟~
  2. Percona Toolkit 使用
  3. texy
  4. ruby 2.2
  5. Nginx 笔记与总结(14)expires 缓存设置
  6. poj 3621 0/1分数规划求最优比率生成环
  7. CentOS(七)--Linux文件类型及目录配置
  8. 【转】树莓派学习笔记——I2C Tools 学习笔记
  9. (Problem 4)Largest palindrome product
  10. 64位win7系统中vb工程显示加载MSCOMCTL.OCX失败
  11. win10 uwp 异步进度条
  12. Hibernate自动生成实体类注解(转)
  13. [redis] Redis的介绍
  14. 解决AD9中“......has&#160;no&#160;driver”的问题
  15. ESP8266 上线
  16. Templates中的标签if
  17. andorid UI事件
  18. c++ 容器元素填充指定数量的元素(generate_n)
  19. uva 12356 Army Buddies 树状数组解法 树状数组求加和恰为k的最小项号 难度:1
  20. Framework​Element.​Find​Name 根据名字查找控件

热门文章

  1. FastDFS + Nginx 安装
  2. 在gitlab上面创建私有库
  3. 将Tomcat配置到你的mac电脑上,命令行启动tomcat
  4. nyoj281 整数中的1(二) 数位DP
  5. linux 存储技术 部署iSCSI NFS Multipath多路径
  6. Linux基础五
  7. 笔记︱图像语义分割(FCN、CRF、MRF)、论文延伸(Pixel Objectness、)
  8. tensorflow+入门笔记︱基本张量tensor理解与tensorflow运行结构
  9. Android项目中的换肤总结
  10. @restcontroller与@controller的区别