开发者

Display Image at project startup - program.cs?

开发者 https://www.devze.com 2023-04-07 08:33 出处:网络
I have a small Windows Forms project and now Iam looking to display an image at project startup, I mean Program.cs

I have a small Windows Forms project and now Iam looking to display an image at project startup, I mean Program.cs

Is it possible?

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Image MyPrgImage = Image.FromFile("C:\\开发者_如何学GoTemp\\Images\\For_Network.gif");
            ??????

            Application.Run(new Form1());
        }


Sure... Add new WindowsForm to your project, call it SplashImageForm. Add PictureBox control to it, and add the image you want in it. Resize the form, set these SplashImageForm properties:

FormBorderStyle - None
ShowInTaskBar - false
StartPosition - CenterScreen

Then you want to show that form before Form1 and close it after the timeout has expired... Like so for example:

static void Main()
{
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);

    SplashImageForm f = new SplashImageForm();

    f.Shown += new EventHandler((o,e)=>{
        System.Threading.Thread t = new System.Threading.Thread(() =>
            {
                System.Threading.Thread.Sleep(2000);
                f.Invoke(new Action(() => { f.Close(); }));

            });
            t.IsBackground = true;
            t.Start();
    });

    Application.Run(f);
    Application.Run(new Form1());
}

EDIT Now, there is new thread which blocks on System.Threading.Thread.Sleep(2000) for 2 seconds, and the main thread is allowed to block on Application.Run(f) as it is supposed to, until the SplashImageForm isn't closed. So the image gets loaded by the main thread and the GUI is responsive.

When the timeout ends, Invoke() method is called so the main thread which is the owner of the form closes it. If this wasn't here, Cross threaded exception would be thrown.

Now the image is shown for 2 secs, and after it Form1 is shown.


You mean a splash screen, right?
Consider adding a reference to Microsoft.VisualBasic (if not already done) and then set the WindowsFormsApplicationBase.SplashScreen property.

A few more points:

  • Windows Forms doesn't have support for a simple and straight splash screen.
    Even the solution above will take a few seconds until the .net framework is loaded to show the splash screen.
  • See this question here for further examples and important remarks.
  • See this CodeProject.com sample for a custom solution


You would need a simple form, perhaps with a PictureBox, to loadd and display the image. Then remove it once your main form is loaded.


Simply add a windows form(let the name of form be imgsplash) & from option set following:-

FormBorderStyle - None
ShowInTaskBar - false
StartPosition - CenterScreen

in this form set background image[image which you want to show at startup of application]

--now in program.cs add folloing steps:-

       static void Main()
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        imgsplash f = new imgsplash();
        f.Show();
        System.Threading.Thread.Sleep(2000);
        f.Close();
        Application.Run(new Form1());
    }
0

精彩评论

暂无评论...
验证码 换一张
取 消