The display information under Windows is mainly implemented by two functions. One is enumdisplaydevices () and the other is enumdisplaymonitors (). EnumDisplayDevices () Enumerates all display devices, while Enumdisplaymonitors enumerates all the displays. The display device is not the same as the monitor, such as the graphics card, but not the display. The specific differences are analyzed later. Enumdisplaymonitors () will also be an invisible pseudo-display, if you just want to get the actual number of monitors can be used GetSystemMetrics (sm_cmonitors), the function does not include a virtual display.
The following code shows the usage and difference of these three functions (refer to the self-https://social.msdn.microsoft.com/Forums/windowsdesktop/en-US/ 668e3cf9-4e00-4b40-a6f8-c7d2fc1afd39/how-can-i-retrieve-monitor-information?forum= Windowsgeneraldevelopmentissues)
MonitorSerialCtrlApp.cpp:Defines the entry point for the console application.//#include "stdafx.h" #include <window S.h>using namespace Std; BOOL CALLBACK Myinfoenumproc (hmonitor hmonitor, HDC hdcmonitor, LPRECT lprcmonitor, LPARAM dwdata) {Monitori Nfoex mi; ZeroMemory (&mi, sizeof (MI)); mi.cbsize = sizeof (MI); Getmonitorinfo (Hmonitor, &mi); wprintf (L "Displaydevice:%s\n", Mi.szdevice); return TRUE;} int _tmain (int argc, _tchar* argv[]) {int nummonitor; int run=0; while (1) {printf ("*********************%d****************\n", run); run++; printf ("\n\n\enumdisplaydevices\n\n\n"); Display_device DD; ZeroMemory (&DD, sizeof (DD)); DD.CB = sizeof (DD); for (int i=0; EnumDisplayDevices (NULL, I, &DD, 0); i++) {//enumdisplaydevices (NULL, I, &DD, 0); wprintf (L "\n\ndevice%d:", i); wprintf (L "\ n devicename: '%s '", DD.) DeviceName); wprintf (L "\ n devicestring: '%s '", DD.) devicestring); wprintf (L "\ n stateflags:%s%s%s%s", (DD. StateFlags & Display_device_attached_to_desktop)? L "desktop": L ""), ((DD. StateFlags & Display_device_primary_device)? L "PRIMARY": L ""), ((DD. StateFlags & display_device_vga_compatible)? L "VGA": L ""), ((DD. StateFlags & Display_device_multi_driver)? L "Multi": L ""), ((DD. StateFlags & Display_device_mirroring_driver)? L "Mirror": L "")); Get more info about the device Display_device DD2; ZeroMemory (&DD2, sizeof (DD2)); DD2.CB = sizeof (DD2); EnumDisplayDevices (DD. DeviceName, 0, &DD2, 0); wprintf (L "\ n DeviceID: '%s '", DD2. DeviceID); wprintf (L "\ nthe Monitor Name: '%s '", DD2. devicestring); } printf ("\n\n\nenumdisplaymonitors\n\n\n"); Enumdisplaymonitors (null, NULL, MYINFOENUMPROC, 0); NummOnitor = GetSystemMetrics (sm_cmonitors); printf ("GetSystemMetrics:%d\n", nummonitor); Sleep (5000); } while (1); return 0;}
This code refreshes the display display device information every 5 seconds. The output results are as follows:
3***************enumdisplaydevicesdevice 0:devicename: ' \\.\display1 ' devicestring: ' NVIDIA GeForce GTX 760 ' stateflags:desktop primary DeviceID: ' monitor\aci23f7\{4d36e96e-e325-11ce-bfc1-08002be10318}\000 2 ' Monitor Name: ' Generic PnP Monitor ' Device 1:devicename: ' \\.\display2 ' devicestring: ' NVIDIA GeForce GTX 76 0 ' stateflags:desktop DeviceID: ' monitor\aci23f7\{4d36e96e-e325-11ce-bfc1-08002be10318}\0001 ' MONITOR Name: ' G Eneric PnP Monitor ' Device 2:devicename: ' \\.\display3 ' devicestring: ' NVIDIA GeForce GTX 760 ' Stateflags:de Sktop DeviceID: ' monitor\gsm0001\{4d36e96e-e325-11ce-bfc1-08002be10318}\0003 ' monitor Name: ' Generic PnP MONITOR ' Dev Ice 3:devicename: ' \\.\display4 ' devicestring: ' NVIDIA GeForce GTX 760 ' Stateflags:deviceid: ' Monitor Name: ' Device 4:devicename: ' \\.\displayv1 ' devicestring: ' RDPDD Chained DD ' Stateflags:deviceid: ' Monitor Name: ' Device 5: devicename: ' \\.\displayv2 ' devicestring: ' RDP Encoder Mirror Driver ' Stateflags:deviceid: ' Monitor Name: ' Device 6:devicename: ' \\.\displayv3 ' devicestring: ' RDP Reflector Display Driver ' Stateflags:devic EID: ' Monitor Name: ' Enumdisplaymonitorsdisplaydevice: \\.\display1displaydevice: \\.\display2displaydevice: \\.\ Display3getsystemmetrics:3
You can see that enumdisplaydevices () lists the display devices. The first four are my graphics card GEFORCEGTX 760. There are 4 devices because my graphics card has 4 interfaces. The first three interfaces are connected to the monitor, so the display information is shown below. The display name is "Universal Plug and Play display" which is the same as the one shown in Windows Device Manager. The only information that can be differentiated is the 7 characters behind Monitor\ in DeviceID, which are related to the manufacturer's signal. ACI23F7 represents my Asus display, GSM001 refers to the LG Monitor. It is important to note that the Windows7 display property or the Control Panel hardware can display the display can be identified by the display model and manufacturer, this information is not possible to get through the programming method, which has been in the Web message by the Microsoft staff verified. The exact words are "thereis not a supported means to figour out the IDs so you referred toprogrammatically. It is never a design goal to provide a-a-applicationsto label monitors with the same IDs that the screen resolution Control paneluses. "
Enumdisplaymonitors () will only list display information. As above, the display is
Displaydevice: \\.\display1
Displaydevice: \\.\display2
Displaydevice: \\.\display3
GetSystemMetrics (sm_cmonitors) will only get the number of monitors: 3
It is worth mentioning that the experiment found that when all the monitors are unplugged, Nvidia will own a virtual display NVD0000, so no monitor, using GetSystemMetrics (sm_cmonitors) to get the number of displays is 1
If the above function can only poll for the current display information, how can I detect the display plug and unplug it?
Windows emits wm_devicechange information when a device changes. However, by default, Windows issues wm_devicechange with two conditions:
1. The program must have a main window
2. The port and disk changes are the only lines
To detect other hardware plug-in, or if the program does not have a main window, you must use the RegisterDeviceNotification () function to register the hardware you need to monitor. Microsoft officially gave an example of using the function:
https://msdn.microsoft.com/en-us/library/windows/desktop/aa363432 (v=vs.85). aspx
In this example, the type of device to be monitored is passed to the system through the function parameters of the GUID.
This GUID was for all USB serial host PnP drivers, but can replace the IT//with any valid device class GUID. GUID Wceusbshguid = {0x25dbce51, 0x6c8f, 0x4a72, 0x8a,0x6d,0xb5,0x4c,0x2b,0x4f,0xc8,0x35};
It is important to note that the GUID must be a device interface GUID. There are two types of GUIDs, one is the device class GUID, and the other is the device Interfaceguid. The device type GUID determines which type of device is in Device Manager. The device interface GUID is related to the interface of the device and the system, which is the parameter we need to pass. The device interface GUID can be queried by Microsoft official. Here (https://msdn.microsoft.com/en-us/library/windows/hardware/ff545901 (v=vs.85). aspx) the interface GUID of the query to the display is { E6F07B5F-EE97-4A90-B076-33F57BF4EAA7}, replaced in the sample code can detect the display Plug and unplug ... The results of the operation are as follows:
Dbt_deviceremovecomplete on behalf of hardware removal
Dbt_devnodes_changed represents hardware changes, and insertion removal will have the message
Dbt_devicearrival represents hardware Insertion
The newly inserted display information can then be queried by querying the Dbcc_name members in the DEV_BROADCAST_DEVICEINTERFACE structure behind the dbt_devicearrival information.
Pdev_broadcast_deviceinterface B = (pdev_broadcast_deviceinterface) LParam; TCHAR strbuff[256]; TCHAR deviceid[8]; TCHAR *ptr; Output Some messages to the window. Switch (WParam) {case dbt_devicearrival: msgcount++; stringcchprintf ( strbuff, N, TEXT ("Message%d:dbt_devicearrival\n"), Msgcount); wcscat_s (Strbuff, b->dbcc_name); wcscat_s (Strbuff, TEXT ("\ n")); break; Case Dbt_deviceremovecomplete: ....
The operation results are as follows
The GSM0001 represents the newly inserted LG display.
Reference:
Http://www.codeproject.com/Articles/14500/Detecting-Hardware-Insertion-and-or-Removal
(Reproduced please specify)
Windows Programming detects display information and plug-and-draw