启动画面不会隐藏 – 使用Microsoft.VisualBasic库
我有两种forms。 Form1(下面的代码)和Splash(只是测试的默认forms)。
我的问题是,在应用程序运行后,Splash不会隐藏。 主窗体已加载但Splash仍未关闭。
Form1代码:
using System; using System.Collections.Generic; using System.Windows.Forms; using Microsoft.VisualBasic.ApplicationServices; namespace WindowsFormsApplication2 { class Program : WindowsFormsApplicationBase { /// /// The main entry point for the application. /// [STAThread] static void Main(string[] args) { //Application.EnableVisualStyles(); //Application.SetCompatibleTextRenderingDefault(false); //Application.Run(new Form1()); // Show Form in Single-instance mode var prg = new Program(); prg.EnableVisualStyles = true; prg.IsSingleInstance = true; prg.MinimumSplashScreenDisplayTime = 1000; prg.SplashScreen = new Splash(); prg.MainForm = new Form1(); prg.Run(args); } } }
您必须添加对Microsoft.VisualBasic的引用才能实现此目的。
启动表单代码:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Splash : Form { public Splash() { InitializeComponent(); } } }
预先感谢您的帮助。
你是否正在使用Visual Basic Appplication Framework来运行启动画面? 试试这个。 这是一个快速的表单应用程序 – 请注意,我已将所有名称和命名空间保留为默认值,因此您可能需要为代码更改此名称。 该项目仅有两种forms。 Form2是启动画面。 我在其上嵌入了一个背景图像,以确保它弹出正常,我可以将它与Form1区分开来。
我在我的项目中添加了对.NET Microsoft.VisualBasic的引用。
这是来自program.cs文件
using System; using System.Collections.Generic; using System.Linq; 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 MyApp().Run(args); } } public class MyApp : WindowsFormsApplicationBase { protected override void OnCreateSplashScreen() { this.SplashScreen = new Form2(); } protected override void OnCreateMainForm() { // Do your initialization here //... System.Threading.Thread.Sleep(5000); // Test // Then create the main form, the splash screen will automatically close this.MainForm = new Form1(); } } }
我知道这与你正在使用的不同,但似乎有效。