bring a console window to front in c#

后端 未结 3 1905
名媛妹妹
名媛妹妹 2020-12-03 05:50

How can I bring a console application window to front in C# (especially when running the Visual Studio debugger)?

3条回答
  •  [愿得一人]
    2020-12-03 06:16

    This is what I would do.

    [DllImport("kernel32.dll", ExactSpelling = true)]
    public static extern IntPtr GetConsoleWindow();
    
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool SetForegroundWindow(IntPtr hWnd);
    
    public void BringConsoleToFront()
    {
        SetForegroundWindow(GetConsoleWindow()); 
    }
    

提交回复
热议问题