现在我的显示器设置为 DVI-D,我知道这是我的电脑。 但是例如我的 playstation 4 连接到 HDMI 1 我想要做的是当我将我的监视器源更改为 HDMI 1 时检测它,当我将它更改回 DVI-D 时检测它以便我可以知道我的监视器源何时在 pc 或 ps4 上。
到目前为止我尝试的是这个。 我在我的设计器中添加了一个计时器我正在运行计时器 40 秒并在计时器中调用一个方法:
int counttimer2 = 0;
private void timer2_Tick(object sender, EventArgs e)
{
if (counttimer2 == 40)
{
w.Close();
timer2.Stop();
}
DetectScreenName();
counttimer2 += 1;
}
以及方法 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());
}
}
}
}
在文本文件上,我正在写结果以查看更改,所有结果都是相同的:
通用 PnP 监视器 桌面显示器1 通用 PnP 监视器
我看到的每一秒:
通用 PnP 监视器 桌面显示器1 通用 PnP 监视器
当我将监听源切换到 HDMI 1 时它没有改变
最佳答案
当您切换显示器的信号源时,您的计算机没有任何变化 - 就好像显示器已关闭一样。
不幸的是,您无法可靠地检测到这一点,如 Is there any way to detect the monitor state in Windows (on or off)? 中所述和 Detect external display being connected or removed under Windows 7 .
关于c# - 我如何检测我的显示器现在设置的来源?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684373/