如何在同一应用程序的所有窗口上显示窗口

问题描述:

我有一个应用程序中所有其他窗口应该显示的SplashScreen。如何在同一应用程序的所有窗口上显示窗口

由于它是SplashScreen,因此这不能是模态对话框。相反,这应该通过其他线程来表示。

我创建的启动画面是这样的:

  SplashScreenForm = new SplashScreen(mainForm); 
      // SplashScreenForm.TopMost = true; 

,并显示出它,我用这电话,从不同的线程调用:如果我取消SplashScreenForm.TopMost = true

Application.Run(SplashScreenForm); 

,飞溅显示在其他窗口的顶部,即使在属于不同应用程序的窗口顶部也是如此。

如果你想知道如何线程创建:

public void ShowSplashScreen() 
    { 
     SplashScreenThread = new Thread(new ThreadStart(ShowForm)); 
     SplashScreenThread.IsBackground = true; 
     SplashScreenThread.Name = "SplashScreenThread"; 
     SplashScreenThread.Start(); 
    } 

    private static void ShowForm() 
    { 
     Application.Run(SplashScreenForm); 
    } 

我该怎么办呢?

+0

为什么需要从另一个线程显示该对话框? –

+0

@RomanoZumbé,因为它应在主线程加载应用程序时显示(加载设备驱动程序,数据库中的某些列表等)。在加载应用程序时,启动屏幕应显示加载状态。 – jstuardo

+2

听起来有点奇怪,我在gui线程中做了繁重的工作,并把实际的gui元素放到了另一个线程中。你为什么不把你的资源(以及你需要做的)加载到工作线程并从主线程显示你的飞溅屏幕? –

喜欢的东西:

static void Main() 
{ 
    Application.EnableVisualStyles(); 
    Application.SetCompatibleTextRenderingDefault(false); 

    Thread splashThread = new Thread(new ThreadStart(
     delegate 
     { 
      splashForm = new SplashForm(); 
      Application.Run(splashForm); 
     } 
     )); 

    splashThread.SetApartmentState(ApartmentState.STA); 
    splashThread.Start(); 

    // Load main form and do lengthy operations 
    MainForm mainForm = new MainForm(); 
    mainForm.Load += new EventHandler(mainForm_Load); 
    Application.Run(mainForm); 
} 

再后来耗时的操作结束之后:

static void mainForm_Load(object sender, EventArgs e) 
{ 
    if (splashForm == null) 
     return; 
    splashForm.Invoke(new Action(splashForm.Close)); 
    splashForm.Dispose(); 
    splashForm = null; 
} 

这将启动您的启动画面之前您的主要形式,只有在长期操作完成mainForm_Load时才解雇。

+0

精彩的解决方案。那正是我所期待的。 – jstuardo

+0

@jstuardo很高兴能有所帮助。您可能想要提供答案,以便其他人也能找到答案。 – TomServo

你可以尝试调用函数SetForegroundWindow(HWND)WinAPI

[DllImport("user32.dll")] 
[return: MarshalAs(UnmanagedType.Bool)] 
static extern bool SetForegroundWindow(IntPtr hWnd); 

// somewhere in the code : 
SetForegroundWindow(SplashScreenForm.Handle); 
+0

我已经尝试过了,但是,首先显示启动画面,但几乎立即隐藏起来。我认为这是因为主线程仍然在主动加载一些东西。 – jstuardo