John-L_.
John-L_.

Reputation: 45

Accessing MONITORINFOEX values in Visual C++

I'm perplexed trying to retrieve MONITORINFOEX values. I tried:

typedef struct tagMONITORINFO {
    DWORD cbSize;
    RECT  rcMonitor;
    RECT  rcWork;
    DWORD dwFlags;
} MONITORINFO, *LPMONITORINFO;
typedef struct tagMONITORINFOEX {
    CHAR       szDevice[CCHDEVICENAME];
    MONITORINFO tagMONITORINFO;
} MONITORINFOEX, *LPMONITORINFOEX;
MONITORINFOEX miea;
miea.tagMONITORINFO.cbSize = sizeof(MONITORINFOEX);
GetMonitorInfo(hMonitor, (&miea));

to no avail. I modified the last line to:

GetMonitorInfo(hMonitor, ((LPMONITORINFO) &miea));

again, no luck. I get compiler messages about the 2nd parameter of GetMonitorInfo.

Upvotes: 1

Views: 1519

Answers (1)

Swordfish
Swordfish

Reputation: 13134

In the code you have shown you try to define structs that are part of WinAPI yourself. Don't do that, there is no need to. Include the appropriate header files instead.

Simple sample:

#include <cstdlib>
#include <iostream>

#include <windows.h>

int main()
{
    // just a cheap way to get a handle
    auto monitor{ MonitorFromWindow(GetConsoleWindow(), MONITOR_DEFAULTTONEAREST) };

    MONITORINFOEXW miex{ sizeof miex };  // set cbSize member
    if (!GetMonitorInfoW(monitor, &miex)) {
        std::cerr << "GetMonitorInfo() failed :(\n\n";
        return EXIT_FAILURE;
    }

    std::wcout << miex.szDevice << ": "
               << miex.rcMonitor.right  - miex.rcMonitor.left << " x "
               << miex.rcMonitor.bottom - miex.rcMonitor.top  << '\n';
}

Sample output:

\\.\DISPLAY1: 2560 x 1440

Upvotes: 4

Related Questions