【问题标题】:Splash screen doesn't hide - using Microsoft.VisualBasic library启动画面不隐藏 - 使用 Microsoft.VisualBasic 库
【发布时间】:2010-07-15 15:57:17
【问题描述】:

我有 2 个表格。 Form1(下面有代码)和 Splash(只是测试的默认表单)。

我的问题是应用程序运行后,Splash 并没有隐藏。主窗体已加载,但 Splash 仍未关闭。

Form1代码:

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using Microsoft.VisualBasic.ApplicationServices;

namespace WindowsFormsApplication2
{
    class Program : WindowsFormsApplicationBase 
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [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 的引用才能执行此操作。

Splash表单代码:

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();
        }
    }
}

提前感谢您的帮助。

【问题讨论】:

    标签: c# winforms


    【解决方案1】:

    啊,您正在使用 Visual Basic 应用程序框架来运行启动画面吗? 尝试这个。 这是来自快速表单应用程序 - 请注意,我已将所有名称和命名空间保留为默认值,因此您可能需要为您的代码更改此设置。该项目只有两种形式。 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();
            }
        }
    }
    

    我知道这与您使用的不同,但它似乎有效。

    【讨论】:

    • 非常感谢,你给我指引了一条好路。我刚刚添加到您的代码中: public MyApp() { this.IsSingleInstance = true;加上覆盖 OnStartupNextInstance ,现在我拥有了我需要的一切。单实例应用程序和启动画面。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多