zoukankan      html  css  js  c++  java
  • Winform加载loading界面

    WinForm实现Loading等待界面
    
     
    
    1,LoaderForm窗体中添加PictureBox,然后添加Loading图片
    
    2,窗体内属性设置
    
    StartPosition :CenterScreen在屏幕中心显示
    
    TopMost:True置顶显示
    
    ShowInTaskbar:False不在任务栏显示
    
    FormBorderStyle:None不显示窗体边框和标题栏
    
    TransparencyKey:Control颜色为Control的部分透明
    
    BackColor:Control窗体背景颜色设为Control
    
     
    
    3,调用:
    
    LoadingHelper.ShowLoadingScreen();//显示
    LoadingHelper.CloseForm();//关闭
    
     
    
     
    
    4,显示效果如下:
    
    
    5,LoaderForm和LoadingHelper部分代码如下,ref:https://www.cnblogs.com/morewindows0/p/7107599.html
    
    LoaderForm:
    
     public partial class LoaderForm : Form
        {
            public LoaderForm()
            {
                InitializeComponent();
            }
     
            /// <summary>
            /// 关闭命令
            /// </summary>
            public void closeOrder()
            {
                if (this.InvokeRequired)
                {
                    //这里利用委托进行窗体的操作,避免跨线程调用时抛异常,后面给出具体定义
                    CONSTANTDEFINE.SetUISomeInfo UIinfo = new CONSTANTDEFINE.SetUISomeInfo(new Action(() =>
                    {
                        while (!this.IsHandleCreated)
                        {
                            ;
                        }
                        if (this.IsDisposed)
                            return;
                        if (!this.IsDisposed)
                        {
                            this.Dispose();
                        }
     
                    }));
                    this.Invoke(UIinfo);
                }
                else
                {
                    if (this.IsDisposed)
                        return;
                    if (!this.IsDisposed)
                    {
                        this.Dispose();
                    }
                }
            }
     
            private void LoaderForm_FormClosing(object sender, FormClosingEventArgs e)
            {
                if (!this.IsDisposed)
                {
                    this.Dispose(true);
                }
     
            }
        }
        class CONSTANTDEFINE
        {
            public delegate void SetUISomeInfo();
        }
    LoadingHelper:
    
      public class LoadingHelper
        {
             #region 相关变量定义
           /// <summary>
           /// 定义委托进行窗口关闭
           /// </summary>
           private delegate void CloseDelegate();
           private static LoaderForm loadingForm;
           private static readonly Object syncLock = new Object();  //加锁使用
     
            #endregion
     
            //private LoadingHelper()
            //{
     
            //}
     
            /// <summary>
            /// 显示loading框
            /// </summary>
            public static void ShowLoadingScreen()
            {
                // Make sure it is only launched once.
                if (loadingForm != null)
                    return;
                Thread thread = new Thread(new ThreadStart(LoadingHelper.ShowForm));
                thread.IsBackground = true;
                thread.SetApartmentState(ApartmentState.STA);
                thread.Start();
     
            }
     
            /// <summary>
            /// 显示窗口
            /// </summary>
            private static void ShowForm()
            {
                if (loadingForm != null)
                {
                    loadingForm.closeOrder();
                    loadingForm = null;
                }
                loadingForm = new LoaderForm();
                loadingForm.TopMost = true;
                loadingForm.ShowDialog();
            }
     
            /// <summary>
            /// 关闭窗口
            /// </summary>
            public static void CloseForm()
            {
                Thread.Sleep(50); //可能到这里线程还未起来,所以进行延时,可以确保线程起来,彻底关闭窗口
                if (loadingForm != null)
                {
                    lock (syncLock)
                    {
                        Thread.Sleep(50);  
                        if (loadingForm != null)
                        {
                            Thread.Sleep(50);  //通过三次延时,确保可以彻底关闭窗口
                            loadingForm.Invoke(new CloseDelegate(LoadingHelper.CloseFormInternal));
                        }
                    }
                }
            }
     
            /// <summary>
            /// 关闭窗口,委托中使用
            /// </summary>
            private static void CloseFormInternal()
            {
     
                loadingForm.closeOrder();
                loadingForm = null;
     
            }
     
        }
     
    

      

    对于不可控的事情,保持乐观; 对于可控的事情,保持谨慎
  • 相关阅读:
    操作系统进程通信
    操作系统进程调度
    java中的变量
    java移位运算符
    String, StringBuffer, StringBuilder 的区别
    多线程相关问题汇总
    java内存管理与GC机制(二)
    java内存管理与GC机制(一)
    进程与线程的理解
    Liferay7使用maven引入第三方jar包
  • 原文地址:https://www.cnblogs.com/baylor2019/p/13100592.html
Copyright © 2011-2022 走看看