C# Winform欢迎窗体实现()

方法一、program.cs 中先启动欢迎窗体,然后注册程序运行空闲去执行主程序窗体相应初始化代码

 1  static void Main(string[] args)
 2         { 
 3             Application.EnableVisualStyles();                       //样式设置
 4             Application.SetCompatibleTextRenderingDefault(false);   //样式设置
 5 
 6             
 7              UIForm.WelcomeForm sp = new UIForm.WelcomeForm();       //启动窗体
 8              sp.Show();                                              //显示启动窗体
 9              context = new ApplicationContext();
10              context.Tag = sp;
11              Application.Idle += new EventHandler(Application_Idle); //注册程序运行空闲去执行主程序窗体相应初始化代码
12              Application.Run(context); 
13         }
View Code

 

 1     //初始化等待处理函数
 2         private static void Application_Idle(object sender, EventArgs e)
 3         {
 4 
 5             Application.Idle -= new EventHandler(Application_Idle);
 6             if (context.MainForm == null)
 7             {
 8                 MainForm mw = new MainForm(_args);
 9                 context.MainForm = mw;
10                 mw.InitForm();                                  //主窗体要做的初始化事情在这里,该方法在主窗体里应该申明为public
11                 UIForm.WelcomeForm sp = (UIForm.WelcomeForm)context.Tag;
12                 sp.Close();                                 //关闭启动窗体 
13                 mw.Show();                                  //启动主程序窗体
14             }
15 
16 
17         }
View Code

 

方法二、实现一个Splasher类,由主窗体操作欢迎窗体(受益于大神:键盘敲击者cncxz http://www.cnblogs.com/cncxz/archive/2006/07/14/450987.html)

首先创建一个名为ISplashForm的接口,该接口包含一个名为SetStatusInfo的方法以用来在启动屏幕上设置系统加载状态,代码如下:

1  public interface ISplashForm
2     {
3         void SetStatusInfo(string statusInfo);
4     }

 然后实现一个Splasher类,负责欢迎窗体的Show、Close和Status更新,代码如下:

 1   public class Splasher
 2     {
 3         private static Form splashForm = null;
 4         private static ISplashForm splashInterface = null;
 5         private static System.Threading.Thread splashThread = null;
 6         private static string tempStatus = string.Empty;
 7 
 8         public static void Show(Type splashFormType)
 9         {
10             if (splashThread != null)
11             {
12                 return;
13             }
14             if (splashFormType == null)
15             {
16                 throw new Exception("必须设置启动窗体");
17             }
18             splashThread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate()
19             {
20                 createInstance(splashFormType);
21                 Application.Run(splashForm);
22             }));
23             splashThread.IsBackground = true;
24             splashThread.SetApartmentState(System.Threading.ApartmentState.STA);
25             splashThread.Start();
26         }
27         public static void Close()
28         {
29             if (splashThread == null || splashForm == null) return;
30             try
31             {
32                 splashForm.Invoke(new MethodInvoker(splashForm.Close));
33             }
34             catch (Exception ex)
35             {
36                 Controller.MessageConsole.WriteLog(ex);
37                 HttpProxy.LogProxy.DoTask(ex, Controller.SQLiteController.Biz.GetAuthor());
38             }
39             splashThread = null;
40             splashForm = null;
41         }
42         public static string Status
43         {
44             set
45             {
46                 {
47                     if (splashInterface == null || splashForm == null)
48                     {
49                         tempStatus = value;
50                         return;
51                     }
52                     splashForm.Invoke(
53                             new splashStatusChangedHandle(delegate(string str) { splashInterface.SetStatusInfo(str); }),
54                             new object[] { value }
55                         );
56                 }
57             }
58         }
59         private static void createInstance(Type FormType)
60         {
61             object obj = FormType.InvokeMember(null, System.Reflection.BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.CreateInstance, null, null, null);
62             splashForm = obj as Form;
63             splashInterface = obj as ISplashForm;
64             if (splashForm == null)
65             {
66                 throw new Exception("启动窗体类型必须是System.Windows.Forms.Form的子类");
67             }
68             if (splashInterface == null)
69             {
70                 throw new Exception("启动窗体必须实现ISplashForm接口");
71             }
72             if (!string.IsNullOrEmpty(tempStatus))
73             {
74                 splashInterface.SetStatusInfo(tempStatus);
75             }
76         }
77         private delegate void splashStatusChangedHandle(string NewStatusInfo);
78     }
View Code

欢迎窗体代码如下:

 1  public partial class WelcomeForm : DevExpress.XtraEditors.XtraForm, ISplashForm
 2     {
 3         public WelcomeForm()
 4         {
 5             InitializeComponent();
 6             this.FormBorderStyle = FormBorderStyle.None;//取消窗体边框
 7             this.ShowInTaskbar = false;//取消任务栏图标显示
 8         }
 9 
10         public void SetStatusInfo(string statusInfo)
11         {
12             this.lbInfo.Text = statusInfo;
13         }
14     }
View Code

主窗体需要初始化的地方调用代码如下:

           Splasher.Status = "正在初始化窗体...";
            InitializeComponent();
            //一系列初始化操作。。。
            //为Splasher.Status复制实现消息通知
            Splasher.Close();

program.cs Main函数中实现代码如下:

1    UI.UIForm.Splasher.Show(typeof(UIForm.WelcomeForm));
2    Application.Run(new MainForm(args));
View Code

 

posted @ 2015-10-17 12:44  AllanHao  阅读(2077)  评论(0编辑  收藏  举报