質問

C#でコンソールアプリケーションウィンドウを前面に表示するにはどうすればよいですか(特にVisual Studioデバッガーを実行している場合)?

役に立ちましたか?

解決

それはハックですが、恐ろしいですが、私にとってはうまくいきます(ありがとう、 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()); 
}

2つ以上のモニターを取得し、セカンダリモニターでVisualStudioを開きます。 VisualStudio内からアプリを実行すると、デフォルトでプライマリモニターで起動します。開かれるのは最後のアプリなので、一番上から起動し、VisualStudioに切り替えても影響はありません。とにかく私のために働く。

2番目のモニター(IMHO)がまだない場合は、そうする必要があります。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top