How can I bring a console application window to front in C# (especially when running the Visual Studio debugger)?
+6
A:
It's hacky, it's horrible, but it works for me (thanks, 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));
}
}
}
Jon Skeet
2008-10-17 19:40:48
Please don't ever add this to your code just to get it to appear on top when debugging.
tvanfosson
2009-02-05 14:31:41
@tv - Maybe you could give an explanation to your absolute statement. Personally I think this is a legitimate and recommended use of window management functions. It's not the best DRY approach but with a little refactoring it's a common practice in win32 application development. It's not hackey, it's how you move windows around the desktop.
Marcus Pope
2010-07-08 16:15:54
@Jon Skeet - just to add, if you were looking to run this code only when in the visual studio debugger you can use the following: if (Debugger.IsAttached == true) { ... }
Marcus Pope
2010-07-08 16:32:33
@Marcus: Or preferably without the "== true" bit ;)
Jon Skeet
2010-07-08 16:44:29
+1
A:
Get two monitors (at least) and open VisualStudio in the secondary monitor. When you run your app from within VisualStudio it will start up by default on the primary monitor. Since it's the last app to be opened, it starts on top and changing over to VisualStudio doesn't affect it. Works for me anyway.
If you don't already have a second monitor, IMHO, you should.
tvanfosson
2008-10-17 20:01:31
If the issue is how do I get the application window on top when my app is started from the debugger, using Alt-Tab is a better solution than hacking your app just to make the window pop up when debugging.
tvanfosson
2009-02-05 14:31:00
To send Alt+Tab from C# you would simply use SendKeys.SendWait("%{TAB}");However unlike the referenced code above - this is in fact the hackey approach because your sendkey message could be intercepted by any foreground window.Even if you explicitly set the foreground window, you still risk a race condition where another form pops up between execution of each line. This is why direct window messaging or their designated user32 api calls are the proper method.I believe tvanfosson was suggesting that you just press Alt-Tab manually, but that's not germane to the question.
Marcus Pope
2010-07-08 16:28:30