2014-02-08 92 views
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(); 

这会启动外部应用程序,但不会隐藏它。 我再读取该命令PROMT可以隐藏应用程序:

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

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

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

问候

+1

由于禁用窗口时隐藏应用程序不规范的行为,似乎不太可能任何人都可以帮助你不了解具体的应用程序。 –

回答

0

我把它做的工作如下:

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; 
     } 
    } 
} 
相关问题