1. 程式人生 > >c#窗體歡迎界面

c#窗體歡迎界面

初始 perf 耗時 public only 加載 isnull contain graph

調用

            WYL.OrderWater.ServerSide.SplashScreen.Splasher.Show(typeof(WYL.OrderWater.ServerSide.SplashScreen.frmSplash));
            System.Threading.Thread.Sleep(800);
            
            System.Threading.Thread.Sleep(800);

            //.......此處加載耗時的代碼

            Splasher.Status = "初始化完畢!
"; System.Threading.Thread.Sleep(800); Splasher.Close();

1.

Splasher.cs

using System;
using System.Windows.Forms;
using System.Threading;
using System.Reflection;

namespace WYL.OrderWater.ServerSide.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 WYL.OrderWater.ServerSide.SplashScreen
{
    /// <summary>
    /// interface for Splash Screen
    /// </summary>
    public interface ISplashForm
    {
        void SetStatusInfo(string NewStatusInfo);
    }
}

3.窗體代碼

frmSplash.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WYL.OrderWater.ServerSide.SplashScreen
{
    public partial class frmSplash : Form,ISplashForm
    {
        public frmSplash()
        {
            InitializeComponent();
        }

        #region ISplashForm

        void ISplashForm.SetStatusInfo(string NewStatusInfo)
        {
            lbStatusInfo.Text = NewStatusInfo;
        }

        #endregion

        private void frmSplash_Load(object sender, EventArgs e)
        {

        }
    }
}

frmSplash.designer.cs

namespace WYL.OrderWater.ServerSide.SplashScreen
{
    partial class frmSplash
    {/// <summary>
        /// 必需的設計器變量。
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// 清理所有正在使用的資源。
        /// </summary>
        /// <param name="disposing">如果應釋放托管資源,為 true;否則為 false。</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows 窗體設計器生成的代碼

        /// <summary>
        /// 設計器支持所需的方法 - 不要
        /// 使用代碼編輯器修改此方法的內容。
        /// </summary>
        private void InitializeComponent()
        {
            this.lbStatusInfo = new System.Windows.Forms.Label();
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // lbStatusInfo
            // 
            this.lbStatusInfo.AutoSize = true;
            this.lbStatusInfo.BackColor = System.Drawing.Color.Transparent;
            this.lbStatusInfo.Font = new System.Drawing.Font("宋體", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.lbStatusInfo.ForeColor = System.Drawing.Color.White;
            this.lbStatusInfo.Location = new System.Drawing.Point(148, 395);
            this.lbStatusInfo.Name = "lbStatusInfo";
            this.lbStatusInfo.Size = new System.Drawing.Size(164, 12);
            this.lbStatusInfo.TabIndex = 0;
            this.lbStatusInfo.Text = "正在初始化應用程序......";
            // 
            // label1
            // 
            this.label1.AutoSize = true;
            this.label1.Font = new System.Drawing.Font("宋體", 26.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label1.ForeColor = System.Drawing.SystemColors.HighlightText;
            this.label1.Location = new System.Drawing.Point(144, 18);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(159, 35);
            this.label1.TabIndex = 1;
            this.label1.Text = "銀龍軟件";
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.Font = new System.Drawing.Font("宋體", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.label2.ForeColor = System.Drawing.SystemColors.ScrollBar;
            this.label2.Location = new System.Drawing.Point(50, 536);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(161, 28);
            this.label2.TabIndex = 2;
            this.label2.Text = "(C) 2017 YinLongSoft。\r\n    保留版權所有。";
            // 
            // frmSplash
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.ActiveCaptionText;
            this.ClientSize = new System.Drawing.Size(450, 600);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.lbStatusInfo);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Name = "frmSplash";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "frmSplash";
            this.TopMost = true;
            this.Load += new System.EventHandler(this.frmSplash_Load);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Label lbStatusInfo;
        private System.Windows.Forms.Label label1;
        private System.Windows.Forms.Label label2;
    }
}

frmSplash.resx

c#窗體歡迎界面