WinForm实现Loading等待界面

转自https://blog.csdn.net/qq_36240878/article/details/84024369

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)
                    {
                        Program.Form.Activate(); //你的加载完之后的界面要先设置激活,不然会导致焦点跑到其他的界面上,其他的界面会显示出来
                        this.Dispose();
                    }
 
                }));
                this.Invoke(UIinfo);
            }
            else
            {
                if (this.IsDisposed)
                    return;
                if (!this.IsDisposed)
                {
                    Program.Form.Activate();
                    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;
 
        }
 
    }

 

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
WinForm是Windows Forms的缩写,是微软公司开发的基于Windows操作系统的桌面应用程序开发框架。在WinForm实现选项设置界面可以使用多种控件来完成,例如:Checkbox、RadioButton、ComboBox、TextBox等等。下面我会介绍一种比较常用的实现方式。 首先,在Visual Studio中创建一个WinForm项目,然后在界面上拖放需要的控件,例如Checkbox、RadioButton、ComboBox等等。然后,你可以通过代码来设置这些控件的属性,例如: ```csharp checkBox1.Text = "启用自动更新"; checkBox1.Checked = true; radioButton1.Text = "开启自动保存"; radioButton2.Text = "关闭自动保存"; radioButton1.Checked = true; comboBox1.Items.Add("英文"); comboBox1.Items.Add("中文"); comboBox1.SelectedIndex = 0; ``` 在保存选项时,可以使用配置文件或者注册表来保存选项。例如,使用配置文件保存选项的代码示例: ```csharp //保存选项 Properties.Settings.Default.AutoUpdate = checkBox1.Checked; Properties.Settings.Default.AutoSave = radioButton1.Checked; Properties.Settings.Default.Language = comboBox1.SelectedItem.ToString(); Properties.Settings.Default.Save(); ``` 在读取选项时,可以在窗体加载时从配置文件或者注册表中读取选项,例如: ```csharp //读取选项 checkBox1.Checked = Properties.Settings.Default.AutoUpdate; radioButton1.Checked = Properties.Settings.Default.AutoSave; comboBox1.SelectedItem = Properties.Settings.Default.Language; ``` 通过这种方式,就可以很方便地实现选项设置界面了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值