当 ShowInTaskbar = false 时,将另一个进程窗口置于前台 [英] Bring another processes Window to foreground when it has ShowInTaskbar = false

查看:26
本文介绍了当 ShowInTaskbar = false 时,将另一个进程窗口置于前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们只希望我们的应用程序在任何时候运行一个实例.因此,在启动时,它会查看应用程序是否正在运行,如果是,它会在主窗口上调用 SetForegroundWindow.

We only want one instance of our app running at any one time. So on start up it looks to see if the app is running and if it is, it calls SetForegroundWindow on the Main Window.

这一切都很好......在大多数情况下......

This is all good and well ... for the most part..

当我们的应用程序启动时,它会显示一个启动画面和一个登录表单.这两种形式都有 ShowInTaskBar = false.

When our app starts up it will show a Splash screen and a Logon form. Both of these forms have ShowInTaskBar = false.

因此,如果您在登录表单显示时尝试启动应用程序的另一个副本,则该登录表单不会出现在前面

Because of this, if you try to start up another copy of the app when the Logon form is showing, that Logon form is not brought to the front!

特别是当用户在任务栏中也看不到任何内容时,他们认为该应用程序很笨拙,无法启动.没有迹象表明有另一个实例正在运行.

Especially as the user cant see anything in the taskbar as well, all they figure is that the app is duff and cannot start. There is no indication that there is another instance running.

有什么办法可以解决这个问题吗?

Is there any way around this problem?

推荐答案

好吧,代码在这里.即使 ShowInTaskBarfalse,你也应该能够把它放在前面.

Well, code is here. Even if the ShowInTaskBar is false, you should be able to bring it to the front.

    [DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
    public static extern IntPtr FindWindow(String lpClassName, String lpWindowName);

    [DllImport("USER32.DLL")]
    public static extern bool SetForegroundWindow(IntPtr hWnd);

    public static void bringToFront(string title) {
        // Get a handle to the Calculator application.
        IntPtr handle = FindWindow(null, title);

        // Verify that Calculator is a running process.
        if (handle == IntPtr.Zero) {
            return;
        }

        // Make Calculator the foreground application
        SetForegroundWindow(handle);
    }

注意:您应该FindWindow 使用表单的类而不是名称,因为启动屏幕表单有时没有标题甚至没有控制框.使用 Spy++ 深入挖掘.

Note: you should FindWindow using the form's class and not by name as the splash screen forms sometimes do not have titles or even the controlbox. Use Spy++ to dig deeper.

在启动时使用 FindWindow.我认为这就是您想要做的 - 在加载主窗体时将启动画面放在前面.

Use FindWindow on splash. I think this is what you want to do - bring the splash screen in front while loading of the main form.

这篇关于当 ShowInTaskbar = false 时,将另一个进程窗口置于前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆