简体   繁体   中英

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)
display at screen 1
else if (screen 0 is not empty)
display at screen 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 I don't have screen 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. 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. Probably not what you want.

https://docs.microsoft.com/en-us/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?" - 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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