C#控制台应用程序将进程带到前台

时间:2017-10-27 11:40:36

标签: c# c#-4.0 console-application

使用以下C#控制台应用程序代码,我可以使用Jenkins在后台运行该过程。但现在我想在前台看到这个过程。我在这做什么错?

[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[System.Runtime.InteropServices.DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);

private void startT32app()
{
    IntPtr handle;
    try
    {
        Console.WriteLine("T32 launching");
        string path = @"C:\T32\bin\windows64\t32mppc.exe";
        string args = @"C:\T32\config.t32";
        ProcessStartInfo procInfo = new ProcessStartInfo(path, args);
        procInfo.CreateNoWindow = false;
        procInfo.UseShellExecute = true;
        procInfo.WindowStyle = ProcessWindowStyle.Normal;

        Process procRun = Process.Start(procInfo);
        handle = procRun.MainWindowHandle;
        SetForegroundWindow(handle);
    }
    catch
    {
        Console.WriteLine("Failed to launch T32");
    }
}
static void Main(string[] args)
{ 
    Program Beginapps = new Program();
    Beginapps.startT32app();        
}

1 个答案:

答案 0 :(得分:1)

实现任务的一个选项是将shift + tab发送到窗口以将其设置在所有内容之前(我在另一个应用程序中尝试了不同的方式,但只有这对我有用):

// is used to set window in front
[DllImport("User32.dll", SetLastError = true)]
static extern void SwitchToThisWindow(IntPtr hWnd, bool fAltTab);

public void startT32app()
{
    IntPtr handle;
    try
    {
        Console.WriteLine("T32 launching");
        string path = @"C:\T32\bin\windows64\t32mppc.exe";
        string args = @"C:\T32\config.t32";
        ProcessStartInfo procInfo = new ProcessStartInfo(path, args);
        procInfo.CreateNoWindow = false;
        procInfo.UseShellExecute = true;
        procInfo.WindowStyle = ProcessWindowStyle.Normal;

        Process procRun = Process.Start(procInfo);
        handle = procRun.MainWindowHandle;
        SwitchToThisWindow(handle, true);
    }
    catch
    {
        Console.WriteLine("Failed to launch T32");
    }
}