显示启动图片

Afn*_*hir 6 c# visual-studio-2010

当我的应用程序启动时,我们怎样才能显示启动图片,因为每个软件如Photoshop,vs word等?我计划将它粘贴在表格上,然后显示它,但有顶部蓝色条,有控制等任何想法/

Cod*_*ray 9

如果您正在寻找最简单的方法,您可以使用.NET Framework对启动屏幕的出色内置支持.你不得不抛开任何非理性的恐惧,你可能会在C#应用程序中包含一些名为"Visual Basic"的东西,但这样可以避免你不得不推出自己的自定义解决方案并担心像线程,调用等等.无论如何,这一切都归结为同样的IL.以下是它的工作原理:

  1. 添加Microsoft.VisualBasic对项目的引用.

  2. 添加一个新表单(类似名称SplashForm)作为启动画面.

  3. 要使其看起来更像正确的启动画面,请将窗体的FormBorderStyle属性设置为"无",将其StartPosition属性设置为"CenterScreen".您可以将任何控件或图像添加到此表单,以便在启动屏幕上显示此表单.

  4. 将以下代码添加到您的Project.cs文件中:

    using System;
    using System.Windows.Forms;
    using Microsoft.VisualBasic.ApplicationServices;
    
    namespace WindowsFormsApplication1
    {
       static class Program
       {
          [STAThread]
          static void Main(string[] args)
          {
             Application.EnableVisualStyles();
             Application.SetCompatibleTextRenderingDefault(false);
             new SplashScreenApp().Run(args);
          }
       }
    
       public class SplashScreenApp : WindowsFormsApplicationBase
       {
          protected override void OnCreateSplashScreen()
          {
             this.SplashScreen = new SplashForm();
             this.SplashScreen.ShowInTaskbar = false;
             this.SplashScreen.Cursor = Cursors.AppStarting;
          }
    
             protected override void OnCreateMainForm()
             {
                 //Perform any tasks you want before your application starts
    
                 //FOR TESTING PURPOSES ONLY (remove once you've added your code)
                 System.Threading.Thread.Sleep(2000);
    
                //Set the main form to a new instance of your form
                //(this will automatically close the splash screen)
                this.MainForm = new Form1();
              }
           }
        }
    
    Run Code Online (Sandbox Code Playgroud)

如果你想做一些像 Adobe Photoshop 一样创建透明闪屏的想法,可以将alpha通道PNG图像添加到项目的Resources文件中,然后将以下代码添加到启动画面窗体中,替换splashImage为嵌入式图像资源的路径:

protected override void OnPaintBackground(PaintEventArgs pevent)
{
    Graphics g = pevent.Graphics;
    g.DrawImage(splashImage, new Rectangle(0, 0, this.Width, this.Height));
}

protected override void OnPaint(PaintEventArgs e)
{
    //Do nothing here
}
Run Code Online (Sandbox Code Playgroud)

为此,请确保关闭双缓冲,否则您将获得表单的黑色背景.无论如何,没有理由双重缓冲启动画面.

  • @comcme:我没有建议使用Visual Basic,代码确实是用C#编写的.但是,我建议添加对"Microsoft.VisualBasic"的引用,其中包含"Visual Basic"字样.该命名空间中的所有函数都是*也是用C#编写的,正如我所提到的,无论如何它都会编译成相同的IL.关键是,该命名空间中提供的`WindowsFormsApplicationBase`类已经免费为您提供了所有的启动画面业务,从而使您无需编写大量额外代码.这只是一个笑话,C#开发者不应该对这个名字感到不满. (2认同)
  • +1科迪,你是男人!太棒了.为什么MS不为SplashScreen类的winforms添加支持,我不知道.但这很完美.我向你倾诉我 (2认同)

Lor*_*enz 2

您可以通过this.FormBorderStyle = FormBorderStyle.None在您的Form_Load().

因此,如果我是你,我会创建一个特定大小的表单,然后在设计器生成的代码中Form_Load() 或直接在设计器生成的代码中设置以下内容:

this.FormBorderStyle = FormBorderStyle.None;
this.StartPosition = FormStartPostition.CenterScreen;
Run Code Online (Sandbox Code Playgroud)

现在你有一个像许多其他应用程序一样的启动屏幕 - 你所要做的就是编写所有使其可见或不可见的代码等:)