以下代码仅在我运行控制台窗口的新实例时才有效。例如,当我从 VS 运行代码或双击 exe 文件时,它可以工作。但是,当我从已打开的 CMD 运行 exe 时,它不起作用。
/// <summary>
/// Activates and displays the window. If the window is minimized or
/// maximized, the system restores it to its original size and position.
/// </summary>
private const int SW_RESTORE = 9;
[DllImport("User32.dll")]
private static extern bool SetForegroundWindow(IntPtr handle);
[DllImport("User32.dll")]
private static extern bool ShowWindow(IntPtr handle, int nCmdShow);
[DllImport("User32.dll")]
private static extern bool IsIconic(IntPtr handle);
public static void BringToFront(IntPtr handle)
{
if (IsIconic(handle))
{
ShowWindow(handle, SW_RESTORE);
}
SetForegroundWindow(handle);
}
有没有办法让它工作,还是不可能?