Start-up interface of WinForm interface development

Source: Internet
Author: User

We are in the development of desktop applications, because the program starts relatively slow, often in order to improve the user experience, add a splash screen, that is, splashscreen, the benefits are: 1, let the user see the loading process, improve the interactive response of the program 2. Can briefly display or introduce the function of the program or display the logo, give the customer a deeper impression.

I in the development of the shared software, for the start of the slow program, but also inclined to introduce this control to show, first look at the effect of software startup

The middle of the text "initializing the application ..." can display different content according to the progress of the load, of course, the best concise, other content you can also change as necessary, because this is a form, you want to change what change what.

To see how the splash screen code is used first, let's start with the main function of the portal.

    Static classProgram {/// <summary>        ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false); //Login InterfaceFrmlogin dlg =NewFrmlogin (); Dlg. StartPosition=Formstartposition.centerscreen; if(DialogResult.OK = =dlg. ShowDialog ()) { SplashScreen.Splasher.Show ( typeof (Splashscreen.frmsplashscreen)); Application.ThreadException+=NewSystem.Threading.ThreadExceptionEventHandler (application_threadexception); Application.Run (Newfrmmain ()); }                   }        Private Static voidApplication_threadexception (Objectsender, System.Threading.ThreadExceptionEventArgs ex) {            //Loghelper.error (ex. Exception);            stringMessage =string. Format ("{0}\r\n operation has an error, do you need to exit the system? ", ex.            Exception.Message); if(Dialogresult.yes ==messagebox.show (message,"System Error", Messageboxbuttons.yesno))            {application.exit (); }        }    }

In the above code: SplashScreen.Splasher.Show (typeof (Splashscreen.frmsplash)), mainly to start the splash screen class code,

Where splasher This class uses a background thread, reflection to instantiate the form, the code is as follows:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Windows.Forms;usingSystem.Threading;usingSystem.Reflection;namespacesplashscreen{ Public classSplasher {Private StaticForm M_splashform =NULL; Private StaticIsplashform M_splashinterface =NULL; Private StaticThread M_splashthread =NULL; Private Static stringM_tempstatus =string.        Empty; /// <summary>        ///Show the Splashform/// </summary>         Public Static voidShow (Type splashformtype) {if(M_splashthread! =NULL)                return; if(Splashformtype = =NULL)            {                Throw(NewException ("Splashformtype is null")); } m_splashthread=NewThread (NewThreadStart (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 stringStatus {Set            {                if(M_splashinterface = =NULL|| M_splashform = =NULL) {M_tempstatus=value; return; } m_splashform.invoke (NewSplashstatuschangedhandle (Delegate(stringstr) {m_splashinterface.setstatusinfo (str);}),New Object[] {value}); }        }        /// <summary>        ///Colse the Splashform/// </summary>         Public Static voidClose () {if(M_splashthread = =NULL|| M_splashform = =NULL)return; Try{M_splashform.invoke (NewMethodInvoker (m_splashform.close)); }            Catch(Exception) {} M_splashthread=NULL; M_splashform=NULL; }        Private Static voidCreateInstance (Type formtype) {//creating objects with reflection            Objectobj =Activator.CreateInstance (FormType); M_splashform= obj asForm; M_splashinterface= obj asIsplashform; if(M_splashform = =NULL)            {                Throw(NewException ("Splash screen must inherit from System.Windows.Forms.Form")); }            if(M_splashinterface = =NULL)            {                Throw(NewException ("must implement Interface Isplashform")); }            if(!string.        IsNullOrEmpty (M_tempstatus)) M_splashinterface.setstatusinfo (m_tempstatus); }        Private Delegate voidSplashstatuschangedhandle (stringnewstatusinfo); }}

In order to increase the description of the start-up interface, a text interface is added to the start-up interface, which can be externally modified by the instructions on the boot interface:

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespacesplashscreen{/// <summary>    ///interface for Splash screen/// </summary>     Public InterfaceIsplashform {voidSetstatusinfo (stringnewstatusinfo); }}

Then you inherit the interface on the splash screen and implement the relevant interface code:

usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;namespacesplashscreen{ Public Partial classFrmsplashscreen:form,isplashform { PublicFrmsplashscreen () {InitializeComponent (); }        //implements an interface method, primarily for reflection invocation of an interface        #regionIsplashformvoidIsplashform.setstatusinfo (stringnewstatusinfo) {Lbstatusinfo.text=Newstatusinfo; }        #endregion    }}

Then the program after clicking the "Login" button, you can do a splash screen on the main interface wait, frmmain form code:

namespacesplashscreen{ Public Partial classFrmmain:form { PublicFrmmain () {InitializeComponent (); System.Threading.Thread.Sleep ( -); Splasher.status="is showing the relevant content ..."; System.Threading.Thread.Sleep ( -); //....... Load time-consuming code hereSplasher.status="initialization is complete ......"; System.Threading.Thread.Sleep ( -);        Splasher.close (); }    }}

Start-up interface of WinForm interface development

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.