我参考下面的文章写了一个SplashScreen:
http://www.codeproject.com/csharp/PrettyGoodSplashScreen.asp
为了简化问题,我的主要代码如下:SplashScreen.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;namespace SplashScreen
{
    public partial class SplashScreen : Form
    {
        static SplashScreen ms_frmSplash = null;
        static Thread ms_oThread = null;        static public void ShowSplashScreen()
        {
            // Make sure it is only launched once.
            if (ms_frmSplash != null)
                return;
            ms_oThread = new Thread(new ThreadStart(SplashScreen.ShowForm));
            ms_oThread.Start();
        }        static private void ShowForm()
        {
            ms_frmSplash = new SplashScreen();
            Application.Run(ms_frmSplash);
        }        static public void CloseForm()
        {
            ms_frmSplash.Close();
        }
        
        public SplashScreen()
        {
            InitializeComponent();            this.ClientSize = this.BackgroundImage.Size;
        }
    }
}Program.cs:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Threading;
namespace SplashScreen
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            //Application.Run(new SplashScreen());
            SplashScreen.ShowSplashScreen();
            Thread.Sleep(3000);
            SplashScreen.CloseForm();            Form1 form1 = new Form1();
            form1.TopMost = true;
            form1.ShowDialog();        }
    }
}无论怎样设置form1的TopMost属性,都无法将form1显示在最前端,而且似乎不仅仅是TopMost属性的问题,当SplashScreen窗口关闭后根本看不到form1窗口,form1窗口隐藏到了当前窗口(例如VisualStudio开发窗口)的后面,一定要最小化当前的窗口才能看到form1窗口。如果我把Program.cs中显示SplashScreen的三行代码去掉,form1就正常了。请大家帮忙看看问题在哪?

解决方案 »

  1.   

    gei ni ding yi xia
      

  2.   

    因為你的Form1 是用的ShowDialog,試試ShowApplication.Run() 都沒寫?如何運行滴
      

  3.   

    你在ShowSplashScreen方法调用ShowForm,在ShowForm中使用了Application.Run(ms_frmSplash);这句Application.Run(ms_frmSplash)会阻止程序,在关闭splash之前,程序是运行不到下一句代码的,所以你使用了线程,但是这样本来挺简单的问题,你变的很乱了.不知道你为什么要这样来做.
      

  4.   

    对 Application.Run都没有 怎么跑起来的
      

  5.   

    SplashScreen.cs中的代码是从文章:
    http://www.codeproject.com/csharp/PrettyGoodSplashScreen.asp
    中copy出来的,原文中需要在SplashScreen中使用进度条等控件,并在主程序中更新这些控件,所以开了一个新的线程。开线程是把程序变复杂了,但我认为并没有引入错误,如果需要更新SplashScreen中的控件,开线程就是必须的。我是为了简化问题,所以在这里没有使用任何控件。由于SplashScreen窗口是在一个独立的线程中运行的,所以Application.Run(ms_frmSplash)是在一个独立的线程中运行的,主线程在调用了SplashScreen.ShowSplashScreen()之后仍然可以继续运行。现在的问题是如何将form1显示到前面来,由于需要使用form1返回的DialogResult(虽然我在这里贴出的代码中没有使用),所以我使用了form1.ShowDialog();希望大家能在VisualStudio中建一个项目,把上面的代码贴进去试试,看看问题到底出在哪里,谢谢了。 :)
      

  6.   

    你应该把Windows应用程序这一块的常见任务再好好看一下,在没有必要的情况下,最好把问题简化,从跟本入手,不必直接抄别人的代码,不一定合适,意见只供参考!
      

  7.   

    简单的代码如下:SplashScreen.cs:
    using...
    namespace SplashScreen
    {
        public partial class SplashScreen : Form
        {
            static SplashScreen ms_frmSplash = null;        static public void ShowForm()
            {
                ms_frmSplash = new SplashScreen();
                ms_frmSplash.Show();
            }        static public void CloseForm()
            {
                ms_frmSplash.Close();
            }
            
            public SplashScreen()
            {
                InitializeComponent();            this.ClientSize = this.BackgroundImage.Size;
            }
        }
    }
    Program.cs:
    using ...
    namespace SplashScreen
    {
        static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);            SplashScreen.ShowForm();
                Thread.Sleep(3000);
                SplashScreen.CloseForm();            Form1 form1 = new Form1();
                form1.ShowDialog();
            }
        }
    }的确,上述代码不使用线程,可以很好的运行,form1的TopMost属性没有任何问题。但是,如果我需要在一个新的线程中启动SplashScreen该如何写呢? :)
      

  8.   

    对于界面上的编程,不是不得已,最好不把界面或控件在其它的线程操作,这样会带来不必要的麻烦.觉得你完全可以不使用线程而达到相同的效果.当然在其它的线程中是可以操作任何一个控件的,不过是通过Invoke要转到建立控件的线程中去执行.还是那句话,没有必要的话,不要这样做.
      

  9.   

    to 但是,如果我需要在一个新的线程中启动SplashScreen该如何写呢? :)你可以参考我的一篇文章
    如何弹出一个模式窗口来显示进度条
    http://blog.csdn.net/knight94/archive/2006/05/27/757351.aspx