2

Now my monitor is set to DVI-D and i know it's my pc. But for example my playstation 4 is connected to HDMI 1 What i want to do is when i change my monitor source to HDMI 1 detect it and when i change it back to DVI-D detect it to so i can then know when my monitor source is on the pc or on the ps4.

What i tried so far is this. I added a timer in my designer i'm running the timer for 40 seconds and in the timer calling a method:

int counttimer2 = 0;
    private void timer2_Tick(object sender, EventArgs e)
    {
        if (counttimer2 == 40)
        {
            w.Close();
            timer2.Stop();
        }
        DetectScreenName();
        counttimer2 += 1;
    }

And the method DetectScreenName:

private void DetectScreenName()
        {
            if (counttimer2 < 40)
            {
                SelectQuery q = new SelectQuery("SELECT Name, DeviceID, Description FROM Win32_DesktopMonitor");
                using (ManagementObjectSearcher mos = new ManagementObjectSearcher(q))
                {
                    foreach (ManagementObject mo in mos.Get())
                    {
                        Console.WriteLine("{0}, {1}, {2}",
                            mo.Properties["Name"].Value.ToString(),
                            mo.Properties["DeviceID"].Value.ToString(),
                            mo.Properties["Description"].Value.ToString());
                        results.Add(mo.Properties["Name"].Value.ToString());
                        results.Add(mo.Properties["DeviceID"].Value.ToString());
                        results.Add(mo.Properties["Description"].Value.ToString());
                        w.WriteLine(mo.Properties["Name"].Value.ToString());
                        w.WriteLine(mo.Properties["DeviceID"].Value.ToString());
                        w.WriteLine(mo.Properties["Description"].Value.ToString());
                    }
                }
            }
        }

On the text file i'm writing the results to see the changes all the results are the same:

Generic PnP Monitor DesktopMonitor1 Generic PnP Monitor

Every second i see:

Generic PnP Monitor DesktopMonitor1 Generic PnP Monitor

It didn't change when i switch the monitor source to HDMI 1

1 Answers1

0

When you switch the source of your monitor, for your computer nothing changes - it is as if the monitor is switched off.

Unfortunately, you can't reliably detect that, as explained in Is there any way to detect the monitor state in Windows (on or off)? and Detect external display being connected or removed under Windows 7.

Community
  • 1
  • 1
CodeCaster
  • 147,647
  • 23
  • 218
  • 272