문제

C# (특히 Visual Studio Debugger를 실행할 때)에서 콘솔 애플리케이션 창을 전면으로 가져 오는 방법은 무엇입니까?

도움이 되었습니까?

해결책

해킹, 끔찍하지만 나에게 효과적입니다 (감사합니다, pinvoke.net!):

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

public class Test 
{

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    [DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)]
    static extern IntPtr FindWindowByCaption(IntPtr zeroOnly, string lpWindowName);

    public static void Main()
    {
        string originalTitle = Console.Title;
        string uniqueTitle = Guid.NewGuid().ToString();
        Console.Title = uniqueTitle;
        Thread.Sleep(50);
        IntPtr handle = FindWindowByCaption(IntPtr.Zero, uniqueTitle);

        if (handle == IntPtr.Zero)
        {
            Console.WriteLine("Oops, cant find main window.");
            return;
        }
        Console.Title = originalTitle;

        while (true)
        {
            Thread.Sleep(3000);
            Console.WriteLine(SetForegroundWindow(handle));
        }
    }
}

다른 팁

이것이 내가 할 일입니다.

[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()); 
}

보조 모니터에서 두 개의 모니터를 구입하고 VisualStudio를 엽니 다. VisureStudio 내에서 앱을 실행하면 기본 모니터에서 기본적으로 시작됩니다. 마지막으로 열리는 앱이기 때문에 맨 위에서 시작하여 VisualStudio로 바뀌는 것은 영향을 미치지 않습니다. 어쨌든 나를 위해 일합니다.

아직 두 번째 모니터가 없다면 IMHO.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top