Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows API MONITORINFO Structure

Tags:

c++

winapi

I am trying to get monitor data from the windows API. The GetSystemMetrics() command returns the wrong width in pixels. According to Microsoft's website this is because I need to SetProcessDPIAware()

which means I should preferably be able to create an application manifest which I do not understand.

In searching for an equally low level alternative I found the multiple display monitors functions and structs. I must pass HMONITOR to access the rect structure I want but getting HMONITOR is where I am having issues.

MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY) This command is out of scope- strange because GetMonitorInfo() [which I need HMONITOR for] doesn't cause any issues. I already have windows.h and windowsx.h included. Am I missing a library or what is the issue?

On a separate note, after looking there it became evident that it might also be nice to make the monitor used user-adjustable. SM_CMONITORS should return a count but I would like to know how to convert these numbers to the HMONITOR data I need to get monitor specific information.

::Edit::

I am putting the edit here because the "comment" feature does not provide me with enough space to place the code clip which was requested

Also, I am using GNU GCC with MinGW

#include <iostream>//using these libraries
#include <Windowsx.h>
#include <windows.h>

using namespace std;

int main()
{
    //should print screen width in pixels

    LPMONITORINFO target;
        //create a monitor info struct to store the data to
    HMONITOR Hmon = MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY);
        //create a handle to the main monitor
        //(should start at top left of screen with (0,0) as apposed to other monitors i believe)
        //if i could gather aditional info on what monitors are available that might be           useful
    GetMonitorInfo(Hmon, target);
        //Get the necessary data and store it to target

    cout << "bottom of selected monitor in pixels: " << target->rcMonitor.bottom
         << "Top of the selected monitor" << target->rcMonitor.top
         << "right extreme of selected monitor" << target->rcMonitor.right
         << "left extreme of selected monitor" << target->rcMonitor.left;

    return 0;
}
like image 483
user1964975 Avatar asked Feb 01 '13 03:02

user1964975


1 Answers

If you want to use features that appeared after Windows 95/Windows NT 4, you must specify the WINVER before compiling.

Windows 2000 is WINVER 0x0500, so the compile line needs to add -DWINVER=0x500 in order to see the MONITOR_DEFAULTTOPRIMARY constant.

You need to allocate a MONITORINFO struct, not a pointer to a MONITORINFO struct, and intialize the cbSize field so that Windows knows what information to populate, so in your code:

MONITORINFO target;
target.cbSize = sizeof(MONITORINFO);

HMONITOR hMon = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY);
GetMonitorInfo(hMon, &target);

And then display using:

 target.rcMonitor

instead of

target->rcMonitor

Using SetProcessDPIAware(), is a feature of Windows Vista, so WINVER needs to be set to 0x0600, but the headers shipped with MinGW don't appear to be a complete set of headers for Windows Vista - That function definition is missing, but is present in the Windows 7 SDK headers (I don't have the Windows Vista SDK at hand to check it on).

So, using a manifest seems like an easier solution than pulling the newer APIs.

Monitor handles are meant to be an opaque representation of a monitor - i.e. the value you get should not be used for anything other than other monitor functions. If you want to walk the monitor structures, you should use the EnumDisplayMonitors function, and an appropriate callback routine.

like image 85
Petesh Avatar answered Sep 28 '22 16:09

Petesh