在保持活动状态的同时隐藏另一个应用程序 [英] Hide another application while keeping it active

查看:67
本文介绍了在保持活动状态的同时隐藏另一个应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个外部应用程序,它在运行时生成一些数据并存储它(它是一个窗口应用程序 - 不是控制台应用程序).现在我正在创建自己的应用程序来分析这些数据.问题是外部软件必须同时运行.

I'm having an external application that generates some data and stores it, when it runs (it is a window application - not a console application). Now I'm in the process of creating my own application that analyzes this data. The problem is that the external software has to run at the same time.

当用户打开我的应用程序时,我希望它自动启动外部应用程序并隐藏它.我在这个主题上搜索了很多,并尝试了我找到的一些建议.首先我试过:

When the user opens my application I want it to automatically start the external application and hide it. I've searched a lot on the topic and tried some of the suggestions I found. First I tried:

Process p = new Process();
p.StartInfo.FileName = @"c:\app.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WorkingDirectory = @"c:\";
p.StartInfo.CreateNoWindow = true;
p.Start();

这会启动外部应用程序,但不会隐藏它.然后我读到命令提示符可以隐藏应用程序:

This starts the external application, but doesn't hide it. I then read that the command promt could hide the application:

ProcessStartInfo psi = new ProcessStartInfo("cmd.exe", "/c \""c:\app.exe\"");
psi.UseShellExecute = false;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process.Start(psi);

同样,这会启动非隐藏的应用程序.

Again, this starts the application non-hidden.

然后我想到了启动应用程序然后将其隐藏.我发现了以下内容:

I then thought of starting the application and then hide it. I found the following:

[DllImport("user32.dll")]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

var Handle = FindWindow(null, "Application Caption");
ShowWindow(Handle, 0);

这将隐藏窗口.问题是应用程序在此状态下处于非活动状态,并且不会生成任何数据.

This will hide the window. The problem is that the application is inactive in this state and doesn't generate any data.

我离可接受的解决方案又近了一步(最小化而不是隐藏).由于外部应用程序启动有点慢,我执行以下操作:

I've come a step closer to an acceptable solution (minimizing instead of hiding). As the external application is a little slow starting up, I do the following:

Process p = new Process();
p.StartInfo.FileName = @"c:\app.exe";
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.UseShellExecute = false;
p.StartInfo.WorkingDirectory = @"c:\";
p.StartInfo.CreateNoWindow = true;
p.Start();

while (true)
{
    int style = GetWindowLong(psi.MainWindowHandle, -16); // -16 = GWL_STYLE

    if ((style & 0x10000000) != 0) // 0x10000000 = WS_VISIBLE
    {
        ShowWindow(psi.MainWindowHandle, 0x06);
        break;
    }

    Thread.Sleep(200);          
}

这也行不通,但我相信这是朝着正确方向迈出的一步.

This doesn't work either, but I believe it's a step in the right direction.

有没有办法启动和隐藏外部应用程序,同时保持其处于活动状态?

Is there a way to start and hide an external application, while keeping it active?

最好的问候

推荐答案

我做了以下工作:

const int GWL_STYLE = -16;
const long WS_VISIBLE = 0x10000000;

while (true)
{
    var handle = FindWindow(null, "Application Caption");

    if (handle == IntPtr.Zero)
    {
        Thread.Sleep(200);
    }
    else
    {
        int style = GetWindowLong(handle, GWL_STYLE);

        if ((style & WS_VISIBLE) != 0)
        {
            ShowWindow(handle, 0x06);
            break;
        }
    }
}

这篇关于在保持活动状态的同时隐藏另一个应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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