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;
 
        }
 
    }
 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM