C# .NET - How to show splash screen... - Asked By Mark Fix on 24-Feb-09 01:02 AM

Hi Friend's...

Can anyone say how to use splash dialogue and when i run my windows application first i want to run a splash screen for 2min and after it should hide and Show another window form.....


Tks....

Mark's....

try this - Sat Sat replied to Mark Fix on 24-Feb-09 01:24 AM

http://www.codeproject.com/KB/cs/prettygoodsplashscreen.aspx
Santhosh N replied to Mark Fix on 24-Feb-09 01:37 AM
check these with different ways of creating splash screens in c#...

http://www.vcskicks.com/splash-screen.php

http://www.codersource.net/csharp_splash_screen.aspx

try this - C_A P replied to Mark Fix on 24-Feb-09 05:57 AM

Getting to work:

   Let's start by creating a new C# Windows Application ( File -> New -> Project -> Visual C# Projects -> Windows Application ). Name the project SplashScreen.

http://www.codersource.net/csharp_splash_screen.aspx#. Name the class Splash.cs and click OK.

   Visual Studio .NET has created another form for you. Let's work a little on our new form. Let's set it's FormBorderStyle to None its StartPosition to CenterScreen. In the attached archive, I've created a simple image witch we will set as the BackgoundImage for our current form. Resize the form until it displays the full image. Now set the TransparencyKey property of our form to the background color of our image. As you can see, the color is Black. Also, it would be nice that this form would not be displayed in the taskbar. So set the ShowInTaskbar property to false.

   Now go back to Form1 class, created by default by Visual Studio .NET. We must add some code in this class to show the Splash form before initializing Form1.

   Let's go to Form1() method. That method is the constructor of the class and it executes before any other method. Let's add here the following code, right bellow the call to InitializeComponent method:

Thread th = new Thread(new ThreadStart(DoSplash));
th.Start();
Thread.Sleep(3000);
th.Abort();
Thread.Sleep(1000);

   Let's see what is done here. Fist, we declare a thread and tell him that when he starts, to execute the DoSplash method. The next line starts the thread. Now the started thread executes in parallel with the main application thread. To prevent the main form from appearing before the thread exits, we put the main thread to sleep for 3 seconds( 3000 milliseconds ).To be sure that the second thread will exit, we’ll leave him an extra 1 second to cleanup. . Let’s take a look at the DoSplash method that our second thread executes:

Splash sp = new Splash();
sp.ShowDialog();


   Let's see what is happening here. We declared an instance of our Splash class and displayed it. When we'll call the abort method of the thread, all the variable declared or instantiated in that thread will be disposed.

   Run the application now and see what happens. On my http://www.codersource.net/csharp_splash_screen.aspx#, the image is displayed correctly only if I set the color depth of http://www.codersource.net/csharp_splash_screen.aspx# to 16 bits. This is a bug from Microsoft. After a few searches on google, you will find there is a fix for that. So let's fix it.

   Go to the Splash class constructor, and add the following code, after the InitializeComponent method:

Bitmap b = new Bitmap(this.BackgroundImage);
b.MakeTransparent(b.GetPixel(1,1));
this.BackgroundImage = b;
 

   What our code does is get the image we set as BackgroundImage and creates a Bitmap object as a copy of the same image. Next, we call MakeTransparent method of the Bitmap class that gets a color as an argument and makes it transparent. The argument that we pass is the color of the pixel at coordinates 1,1(these coordinates are not the only ones that you can use. You can use as well 0,0 and any other.).

multithreaded splash screen - C_A P replied to Mark Fix on 24-Feb-09 05:59 AM
When you run you app .net creates main thread and loads specified (main) form. To conceal hard work you can hide main form until loading is done.

Assuming that Form1 - is your main form and SplashForm is top level, borderles nice splash form:

private void Form1_Load(object sender, EventArgs e)
{
   
Hide();
   
bool done = false;
   
ThreadPool.QueueUserWorkItem((x) =>
   
{
       
using (var splashForm = new SplashForm())
       
{
            splashForm
.Show();
           
while (!done)
               
Application.DoEvents();
            splashForm
.Close();
       
}
   
});

   
Thread.Sleep(3000); // Emulate hardwork
   
done = true;
   
Show();
}
splash screen while loading .net assemblies - C_A P replied to Mark Fix on 24-Feb-09 06:02 AM
You could write an event handler for AppDomain.AssemblyLoad().  A generic example:

  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      AppDomain.CurrentDomain.AssemblyLoad += new AssemblyLoadEventHandler(ShowAssemblies);
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new Form1());
    }
    private static void ShowAssemblies(object sender, AssemblyLoadEventArgs e)
    {
      Console.WriteLine(e.LoadedAssembly.FullName);
    }
  }

Turn off the Visual Studio hosting process to see this work in the debugger.
splash screen - C_A P replied to Mark Fix on 24-Feb-09 06:03 AM

To show the splash form when the application is loading, whatever method you used to create the form, you need to set a few properties and add a few lines of code.

First, select Project | Properties... Select the General Tab, and change the Startup Object drop down to Sub Main.

Now, select Project | Add Module . Select Module from the list of items. Note, if you already have a module in your project, then you do not need to add a new one.

Then insert the following code into that new module:

Sub Main()
    ' Show the splash form
    frmSplash.Show
    ' Load the main form
    Load frmMain
    ' Load any other forms you may have that you need
    ' Show the main form
    frmMain.Show
    ' Remove the splash form from the memory
    Unload frmSplash
End Sub

If the splash screen does not appear for long enough, that is because your program is too small! To get round this, you can add a timer to the splash screen so that it will appear for x number of seconds. Add the control, name it tmrShow, set its Interval property to 2000 (2 second) and add the following code to the splash form:

Private Sub tmrShow_Timer()
    tmrShow.Enabled = False
    ResumeStartup 'resume the startup
    Unload Me 'unload the form (hide it)
End Sub

Then, modify the module startup code:

Sub Main()
    ' Show the splash form
    frmSplash.Show 'as the form loads, the timer will start
End Sub

Sub ResumeStartUp()
   ' Load the main form
    Load frmMain
    ' Load any other forms you may have that you need
    ' Show the main form
    frmMain.Show
End Sub