从Visual Studio开始调试到第二个监视器



当我从Visual Studio开始调试应用程序时,它会直接在Visual Studio上启动该应用程序。这在我的调试中强加了一个步骤(尽管很小),将应用程序窗口移动到我的第二个监视器,以便我可以看到应用程序的状态,同时还可以观察命中断点。(有时,很少,我什至遇到异常!

我已经在SO上阅读了几篇关于为多个显示器优化Visual Studio的文章,但大多数答案都围绕着Visual Studio窗口,而不是调试的应用程序窗口。另一个答案特定于控制台应用程序。有没有办法强加我想要的窗口位置,或者另一种解决方案来解决我在暂停时被困在 Visual Studio 后面的应用程序的问题?

我在调试应用程序中使用此代码。我将调试屏幕设置为环境变量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

您可以使用主窗体而不是 Application.OpenForms[0]。

MoveToScreen 方法来自 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;
}

你不能指望VS处理你的应用程序窗口,所以让你的gui应用程序记住它们之前关闭时的位置。这不仅完美地解决了您描述的问题(好吧,在第一次运行之后),它还提供了更好的用户体验。查看 Get/SetWindowPlacement(也可以从 C# 轻松使用)。

最新更新