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 就能看到效果啦!

在 WinForm 中 如何实现 加载等待功能的更多相关文章

  1. WinForm中实现Loading加载界面

    1,LoaderForm窗体中添加PictureBox,然后添加Loading图片 2,窗体内属性设置 StartPosition :CenterScreen在屏幕中心显示 TopMost:True置 ...

  2. Android中的Glide加载图片

    注意:在Android Studio的项目的build.gradle中添加: compile 'com.github.bumptech.glide:glide:3.6.1' 然后同步一下 目录: 使用 ...

  3. WinForm ListView不分页加载大量数据

    WinForm的ListView在加载大量数据时会出现闪烁的问题,同时数据加载很慢.如果你的列表中有超过千条的数据且不做特殊处理还是用普通的ListView.Items.Add(),估计你的用户得抱怨 ...

  4. IOS开发UI篇之──自定义加载等待框(MBProgressHUD)

    本文转载至 http://blog.csdn.net/xunyn/article/details/8064984   原文地址http://www.189works.com/article-89289 ...

  5. [Swift通天遁地]一、超级工具-(11)使用EZLoadingActivity制作Loading加载等待动画

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  6. WPF加载等待动画

    原文:WPF加载等待动画 原文地址:https://www.codeproject.com/Articles/57984/WPF-Loading-Wait-Adorner 界面遮罩 <UserC ...

  7. vue-element-admin 全局loading加载等待

    最近遇到需求: 全局加载loading,所有接口都要可以手动控制是否展示加载等待的功能 当拿到这个需求的时候我是拒绝的,因为我以及局部写好了0.0,这是要大改呀....,没办法老板的要求,只能硬着头皮 ...

  8. DevExpress窗体加载等待

    using DevExpress.XtraEditors; using DevExpress.XtraSplashScreen; using System; using System.Collecti ...

  9. Android什么时候进行View中Background的加载

    对大多数Android的开发者来说,最经常的操作莫过于对界面进行布局,View中背景图片的加载是最经常做的.但是我们很少关注这个过程,这篇文章主要解析view中背景图片加载的流程.了解view中背景图 ...

随机推荐

  1. 使用mybatis插入自增主键ID的数据后返回自增的ID

    在开发中碰到用户注册的功能需要用到用户ID,但是用户ID是数据库自增生成的,这种情况上网查询后使用下面的方式配置mybatis的insert语句可以解决: <insert id="in ...

  2. nxlog4go 按天或按文件大小分割日志

    Building a new rotate file writer: rfw := l4g.NewRotateFileWriter("_rfw.log").SetMaxSize(1 ...

  3. vim操作备忘录

    vim操作备忘录 vim 备忘录 vim的书籍虽然看不不少,可是老是容易忘记,主要是自己操作总结过少,这个博客就主要用来记录一些比较常见的术语和操作,以防止自己再次忘记. <leader> ...

  4. nginx虚拟域名的配置以及测试验证

    1.保证该机器上安装了nginx 未安装请看:centos/linux下的安装Nginx 2.使用root用户编辑配置文件 vim /usr/local/nginx/conf/nginx.conf 3 ...

  5. 《android开发艺术探索》读书笔记(十)--Android的消息机制

    接上篇<android开发艺术探索>读书笔记(九)--四大组件 No1: 消息队列MessageQueue的内部存储结构并不是真正的队列,而是采用单链表的数据结构来存储消息列表,因为单链表 ...

  6. Mybatis认识

    MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis .iB ...

  7. R-CNN算法概要

    参考论文:Rich feature hierarchies for accurate object detection and semantic segmentation 下载地址:https://a ...

  8. 5.3 存储器、I/O和配置读写请求TLP

    本节讲述PCIe总线定义的各类TLP,并详细介绍这些TLP的格式.在这些TLP中,有些格式对于初学者来说较难理解.读者需要建立PCIe总线中与TLP相关的一些基本概念,特别是存储器读写相关的报文格式. ...

  9. l【linux】linux rpm包命名规范

    RPM包的一般格式为:name-version-arch.rpmname-version-arch.src.rpm name:软件包名称.version:带有主.次和修订的软件包版本.arch:硬件平 ...

  10. freemarker自定义标签报错(六)

    freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered "\"\u4f60\u597d\uff01\& ...