在项目启动时显示图像 - program.cs?

Par*_*amu 2 c# winforms

我有一个小的Windows窗体项目,现在我希望在项目启动时显示图像,我的意思是Program.cs

可能吗?

static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Image MyPrgImage = Image.FromFile("C:\\Temp\\Images\\For_Network.gif");
            ??????

            Application.Run(new Form1());
        }
Run Code Online (Sandbox Code Playgroud)

Cip*_*ipi 6

当然......添加新WindowsForm项目,调用它SplashImageForm.添加对其的PictureBox控制,并在其中添加所需的图像.调整表单大小,设置以下SplashImageForm属性:

FormBorderStyle - None
ShowInTaskBar - false
StartPosition - CenterScreen
Run Code Online (Sandbox Code Playgroud)

然后你想在Form1之前显示该表单并在超时到期后关闭它...例如:

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());
}
Run Code Online (Sandbox Code Playgroud)

编辑 现在,有一个新的线程阻塞System.Threading.Thread.Sleep(2000)2秒,并且允许主线程Application.Run(f)按预期阻塞,直到SplashImageForm没有关闭.因此,图像由主线程加载,GUI响应.

当超时结束时,Invoke()调用方法,因此作为表单所有者的主线程将关闭它.如果不在这里,将抛出交叉线程异常.

现在图像显示2秒,然后显示Form1.