且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

C#控制台应用程序将流程置于前台

更新时间:2023-02-08 18:49:11

完成任务的一种选择是将shift + Tab发送到窗口以将其设置在所有内容之前(我在其他应用程序中尝试了不同的方式,但只有这对我有用):

An option to achieve your task is to send shift + tab to the window to set it in front of everything (i tried in another application different ways, but only this worked for me):

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