获取监视器的名称 [英] Get the name of a monitor
问题描述
我在使用 winapi 检索监视器名称时遇到了一些麻烦.根据 stackoverflow 上的其他条目,获取监视器名称的正确方法是:
I'm having a bit of trouble retrieving the name of a monitor with winapi. According to other entries on stackoverflow, the correct way to get the name of a monitor is this:
EnumDisplayDevices(nullptr, 0, &oDisplayDevice, 0);
char lpszDeviceName[32];
memcpy(lpszDeviceName, oDisplayDevice.DeviceName, 32);
EnumDisplayDevices(lpszDeviceName, 0, &oDisplayDevice, 0);
char lpszMonitorName[128];
memcpy(lpszMonitorName, oDisplayDevice.DeviceString, 128);
然而,EnumDisplayDevices
第二次返回 FALSE
.第一次,DeviceName
是 \\DISPLAY1
而 DeviceString
是 GPU 供应商.使用 MONITORINFOEX
结构为我提供与 DeviceName
相同的值.
However, EnumDisplayDevices
returns FALSE
the second time around. The first time around, DeviceName
is \\DISPLAY1
and DeviceString
is the GPU vendor. Using the MONITORINFOEX
struct gives me the same value as DeviceName
.
明确地说,我正在寻找诸如Samsung blah blah"之类的内容,或者屏幕分辨率页面上控制面板中显示的内容.
To be clear I'm looking for something like "Samsung blah blah," or what appears in the control panel on the screen resolution page.
推荐答案
这似乎为我返回了正确的数据:
This seems to return the proper data for me:
#include <Windows.h>
#include <iostream>
#include <string>
int main()
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
int deviceIndex = 0;
while(EnumDisplayDevices(0, deviceIndex, &dd, 0))
{
std::string deviceName = dd.DeviceName;
int monitorIndex = 0;
while(EnumDisplayDevices(deviceName.c_str(), monitorIndex, &dd, 0))
{
std::cout << dd.DeviceName << ", " << dd.DeviceString << "\n";
++monitorIndex;
}
++deviceIndex;
}
return 0;
}
如果您要为 UNICODE 编译,请改用它:
If you're compiling for UNICODE then use this instead:
#include <Windows.h>
#include <iostream>
#include <string>
int main()
{
DISPLAY_DEVICE dd;
dd.cb = sizeof(dd);
int deviceIndex = 0;
while(EnumDisplayDevices(0, deviceIndex, &dd, 0))
{
std::wstring deviceName = dd.DeviceName;
int monitorIndex = 0;
while(EnumDisplayDevices(deviceName.c_str(), monitorIndex, &dd, 0))
{
std::wcout << dd.DeviceName << L", " << dd.DeviceString << L"\n";
++monitorIndex;
}
++deviceIndex;
}
return 0;
}
以下是输出示例:
\.\DISPLAY1\Monitor0,戴尔 U2410(DP)
\.\DISPLAY2\Monitor0,戴尔2407WFP-HC(数字)
\.\DISPLAY1\Monitor0, Dell U2410(DP)
\.\DISPLAY2\Monitor0, Dell 2407WFP-HC (Digital)
这篇关于获取监视器的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!