使用以下 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();
}
最佳答案
完成您的任务的一个选项是将 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");
}
}
关于C# 控制台应用程序将进程带到前台,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46974192/