using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; namespace SplashSample ...{ /**////<summary> /// Summary description for Splash. ///</summary> publicclass Splash : System.Windows.Forms.Form ...{ private System.Windows.Forms.Label label1; /**////<summary> /// Required designer variable. ///</summary> private System.ComponentModel.Container components =null; public Splash() ...{ // // Required for Windows Form Designer support // InitializeComponent(); // // TODO: Add any constructor code after InitializeComponent call // } /**////<summary> /// Clean up any resources being used. ///</summary> protectedoverridevoid Dispose( bool disposing ) ...{ if( disposing ) ...{ if(components !=null) ...{ components.Dispose(); } } base.Dispose( disposing ); } Windows Form Designer generated code#region Windows Form Designer generated code /**////<summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///</summary> privatevoid InitializeComponent() ...{ this.label1 =new System.Windows.Forms.Label(); this.SuspendLayout(); // // label1 // this.label1.Font =new System.Drawing.Font("Microsoft Sans Serif", 20F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.label1.ForeColor = System.Drawing.Color.Blue; this.label1.Location =new System.Drawing.Point(8, 24); this.label1.Name ="label1"; this.label1.Size =new System.Drawing.Size(280, 80); this.label1.TabIndex =0; this.label1.Text ="Splash Screen"; this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter; // // Splash // this.AutoScaleBaseSize =new System.Drawing.Size(5, 13); this.ClientSize =new System.Drawing.Size(292, 125); this.Controls.Add(this.label1); this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; this.Name ="Splash"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text ="Splash"; this.ResumeLayout(false); } #endregion } }
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace SplashSample ...{ publicclass SplashAppContext : ApplicationContext ...{ Form mainForm =null; Timer splashTimer =new Timer(); public SplashAppContext(Form mainForm, Form splashForm) : base(splashForm) ...{ this.mainForm = mainForm; splashTimer.Tick +=new EventHandler(SplashTimeUp); splashTimer.Interval =2000; splashTimer.Enabled =true; } /**////<summary> /// This is the timer event handler. We use the timer to tell the ApplicationContext /// that the splash screen is ready to be closed. ///</summary> ///<param name="sender"></param> ///<param name="e"></param> privatevoid SplashTimeUp(object sender, EventArgs e) ...{ //This is called if the splash is ready to be closed. In order to do this //we first dispose of the timer (we dont need any leaks do we?) and then //call the base.MainForm.Close function which will triger the MainFormClosed event //that we overrode so we can set the Application Context's main form as the //main form the user passed into the constructor. splashTimer.Enabled =false; splashTimer.Dispose(); base.MainForm.Close(); } /**////<summary> /// Normaly, if not overridden, this event will call ExitThreadCore function. /// We have overridden it to catch when the splash form has closed. When the /// spash form closes, we want to set the main form passed in the contructor to /// the base.MainForm property. when the main form closes, this override will be /// called again, and we just pass the call onto the base.OnMainFormClosed, with will /// tells the Application object to terminate the UI thread. ///</summary> ///<param name="sender"></param> ///<param name="e"></param> protectedoverridevoid OnMainFormClosed(object sender, EventArgs e) ...{ if (sender is Splash) ...{ base.MainForm =this.mainForm; base.MainForm.Show(); } elseif (sender is Form1) ...{ base.OnMainFormClosed(sender, e); } } /**////<summary> /// This sets how long the spash form will show once it is at 100% opacity. Default is 2 seconds. ///</summary> publicint SecondsSplashShown ...{ set ...{ splashTimer.Interval = value *1000; } } } publicclass SplashFadeAppContext : ApplicationContext ...{ /**////<summary> /// Internal flags to tell process what state the splash form is in ///</summary> privateenum FormStatus ...{ Open =1, Opening =2, Closing =4, Closed =8 } private FormStatus formStatus = FormStatus.Open; private Form mainForm =null; private Timer splashTimer =new Timer(); privateint showSplashInterval =2000; privateint fadeInterval =50; privatebool doFadeClose =false; public SplashFadeAppContext(Form mainForm, Form splashForm) : base(splashForm) ...{ this.mainForm = mainForm; splashTimer.Tick +=new EventHandler(SplashTimeUp); splashTimer.Interval = fadeInterval; splashTimer.Enabled =true; } /**////<summary> /// This is the timer event that controls what our spash screen does. ///</summary> ///<param name="sender"></param> ///<param name="e"></param> privatevoid SplashTimeUp(object sender, EventArgs e) ...{ if (formStatus == FormStatus.Opening) ...{ //if the splash is opening, the opacity will increase in increments of 5 until it is fully //shown. Once it is fully opacic, it sets the status flag to open. if (base.MainForm.Opacity <1) base.MainForm.Opacity += .05; else formStatus = FormStatus.Open; } elseif (formStatus == FormStatus.Closing) ...{ //if the splash is closing, the opacity will decrease in increments of 5 until it is fully //hidden. Once it is fully transparent, it sets the status flag to closed. if (base.MainForm.Opacity > .10) ...{ base.MainForm.Opacity -= .05; splashTimer.Interval = fadeInterval; } else ...{ formStatus = FormStatus.Closed; } } elseif (formStatus == FormStatus.Open) ...{ //Once the splash is open and fully shown, the timer interval is set to the splash delay setting, //which is defaulted to 2 seconds, and then sets the status flag depending on if the user //wants to just close the splash or fade it out. splashTimer.Interval = showSplashInterval; if (doFadeClose) formStatus = FormStatus.Closing; else formStatus = FormStatus.Closed; } elseif(formStatus == FormStatus.Closed) ...{ //This is called if the splash is ready to be closed. In order to do this //we first dispose of the timer (we dont need any leaks do we?) and then //call the base.MainForm.Close function which will triger the MainFormClosed event //that we overrode so we can set the Application Context's main form as the //main form the user passed into the constructor. splashTimer.Enabled =false; splashTimer.Dispose(); base.MainForm.Close(); } } /**////<summary> /// Normaly, if not overridden, this event will call ExitThreadCore function. /// We have overridden it to catch when the splash form has closed. When the /// spash form closes, we want to set the main form passed in the contructor to /// the base.MainForm property. when the main form closes, this override will be /// called again, and we just pass the call onto the base.OnMainFormClosed, with will /// tells the Application object to terminate the UI thread. ///</summary> ///<param name="sender"></param> ///<param name="e"></param> protectedoverridevoid OnMainFormClosed(object sender, EventArgs e) ...{ if (sender is Splash) ...{ base.MainForm =this.mainForm; base.MainForm.Show(); } elseif (sender is Form1) ...{ base.OnMainFormClosed(sender, e); } } Public Properties#region Public Properties /**////<summary> /// Bool that determines if spash form will fade up from 0% opacity to 100% when /// the spash form opens. Default is false. ///</summary> publicbool DoFadeOpen ...{ set ...{ if (value ==true) ...{ base.MainForm.Opacity =0; formStatus = FormStatus.Opening; } } } /**////<summary> /// Bool that determines if spash form will fade down from 100% opacity to 0% when /// the spash form closes. Default is false. ///</summary> publicbool DoFadeClose ...{ set ...{ doFadeClose = value; } } /**////<summary> /// This sets how long the spash form will show once it is at 100% opacity. Default is 2 seconds. ///</summary> publicint SecondsSplashShown ...{ set ...{ showSplashInterval = value *1000; } } #endregion Public Properties } }
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; namespace SplashSample ...{ /**////<summary> /// Summary description for Form1. ///</summary> publicclass Form1 : System.Windows.Forms.Form ...{ /**////<summary> /// Required designer variable. ///</summary> private System.ComponentModel.Container components =null; public Form1() ...{ // // Required for Windows Form Designer support // InitializeComponent(); } /**////<summary> /// Clean up any resources being used. ///</summary> protectedoverridevoid Dispose( bool disposing ) ...{ if( disposing ) ...{ if (components !=null) ...{ components.Dispose(); } } base.Dispose( disposing ); } Windows Form Designer generated code#region Windows Form Designer generated code /**////<summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. ///</summary> privatevoid InitializeComponent() ...{ // // Form1 // this.AutoScaleBaseSize =new System.Drawing.Size(5, 13); this.ClientSize =new System.Drawing.Size(292, 273); this.Name ="Form1"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text ="Form1"; } #endregion /**////<summary> /// The main entry point for the application. ///</summary> [STAThread] staticvoid Main() ...{ //SplashAppContext splashContext = new SplashAppContext(new Form1(), new Splash()); SplashFadeAppContext splashContext =new SplashFadeAppContext(new Form1(), new Splash()); splashContext.DoFadeOpen =true; splashContext.DoFadeClose =true; splashContext.SecondsSplashShown =1; Application.Run(splashContext); } } }