C # Add a startup screen for the winform Program

Source: Internet
Author: User
C # Add a startup screen for the winform program 2010-03-27 from: csdnblog
  • Abstract: A startup screen is displayed when many software is started. How does the C # winform program implement the startup screen? This article introduces how to add a startup screen for the C # winform program and provides complete sample code for your reference.

     

If the program needs to be processed for a long time during loading, it is best to use the startup screen. On the one hand, the program is beautified, and on the other hand, the user does not face a blank interface.
When I started the main interface of a small project on hand, I needed to check whether the user file and runtime environment were valid. It took some time to process it. So I wanted to add a startup screen and searched the internet for a while, the following two solutions are found:

1. Use C # To add a startup screen to the program and allow only one application instance to run
Http://www.zahui.com/html/14/36790.htm
2. How to: splash screen, also called the program startup screen (. net2003)
Http://lzmtw.cnblogs.com/archive/2005/10/31/265782.html

The first scheme does not do well in implementation and interface separation. The startup interface (a form) depends on a specific form, and A preload method must be added to the main form to complete the loading task, it can only be reused at the code level. And that only allows one instance to be written too ....

The second solution has a good framework, but there may be some problems with minor processing. You need to judge the windowstate of the main form, and the entire code is also complicated.

I changed the basic structure to the second solution.

Function: adds a startup interface for the program. The main form is loaded when the startup interface is displayed. After the main form is loaded, the startup interface is closed and the main form is displayed. The duration of the Start Screen is the maximum value of the set time and the time required for loading the main form. The startup screen runs on another thread.
 

The program code is as follows:

// Start the form virtual base class, inherited from applicationcontext
Using system. Windows. forms;
Using system. Threading;
Using system;

// The virtual base class of the startup screen. The startup screen remains for a period of time, which is the maximum value of the set time and the time required for the construction of the main form.
Public abstract class splashscreenapplicationcontext: applicationcontext
{
Private form _ splashscreenform; // start the form
Private form _ primaryform; // main form
Private system. Timers. Timer _ splashscreentimer;
Private int _ splashscreentimerinterval = 5000; // The default value is that the startup form is displayed for 5 seconds.
Private bool _ bsplashscreenclosed = false;
Private delegate void disposedelegate (); // close the delegate. The following describes how to use the invoke method of the control. This method requires this delegate.

Public splashscreenapplicationcontext ()
{
This. showsplashscreen (); // create and display the startup form here
This. mainformload (); // The main startup form is created and displayed here.
}

Protected abstract void oncreatesplashscreenform ();

Protected abstract void oncreatemainform ();

Protected abstract void setseconds ();

Protected form splashscreenform
{
Set
{
This. _ splashscreenform = value;
}
}

Protected form primaryform
{// Override the oncreatemainform method in the derived class and call the oncreatemainform method in the mainformload Method
//, The constructor of form1 (main form) will be called here, that is, the constructor of the main form will be called after the display of the form is started.
// To avoid this situation: the construction of the main form takes a long time, and the screen does not respond for a long time.
Set
{
This. _ primaryform = value;
}
}

Protected int secondsshow
{// Use the default time when the stop time of the startup screen is not set
Set
{
If (value! = 0)
{
This. _ splashscreentimerinterval = 1000 * value;
}
}
}

Private void showsplashscreen ()
{
This. setseconds ();
This. oncreatesplashscreenform ();
This. _ splashscreentimer = new system. Timers. Timer (double) (This. _ splashscreentimerinterval )));
_ Splashscreentimer. elapsed + = new system. Timers. elapsedeventhandler (new system. Timers. elapsedeventhandler (this. splashscreendisplaytimeup ));

This. _ splashscreentimer. autoreset = false;
Thread displayspashscreenthread = new thread (New threadstart (displaysplashscreen ));

Displayspashscreenthread. Start ();
}

Private void displaysplashscreen ()
{
This. _ splashscreentimer. Enabled = true;
Application. Run (this. _ splashscreenform );
}

Private void splashscreendisplaytimeup (Object sender, system. Timers. elapsedeventargs E)
{
This. _ splashscreentimer. Dispose ();
This. _ splashscreentimer = NULL;
This. _ bsplashscreenclosed = true;
}

Private void mainformload ()
{
This. oncreatemainform ();

While (! (This. _ bsplashscreenclosed ))
{
Application. doevents ();
}

Disposedelegate splashscreenformdisposedelegate = new disposedelegate (this. _ splashscreenform. Dispose );
This. _ splashscreenform. Invoke (splashscreenformdisposedelegate );
This. _ splashscreenform = NULL;


// It must be displayed first and then activated. Otherwise, the main form cannot appear after the startup form disappears.
This. _ primaryform. Show ();
This. _ primaryform. Activate ();

This. _ primaryform. Closed + = new eventhandler (_ primaryform_closed );

}

Private void _ primaryform_closed (Object sender, eventargs E)
{
Base. exitthread ();
}
}

Usage: defines a startup class. An application starts from the startup class. This class uses a startup form class that inherits the virtual base class of the self-starting form to define the startup form and main form in this class. The code for starting the form and main form is omitted. Note that you should delete the main method section of the form code generated by the machine.

Public class startupclass
{
[Stathread]
Static void main ()
{
Application. Run (New mycontext ());
}
}

// Start the Form class (inheriting the virtual base class of the self-starting form). the Start Screen remains for a period of time, which is the maximum value of the set time and the time required for the construction of the main form.
Public class mycontext: splashscreenapplicationcontext
{
Protected override void oncreatesplashscreenform ()
{
This. splashscreenform = new formstart (); // start the form
}

Protected override void oncreatemainform ()
{
This. primaryform = new formmain (); // main form
}

Protected override void setseconds ()
{
This. secondsshow = 2; // The time (in seconds) when the form is started)
}
}

The above section describes how to add a startup screen for the C # winform program. I hope it will help you. Author: linux7985

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.