简体   繁体   English

正在检测屏幕1和屏幕2,在屏幕1上打开(仅当没有屏幕时)

[英]Detecting Screen 1 and Screen 2 , open at screen 1 (only if there's no screen)

I have the following code: 我有以下代码:

namespace ExtendedDisplay{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    public static void ThreadProc(object arg)
    {
        Form2 form = arg as Form2;
        Application.Run(form);
    }

    int iWidth = 0;
    int iHeight = 0;

    private void button2_Click(object sender, EventArgs e)
    {

        Rectangle rect = new Rectangle(int.MaxValue, int.MaxValue, int.MinValue, int.MinValue);

        int iMonitorCount = Screen.AllScreens.Length;
        foreach (Screen screen in Screen.AllScreens)
            rect = Rectangle.Union(rect, screen.Bounds);
        Console.WriteLine("(width, height) = ({0}, {1})", rect.Width, rect.Height);
        label2.Text = ("Resolution: " + rect.Width + "x" + rect.Height);
        iWidth = rect.Width;
        iHeight = rect.Height;

    }
    [STAThread]
    private void button1_Click(object sender, EventArgs e)
    {
        Rectangle rect = new Rectangle(int.MaxValue, int.MaxValue, int.MinValue, int.MinValue);

        int iMonitorCount = Screen.AllScreens.Length;
        foreach (Screen screen in Screen.AllScreens)
            rect = Rectangle.Union(rect, screen.Bounds);

Form2 form = new Form2() { Text = "test" };

        Thread t = new Thread(ThreadProc);

        if (!Screen.AllScreens[1].Bounds.IsEmpty)
        {
            form.StartPosition = FormStartPosition.Manual;
            form.Bounds = Screen.AllScreens[1].Bounds;
            t.Start(form);
        }
        else 
        {

            t.Start(form);
        }

The code is running fine, however I can only get one of the condition to run 代码运行良好,但是我只能获得其中一种条件来运行

Example: 例:

if (screen1 is not empty & screen 0 is not empty) 如果(screen1不为空&屏幕0不为空)
display at screen 1 在屏幕1上显示
else if (screen 0 is not empty) 否则(屏幕0不为空)
display at screen 0 显示在屏幕0

this if and else if 这个如果,否则,如果
it will only run if 它只会在以下情况下运行

Is it a bug? 是虫子吗?

currently the code is 目前的代码是
if and else only 如果只有
however only the if can be run 但是只有if可以运行
if I don't have screen 1 如果我没有屏幕1
it will crash (thus else is not working) 它将崩溃(因此无法正常工作)

That code is pretty messy, most of it needs junking as it seems to be redundant. 这段代码非常凌乱,因为似乎多余,所以大多数都需要垃圾处理。 The problem line is probably: 问题行可能是:

    if (!Screen.AllScreens[1].Bounds.IsEmpty)
    {

You just flat assume the user has 2 screens here, by referring to the second screen without checking if the Screen.AllScreens array even has a [1] th element. 您只是假定用户在这里有2个屏幕,而是通过引用第二个屏幕而不检查Screen.AllScreens数组是否甚至具有第[1]个元素。 Why not something more like: 为什么不这样:

if(Screen.AllScreens.Length > 1) //does the user have at least 2 screens?

I can't imagine the Rectangle that is Screen.Bounds will ever be empty either - this isn't testing if there is nothing on screen, it's testing if the screen is 0x0 pixels in size. 我无法想象RectangleScreen.Bounds永远也不会是空的-这不是在测试屏幕上是否没有任何东西,而是在测试屏幕是否为0x0像素。 Probably not what you want. 可能不是您想要的。

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.rectangle.isempty?view=netframework-4.7.2 https://docs.microsoft.com/zh-cn/dotnet/api/system.drawing.rectangle.isempty?view=netframework-4.7.2

Ask another question with the actual problem you're trying to solve, perhaps something like "How can I test if my user has 2 monitors and if they do, open my app on the second monitor, but if they don't, then open it on the first monitor?" 询问您要解决的实际问题的另一个问题,例如“如何测试我的用户是否有2台显示器,如果有,请在第二台显示器上打开我的应用程序,但如果没有,请打开在第一台显示器上?” - I think this is an XY problem, where you've got some issue, you're written some code to try and solve it, it doesn't work/won't work, and you're asking for help fixing that code - instead tell use the original problem you're trying to solve, not the problem with the broken solution -我认为这是一个XY问题,您遇到了一些问题,编写了一些代码来尝试解决该问题,它不起作用/不起作用,并且您正在寻求有关修复该代码的帮助-相反,请使用您要解决的原始问题,而不是告诉您解决方案已损坏的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM