Windows MONITORINFO interface structure

I am trying to get monitor data from windows API. The command GetSystemMetrics()

returns the wrong width in pixels. According to Microsoft site, it has to do with what I needSetProcessDPIAware()

which means I should preferably create an application manifest which I don't understand.

Looking for an equally low level, I found that multiple monitors display functions and structures . I have to pass HMONITOR

to access the structure the straight line I want, but getting HMONITOR

is where I am having problems.

MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY)

This command is out of scope - weird because the GetMonitorInfo()

[which I need HMONITOR

for] doesn't cause any problems. I already have windows.h

and windowsx.h

. Am I missing a library or what is the problem?

On a separate note, looking there, it became apparent that it would be nice to force the monitor to use a custom setting. SM_CMONITORS

should return a count, but I would like to know how to convert these numbers to the data HMONITOR

I need to get specific monitor information.

:: Edit ::

I am placing an edit here because the "comment" function does not give me enough space to place the programmed clip

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;
}

      

+3


source to share


1 answer


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

Windows 2000 has WINVER

0x0500

, so the compilation line must add -DWINVER=0x500

to see the constant MONITOR_DEFAULTTOPRIMARY

.

You need to highlight the structure MONITORINFO

, not the pointer to the structure MONITORINFO

, and the cbSize

field cbSize

so Windows knows what information to fill in, like in your code:

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

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

      

And then display with:

 target.rcMonitor

      



instead

target->rcMonitor

      

Usage SetProcessDPIAware()

is a Windows Vista feature, so WINVER

must be set to 0x0600

, but the headers that come with MinGW don't seem like the complete set of headers for Windows Vista. This feature definition is missing, but is present in the headers of the Windows 7 SDK (I don't have the Windows Vista SDK on hand to test it).

So using the manifest seems like a simpler solution than using the newer APIs.

Monitor descriptors are meant to represent the monitor opaquely - i.e. the resulting value should not be used for anything other than other monitor functions. If you want to walk through monitor structures, you must use a function and the associated callback procedure. EnumDisplayMonitors

+6


source







All Articles