12

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

4

3 に答える 3

17

それはハッキーで恐ろしいですが、私にとってはうまくいきます(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));
        }
    }
}
于 2008-10-17T19:40:48.173 に答える
16

これが私がすることです。

[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()); 
}
于 2012-08-22T04:36:51.757 に答える
-4

(少なくとも) 2 つのモニターを入手し、セカンダリ モニターで VisualStudio を開きます。VisualStudio 内からアプリを実行すると、既定でプライマリ モニターで起動します。これは最後に開かれるアプリであるため、最初から開始され、VisualStudio に切り替えても影響はありません。とにかく私のために働く。

まだ 2 番目のモニターを持っていない場合は、IMHO を使用する必要があります。

于 2008-10-17T20:01:31.987 に答える