主窗体在启动画面后不显示在任务栏C#winform

问题描述:

我已经显示了一个启动画面,它工作正常,但主窗体不会显示在任务栏和焦点上。主窗体在启动画面后不显示在任务栏C#winform

有2形式 1.闪屏 2. formMain

在溅射屏幕我添加了一个计时器和下面做一些代码。

public SplashScreen() 
     { 
      InitializeComponent(); 
     } 

     private void splashtimer_Tick(object sender, EventArgs e) 
     { 
      progressBar1.Increment(1); 
      if (progressBar1.Value == 100) splashtimer.Stop(); 
     } 

在主表格 “formMain”

我添加下面的代码。

public formMain() 
    { 
     this.ShowInTaskbar = true; 
     Thread t = new Thread(new ThreadStart(ShowSplashScreen)); 
     t.Start(); 
     Thread.Sleep(5000); 
     InitializeComponent(); 
     t.Abort(); 


    } 

    public void ShowSplashScreen() 
    { 
     Application.Run(new SplashScreen());    
    } 

初始屏幕工作正常,但主要形式不会集中。我从Debug文件夹运行EXE并运行它,启动屏幕显示并且未播下主窗体。任务栏图标未显示。在Ctrl + Tab中显示formMain。为什么???

问题在哪里?

+0

永远不要调用'Thread.Abort()** **除非你试图强制关闭你的整个应用程序。它可以使进程处于未定义的状态。 – Enigmativity

+0

我评论abort()它不会隐藏启动画面 –

+0

是的,但它可能会杀死你的整个应用程序。你应该保留对'ss = new SplashScreen()'的引用并调用ss.Invoke((Action)(()=> ss.Close()));'而不是。 – Enigmativity

闪屏工作正常,但主窗体不会集中。我从Debug文件夹运行EXE 并运行它,显示启动画面,并且未播种主窗体 。任务栏图标未显示。在Ctrl +标签中,formMain显示为 。为什么???

我试图在2015年VS,和我的formMain和闪屏工作正常,但正如你指出,formMain不集中。这可能是因为您在创建并聚焦formMain后使用Application.Run将焦点集中到了SplashScreen。

无论哪种情况,您的方法都不那么干净。试试我下面的方式。这也将解决焦点问题

public partial class SplashScreen : Form { 

    public SplashScreen() { 
     InitializeComponent(); 
     progressBar1.Style = ProgressBarStyle.Marquee; 
    } 

} 

public partial class formMain : Form { 
    public formMain(Form splash) { 
     InitializeComponent(); 

     // make sure to keep yielding to GUI updates, else your progressbar will nto refresh 
     for (int i = 0; i < 100; ++i) { 
      Thread.Sleep(100); // do some work 
      Application.DoEvents(); 
     } 

     splash.Close(); 

    } 
} 

static class Program { 
    [STAThread] 
    static void Main() { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     var frm = new SplashScreen(); 
     frm.Show(); 

     Application.Run(new formMain(frm)); 

    } 
} 

试试这个代码显示启动画面:

private void formMain_Load(object sender, EventArgs e) 
    { 
     EventHandler activated = null; 
     activated = (s2, e2) => 
     { 
      this.Activated -= activated; 
      this.ShowInTaskbar = true; 
      var splash = new SplashScreen(); 
      splash.ShowInTaskbar = false; 
      splash.Show(); 
      var timer = new System.Windows.Forms.Timer(); 
      EventHandler tick = null; 
      tick = (s3, e3) => 
      { 
       timer.Enabled = false; 
       timer.Tick -= tick; 
       timer.Dispose(); 
       splash.Close(); 
       splash.Dispose(); 
      }; 
      timer.Tick += tick; 
      timer.Interval = 5000; 
      timer.Enabled = true; 
     }; 
     this.Activated += activated; 
    } 
+0

我试过这段代码,它可以工作,但是在formMain里面显示的启动画面。我需要首先启动屏幕,然后formMain。 –

+0

我标记为答案,但我的问题尚未解决 –