Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start Debugging from Visual Studio to Second Monitor

When I Start Debugging an application from Visual Studio, it launches the application directly on top of Visual Studio. This imposes a step, albeit small, in my debugging to move the application window to my second monitor, so that I can see the application's state while also watching breakpoints hit. (Sometimes, rarely, I even hit exceptions!)

I have read several posts on SO regarding optimizing Visual Studio for multiple monitors, but most answers revolve around Visual Studio windows, not the debugged application's window. Another answer is specific to console applications. Is there any way to impose the window location I want, or another solution to my problem of an application trapped behind Visual Studio while it's paused?

like image 223
Chris Schiffhauer Avatar asked Oct 22 '13 18:10

Chris Schiffhauer


2 Answers

I use this code in my debugging application. I set the debug screen as the environment variable DEBUG_SCREEN.

#if DEBUG
    if (Debugger.IsAttached)
    {
        int debugScreen;
        if (int.TryParse(Environment.GetEnvironmentVariable("DEBUG_SCREEN") ?? string.Empty, out debugScreen))
        {
            Application.OpenForms[0].MoveToScreen(debugScreen);
        }
    }
#endif

You can use your main form instead of Application.OpenForms[0].

The Method MoveToScreen is from Alex Strickland: https://stackoverflow.com/a/34263234/3486660

public static bool MoveToScreen(this System.Windows.Forms.Form form, int screenNumber)
{
    var screens = Screen.AllScreens;

    if (screenNumber >= 0 && screenNumber < screens.Length)
    {
        var maximized = false;
        if (form.WindowState == FormWindowState.Maximized)
        {
            form.WindowState = FormWindowState.Normal;
            maximized = true;
        }
        form.Location = screens[screenNumber].WorkingArea.Location;
        if (maximized)
        {
            form.WindowState = FormWindowState.Maximized;
        }
        return true;
    }

    return false;
}
like image 135
Georg Avatar answered Sep 17 '22 15:09

Georg


You can't really expect VS to deal with your application's window, so make your gui applications remember the position they had when closed previously. This not only solves the problem you describe perfectly (well, after the very first run), it also makes for a nicer user experience. Look at Get/SetWindowPlacement (can easily be used from C# as well).

like image 32
stijn Avatar answered Sep 19 '22 15:09

stijn