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