• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
bobird的学习笔记
博客园    首页    新随笔    联系   管理    订阅  订阅
SplashForm

1.Splasher.cs

using System;

using System.Windows.Forms;

using System.Threading;

using System.Reflection;

 

namespace SplashScreen

{

    public class Splasher

    {

        private static Form m_SplashForm = null;

        private static ISplashForm m_SplashInterface = null;

        private static Thread m_SplashThread = null;

        private static string m_TempStatus = string.Empty;

 

        /// <summary>

        /// Show the SplashForm

        /// </summary>

        public static void Show(Type splashFormType)

        {

            if (m_SplashThread != null)

                return;

            if (splashFormType == null)

            {

                throw (new Exception("splashFormType is null"));

            }

 

            m_SplashThread = new Thread(new ThreadStart(delegate()

            {

                CreateInstance(splashFormType);

                Application.Run(m_SplashForm);

            }));

 

            m_SplashThread.IsBackground = true;

            m_SplashThread.SetApartmentState(ApartmentState.STA);

            m_SplashThread.Start();

        }

 

 

 

        /// <summary>

        /// set the loading Status

        /// </summary>

        public static string Status

        {

            set

            {

                if (m_SplashInterface == null || m_SplashForm == null)

                {

                    m_TempStatus = value;

                    return;

                }

                m_SplashForm.Invoke(

                        new SplashStatusChangedHandle(delegate(string str) { m_SplashInterface.SetStatusInfo(str); }),

                        new object[] { value }

                    );

            }

 

        }

 

        /// <summary>

        /// Colse the SplashForm

        /// </summary>

        public static void Close()

        {

            if (m_SplashThread == null || m_SplashForm == null) return;

 

            try

            {

                m_SplashForm.Invoke(new MethodInvoker(m_SplashForm.Close));

            }

            catch (Exception)

            {

            }

            m_SplashThread = null;

            m_SplashForm = null;

        }

 

        private static void CreateInstance(Type FormType)

        {

 

            object obj = FormType.InvokeMember(null,

                                BindingFlags.DeclaredOnly |

                                BindingFlags.Public | BindingFlags.NonPublic |

                                BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);

            m_SplashForm = obj as Form;

            m_SplashInterface = obj as ISplashForm;

            if (m_SplashForm == null)

            {

                throw (new Exception("Splash Screen must inherit from System.Windows.Forms.Form"));

            }

            if (m_SplashInterface == null)

            {

                throw (new Exception("must implement interface ISplashForm"));

            }

 

            if (!string.IsNullOrEmpty(m_TempStatus))

                m_SplashInterface.SetStatusInfo(m_TempStatus);

        }

 

 

        private delegate void SplashStatusChangedHandle(string NewStatusInfo);

 

    }

}

2.ISplashForm.cs

using System;

using System.Collections.Generic;

using System.Text;

 

namespace SplashScreen

{

    /// <summary>

    /// interface for Splash Screen

    /// </summary>

    public interface ISplashForm

    {

        void SetStatusInfo(string NewStatusInfo);

    }

}

3.SplashForm(自己创建)

该Form必须继承ISplashForm接口,并且实现其方法:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using SplashScreen;

 

namespace TemplateGenerate

{

    public partial class SplashForm : Form,ISplashForm

    {

        public SplashForm()

        {

            InitializeComponent();

        }

        #region ISplashForm

 

        void ISplashForm.SetStatusInfo(string NewStatusInfo)

        {

            lbStatusInfo.Text = NewStatusInfo;//lbStatusInfo为窗体上用来显示信息的Label控件

        }

 

        #endregion

    }

}

现在只要在程序中需要SplashForm的地方写上:

//显示Splash

Splasher.Show(typeof(SplashForm));

 //设置状态:

Splasher.Status = "Status Info... ";

 在需要关闭SplashForm的地方写上:

Splasher.Close();

 即可。如果想让SplashForm弹出后,父窗口隐藏,可以在Splasher.Show()之前Hide()窗体,然后在Splasher.Close()后Show()窗体,然后在Activate()窗体即可。

posted on 2013-09-26 15:46  bobird  阅读(462)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3