【问题标题】:Hide another application while keeping it active隐藏另一个应用程序,同时保持它处于活动状态
【发布时间】:2014-02-08 18:56:55
【问题描述】:

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

当用户打开我的应用程序时,我希望它自动启动外部应用程序并隐藏它。我已经搜索了很多关于该主题的内容并尝试了一些我发现的建议。首先我尝试了:

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();

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

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

同样,这会以非隐藏方式启动应用程序。

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

[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);

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

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

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);          
}

这也不起作用,但我相信这是朝着正确方向迈出的一步。

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

最好的问候

【问题讨论】:

  • 由于在窗口隐藏时禁用应用程序不是标准行为,因此在不知道具体应用程序的情况下似乎不太可能有人能够帮助您。

标签: c# process window


【解决方案1】:

我通过执行以下操作使其工作:

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;
        }
    }
}

【讨论】:

    猜你喜欢
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多