如何获取实际的监视器名称?如分辨率对话框中所示
我正在尝试获取系统上监视器的友好名称。我正在使用 C#。
我尝试过 Screen
,它只给了我 //./DisplayXX
。我还尝试了 Win32_DesktopMonitor
和 EnumDisplayDevices
,它们都给了我 //./DisplayXX
或 Generic Monitor
的变体>,而我知道我的显示器名称是 SyncMaster
和 SM2333T
。
现在,Windows 知道这些监视器是什么,并以正确的名称在设备和打印机窗口中显示它们,并且还在设置位置和分辨率的对话框中显示它们。
我可以从哪里获取这些名称?我查看了注册表,似乎找不到它们,所以任何帮助都会很棒。
解决方案: 我遇到的问题是当我第二次调用 EnumDisplayDevices
时,我再次将 iDevNum
设置为 id,这意味着我试图从错误的位置获取数据,然后我替换了这个0,它工作得很好,请参阅下面的代码。
var device = new DISPLAY_DEVICE();
device.cb = Marshal.SizeOf(device);
try
{
for (uint id = 0; EnumDisplayDevices(null, id, ref device, 0); id++)
{
Console.WriteLine(String.Format("{0}, {1}, {2}, {3}, {4}, {5}", id, device.DeviceName, device.DeviceString, device.StateFlags, device.DeviceID, device.DeviceKey));
Console.WriteLine();
device.cb = Marshal.SizeOf(device);
EnumDisplayDevices(device.DeviceName, 0, ref device, 0);
Console.WriteLine(String.Format("{0}, {1}, {2}, {3}, {4}, {5}", id, device.DeviceName, device.DeviceString, device.StateFlags, device.DeviceID, device.DeviceKey));
device.cb = Marshal.SizeOf(device);
device.cb = Marshal.SizeOf(device);
return;
}
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format("{0}", ex.ToString()));
}
I am trying to grab the friendly name for the monitors on my system. I am using C#.
I have tried Screen
, which just gives me //./DisplayXX
. I have also tried both Win32_DesktopMonitor
and EnumDisplayDevices
, they all give me variations of //./DisplayXX
OR Generic Monitor
, whereas I know my displays names are SyncMaster
and SM2333T
.
Now Windows knows what these monitors are, displays them in the Devices and Printers windows with the correct names, and also in the dialog for setting location and resolution.
Where can I grab these names from? I have looked in the registry and cant seem to find them, so any help will be great.
SOLUTION:
The issue I had was when calling EnumDisplayDevices
the second time I was setting iDevNum
to id again, which meant I was trying to grab data from the wrong place, I then replaced this with 0, and it works perfectly, see below for the code.
var device = new DISPLAY_DEVICE();
device.cb = Marshal.SizeOf(device);
try
{
for (uint id = 0; EnumDisplayDevices(null, id, ref device, 0); id++)
{
Console.WriteLine(String.Format("{0}, {1}, {2}, {3}, {4}, {5}", id, device.DeviceName, device.DeviceString, device.StateFlags, device.DeviceID, device.DeviceKey));
Console.WriteLine();
device.cb = Marshal.SizeOf(device);
EnumDisplayDevices(device.DeviceName, 0, ref device, 0);
Console.WriteLine(String.Format("{0}, {1}, {2}, {3}, {4}, {5}", id, device.DeviceName, device.DeviceString, device.StateFlags, device.DeviceID, device.DeviceKey));
device.cb = Marshal.SizeOf(device);
device.cb = Marshal.SizeOf(device);
return;
}
}
}
catch (Exception ex)
{
Console.WriteLine(String.Format("{0}", ex.ToString()));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
仔细检查一下!这就是您要找的,
您现在可以编写
Screen.PrimaryScreen.DeviceFriendlyName()
并获取监视器设备的真实名称。(是的,它与分辨率对话框中看到的名称相同)
虽然我确实添加了一些个人风格来包装此代码,但我没有创建它的硬核部分,
因此,我要感谢 2 位开发人员,即提供 C++ 要点的 MS 开发人员和将这些要点翻译为 C# 代码的匿名开发人员。
Examine carefully! this is what you looking for,
you can now write
Screen.PrimaryScreen.DeviceFriendlyName()
and get the real name of the monitor device.(and yes, it is the same name seen in resolution dialog)
While I did add some personal touch to wrap this code I did not create the Hard-Core part of it,
And so I would like to thank 2 developers, The MS developer who gave the essentials in C++ and the anonymous developer who translated those essentials to C# code.
从
EnumDisplayDevices
获得像//./DisplayX
这样的DisplayDevice.DeviceName
后,您应该再次调用“EnumDisplayDevices”,这时间提供您从上一次调用中获得的“DisplayDevice.DeviceName”作为lpDevice
,以及“0”作为iDevNum
。然后,您将在DisplayDevice.DeviceString
中看到显示器名称。After you get a
DisplayDevice.DeviceName
like//./DisplayX
fromEnumDisplayDevices
, you are supposed to call 'EnumDisplayDevices' a second time, this time providing the 'DisplayDevice.DeviceName' that you got from the previous call aslpDevice
, and '0' asiDevNum
. Then you'll have the monitor name inDisplayDevice.DeviceString
.几乎可以肯定,此信息是通过 SetupAPI 系列函数获得的。我不记得具体细节了,但您需要获取所有监视器设备 (GUID_DEVINTERFACE_MONITOR) 并获取它们的友好名称 (SPDRP_FRIENDLYNAME)。
This information is almost certainly obtained through the SetupAPI family of functions. I don't recall the specifics off the top of my head, but you will need to get all monitor devices (GUID_DEVINTERFACE_MONITOR) and obtain their friendly names (SPDRP_FRIENDLYNAME).
从这里: 获取准确的监视器/显示/屏幕名称
好吧,这个问题是不过,老了,为了谷歌重定向,我建议我的“WindowsDisplayAPI”库。
https://www.nuget.org/packages/WindowsDisplayAPI
使用该库,有多个获取显示名称的方法。最简单的方法是:
但是这是使用旧的API,如果你确定你的程序至少面向Windows Vista,我建议使用以下代码:
From here: Get Exact Monitor/Display/Screen Name
Well, this question is old, however, as for the sake of google redirects, I suggest my 'WindowsDisplayAPI' library.
https://www.nuget.org/packages/WindowsDisplayAPI
Using the library, there are multiple ways to get the display name. The simplest way is:
But this is using the old API, if you are sure that your program targets at least Windows Vista, I suggest the following code:
我一直在寻找同一问题的答案。在此链接中提到通用pnp监视器显示,因为没有为显示器安装驱动程序,并且设备正在使用通用 PnP 显示器驱动程序运行。
对于 Windows 7 及更高版本,似乎有一个与 EnumDisplayDevices 不同的函数来获取控制面板中所示的所需显示名称。这可以使用 DisplayConfigGetDeviceInfo 函数来完成,如下所述 此处。我希望很快有人能将其翻译成我正在寻找的Delphi。
I have been seraching for an answer to this same question. In this link it is mentioned that generic pnp monitor is shown, because there is no driver installed for the monitor and the device is running with the Generic PnP Monitor Driver.
For windows 7 and higher there seems to be a different function than EnumDisplayDevices to get the desired display names as shown in the control panel. This can be done with the DisplayConfigGetDeviceInfo function as explained here. I hope someone soon can translate this to Delphi, which I am looking for.