前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >winform 使用SplashScreen窗口

winform 使用SplashScreen窗口

作者头像
跟着阿笨一起玩NET
发布2018-09-19 17:01:22
7780
发布2018-09-19 17:01:22
举报

SplashScreen,就是平时我们说的溅射屏幕,任何一个做过客户端程序的coder应该对它都不陌生,因为它能提升用户体验,让软件看上去更美。SplashScreenForm通常进入程序时是打开,主窗体加载完毕后退出。一般来说,SplashScreenForm比较简洁,窗体的内容只是显示程序主题、版权等信息;复杂些的,可以显示主程序的加载项目情况。

  下面是我实现的一个SplashScreen类:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;

namespace SplashScreen
{
    public class SplashScreen
    {
        private static object _obj = new object();

        private static Form _SplashForm = null;

        private static Thread _SplashThread = null;

        private delegate void ChangeFormTextdelegate(string s);

        public static void Show(Type splashFormType)
        {
            if (_SplashThread != null)
                return;
            if (splashFormType == null)
            {
                throw (new Exception());
            }

            _SplashThread = new Thread(new ThreadStart(delegate()
            {
                CreateInstance(splashFormType);
                Application.Run(_SplashForm);
            }));

            _SplashThread.IsBackground = true;
            _SplashThread.SetApartmentState(ApartmentState.STA);
            _SplashThread.Start();
        }

        public static void ChangeTitle(string status)
        {
            ChangeFormTextdelegate de = new ChangeFormTextdelegate(ChangeText);
            _SplashForm.Invoke(de, status);
        }

        public static void Close()
        {
            if (_SplashThread == null || _SplashForm == null) return;

            try
            {
                _SplashForm.Invoke(new MethodInvoker(_SplashForm.Close));
            }
            catch (Exception)
            {
            }
            _SplashThread = null;
            _SplashForm = null;
        }

        private static void ChangeText(string title)
        {
            _SplashForm.Text = title.ToString();
        }

        private static void CreateInstance(Type FormType)
        {
            if (_SplashForm == null)
            {
                lock (_obj)
                {
                    object obj = FormType.InvokeMember(null,
                                        BindingFlags.DeclaredOnly |
                                        BindingFlags.Public | BindingFlags.NonPublic |
                                        BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
                    _SplashForm = obj as Form;
                    _SplashForm.TopMost = true;
                    _SplashForm.ShowInTaskbar = false;
                    _SplashForm.BringToFront();
                    _SplashForm.StartPosition = FormStartPosition.CenterScreen;
                    if (_SplashForm == null)
                    {
                        throw (new Exception());
                    }
                }
            }
        }
    }
}

调用的时候只需要传入你需要作为溅射屏幕的窗体,然后在主窗体加载完毕后调用close方法:

代码语言:javascript
复制
namespace SplashScreen
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            SplashScreen.Show(typeof(SplashForm));
            InitializeComponent();
            Thread.Sleep(1000);
            SplashScreen.ChangeTitle("111");
            Thread.Sleep(1000);
            SplashScreen.ChangeTitle("222");
            Thread.Sleep(1000);
            SplashScreen.ChangeTitle("333");
            Thread.Sleep(1000);
            SplashScreen.ChangeTitle("444");
            SplashScreen.Close();
        }
    }
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2015-02-06 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档