c# - 如何在同一应用程序的所有窗口之上显示一个窗口

标签 c# winforms

我有一个 SplashScreen,它应该显示在应用程序中所有其他窗口的前面。

因为它是一个SplashScreen,所以这不能是模态对话框。相反,这应该通过其他线程显示。

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

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

为了展示它,我正在使用这个调用,从另一个线程调用:

Application.Run(SplashScreenForm);

如果我取消注释 SplashScreenForm.TopMost = true,启动画面将显示在其他窗口的顶部,甚至是属于不同应用程序的窗口的顶部。

如果想知道线程是如何创建的:

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

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

我该怎么做?

最佳答案

类似于:

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 中的冗长操作完成后才关闭它。

关于c# - 如何在同一应用程序的所有窗口之上显示一个窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44590678/

相关文章:

c# - 在 AngularJS $http 中传递日期到 ASP.NET Web Api

c# - 在设置先决条件导入之前无法调用 GetExportedValue

c# - 如何使用 protobuf-net 处理 .proto 文件

javascript - Windows 窗体 Web 浏览器控件和 Javascript 更改的 DOM

c# - 当我将鼠标悬停在组合框项目上时引发事件

winforms - .Net 2.0 Winform 标签工具提示

c# - 具有不同 TimeSpans 的 .NETMF TimerCallback?

c# - CellStyle 意外应用于工作表中的所有单元格 - NPOI?

c# winforms datagridview 添加行

vb.net - 如何卸载 VB.NET 中所有打开的表单?