.NET Splash屏幕问题

时间:2010-12-26 06:33:48

标签: c# .net splash-screen

我的C#数据库应用程序有一个启动画面,通过Shown事件调用。启动画面包含一些在调用主窗体构造函数时预处理的信息,因此我使用了已显示的事件,因为该信息应该可用。

但是,当显示启动画面时,主窗体会变白,菜单栏,底部菜单栏,甚至灰色背景都是白色和不可见的。看起来程序正在挂起,但是在我内置的5秒延迟之后,横幅消失了,程序正常显示。此外,在横幅上,我有标签,当启动画面显示时没有显示...

这是我的代码,为什么它不起作用的一些推理会有很大的帮助。

SPLASH SCREEN CODE:

public partial class StartupBanner : Form
{
    public StartupBanner(int missingNum, int expiredNum)
    {
        InitializeComponent();
        missingLabel.Text = missingNum.ToString() + " MISSING POLICIES";
        expiredLabel.Text = expiredNum.ToString() + " EXPIRED POLICIES";
    }
}

致电代码:

    private void MainForm_Shown(object sender, EventArgs e)
    {
        StartupBanner startup = new StartupBanner(missingPoliciesNum, expiredPoliciesNum);
        startup.MdiParent = this;
        startup.Show();

        Thread.Sleep(5000);
        startup.Close();
    }

使用startup.ShowDialog()在启动屏幕上显示正确的标签信息,但这会锁定应用程序,我需要在大约5秒后消失,这就是为什么它会引起轰动。 ;)

4 个答案:

答案 0 :(得分:6)

首先使用 ShowDialog()而不是Show()运行启动画面,因此启动画面会锁定主窗体并且不会锁定主线程:

private void MainForm_Shown(object sender, EventArgs e)
{
    StartupBanner startup = new StartupBanner(missingPoliciesNum, expiredPoliciesNum);
    startup.ShowDialog();
}

在初始屏幕中,您应该定义一个在5秒后关闭表单的计时器:

public partial class StartupBanner : Form
{
    private System.Windows.Forms.Timer _closeTimer = new Timer();

    public StartupBanner(int missingNum, int expiredNum)
    {
        this.InitializeComponent();
        missingLabel.Text = missingNum.ToString() + " MISSING POLICIES";
        expiredLabel.Text = expiredNum.ToString() + " EXPIRED POLICIES";

        this._closeTimer = new System.Windows.Forms.Timer();
        this._closeTimer.Interval = 5000;
        this._closeTimer.Tick += new EventHandler(this._closeTimer_Tick);
        this._closeTimer.Start();
    }

    private void _closeTimer_Tick(object sender, EventArgs e)
    {
        System.Windows.Forms.Timer timer = (System.Windows.Forms.Timer)sender;
        timer.Stop();

        this.Close();
    }
}

编辑:Thread.Sleep()锁定整个线程,例如表单的每个操作,以便他们无法处理任何消息,如点击或按下按钮。它们不在后台运行,因此最好使用可以在后台关闭表单的计时器。

FIX:已删除 startup.MdiParent = this;

答案 1 :(得分:2)

这是Windows的一项功能,旨在帮助用户应对无响应的程序。当您显示启动画面时,Windows会向您的主窗体发送一条消息,告诉它它不再是活动窗口。这通常会导致它使用非活动窗口的颜色重绘窗口标题。同样的消息也会触发Form.Deactivated事件。

但是这不再起作用了,你的主线程忙着执行代码而不是空闲来“抽取消息循环”。 Windows注意到这一点,消息未送达。几秒钟后,它会用“鬼窗口”替换你的窗口。它与主窗口具有相同的大小和边框,但没有内容,只有白色背景。并且标题为“未响应”。足以让用户知道尝试使用窗口是行不通的。

通过使用真实的启动画面来避免这种情况,支持already built into .NET

答案 2 :(得分:0)

在你的代码中,MainForm_Shown等待'Thread.Sleep(5000);',所以它变白了,因为主线程睡眠并且无法获得任何消息。 StartupBanner表格也有一些原因。我建议你使用Thread来避免子函数或子线程拦截主线程,并使MainForm失效。

答案 3 :(得分:-1)

.Net中有对内置闪屏的内置支持

使用API​​的最佳方法是

  SplashScreen splash = new SplashScreen("splashscreen.jpg");
  splash.Show(false);
  splash.Close(TimeSpan.FromMilliseconds(2));
  InitializeComponent();
相关问题