win32 - EnumDisplayDevices和EnumDisplayMonitors的使用

EnumDisplayDevices枚举适配器

EnumDisplayMonitors枚举监视器

#pragma comment(lib, "dxva2.lib")
#include <windows.h>
#include <lowlevelmonitorconfigurationapi.h>
#include <physicalmonitorenumerationapi.h>
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string>
#include <highlevelmonitorconfigurationapi.h>
#include <vector>

using namespace std;

void D(HANDLE hPhysicalMonitor)
{
    DWORD cchStringLength = 0;
    BOOL bSuccess = 0;
    LPSTR szCapabilitiesString = NULL;

    // Get the length of the string.
    bSuccess = GetCapabilitiesStringLength(
        hPhysicalMonitor, // Handle to the monitor.
        &cchStringLength
    );
    int err = GetLastError();

    if (bSuccess)
    {
        // Allocate the string buffer.
        LPSTR szCapabilitiesString = (LPSTR)malloc(cchStringLength);
        if (szCapabilitiesString != NULL)
        {
            // Get the capabilities string.
            bSuccess = CapabilitiesRequestAndCapabilitiesReply(
                hPhysicalMonitor,
                szCapabilitiesString,
                cchStringLength
            );
            cout << szCapabilitiesString << endl;
            // Free the string buffer.
            free(szCapabilitiesString);
        }
    }
}

static BOOL CALLBACK MonitorEnum(HMONITOR hMon, HDC hdc, LPRECT lprcMonitor, LPARAM pData)
{
    cout << "hmonitor:" << hMon << endl;

    DWORD cPhysicalMonitors;
    BOOL bSuccess = GetNumberOfPhysicalMonitorsFromHMONITOR(hMon, &cPhysicalMonitors);
    cout << "GetNumber: " << bSuccess << ", number of physical monitors: " << cPhysicalMonitors << endl;

    LPPHYSICAL_MONITOR pPhysicalMonitors = (LPPHYSICAL_MONITOR)malloc(cPhysicalMonitors * sizeof(PHYSICAL_MONITOR));
    bSuccess = GetPhysicalMonitorsFromHMONITOR(hMon, cPhysicalMonitors, pPhysicalMonitors);
    cout << "GetPhysicalMonitor: " << bSuccess << endl
        << "Handle: " << pPhysicalMonitors->hPhysicalMonitor << endl
        << "Description: ";
    wcout << (WCHAR*)(pPhysicalMonitors->szPhysicalMonitorDescription) << endl;;

    D(pPhysicalMonitors->hPhysicalMonitor);

    DestroyPhysicalMonitors(cPhysicalMonitors, pPhysicalMonitors);
    free(pPhysicalMonitors);

    cout << "---------------------------------------" << endl;

    return TRUE;
}

void A()
{
    HWND hWnd = GetDesktopWindow();
    EnumDisplayMonitors(0, 0, MonitorEnum, NULL);
    HMONITOR hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTOPRIMARY);
    cout << "---------------------------------------" << endl;
    cout << "Monitor: " << hMonitor << endl;  

}

void B()
{
    DWORD DispNum = 0;
    DISPLAY_DEVICE DisplayDevice;
    // Initialize DisplayDevice.
    ZeroMemory(&DisplayDevice, sizeof(DisplayDevice));
    DisplayDevice.cb = sizeof(DisplayDevice);

    while ((EnumDisplayDevices(NULL, DispNum, &DisplayDevice, 0)))
    {
        std::wstring deviceName = DisplayDevice.DeviceName;
        DISPLAY_DEVICE DisplayDeviceM;
        ZeroMemory(&DisplayDeviceM, sizeof(DisplayDeviceM));
        DisplayDeviceM.cb = sizeof(DisplayDeviceM);
        int monitorIndex = 0;
        while (EnumDisplayDevices(deviceName.c_str(), monitorIndex, &DisplayDeviceM, EDD_GET_DEVICE_INTERFACE_NAME))
        {
            std::wstring monitorID = DisplayDeviceM.DeviceID;
            wcout <<"monitorID :"<< monitorID<< endl;
            ++monitorIndex;            
        }
        DispNum++;
    }
    cout << "---------------------------------------" << endl;
}

int main()
{
    B();

    A();




    getchar();

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/strive-sun/p/13403996.html