SetForegroundWindow()在现有的控制台窗口中不起作用

时间:2018-10-26 09:33:23

标签: c# console-application user32 bringtofront setforegroundwindow

以下代码仅在我运行控制台窗口的新实例时有效。例如,当我从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);
}

有什么方法可以使它正常工作吗?

0 个答案:

没有答案