Windows API MONITORINFO Structure

Windows API MONITORINFO Structure

本文关键字:Structure MONITORINFO API Windows      更新时间:2023-10-16

我正在尝试从Windows API获取监视器数据。GetSystemMetrics() 命令返回错误的宽度(以像素为单位)。根据Microsoft的网站,这是因为我需要SetProcessDPIAware()

这意味着我最好能够创建一个我不理解的应用程序清单。

在寻找同样低级的替代方案时,我发现了多个显示器的功能和结构。我必须通过HMONITOR才能访问我想要的矩形结构,但HMONITOR是我遇到问题的地方。

MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY)这个命令超出了范围 - 奇怪GetMonitorInfo()因为[我需要HMONITOR]不会导致任何问题。 我已经包括windows.hwindowsx.h。 我是否缺少库或问题是什么?

另外,在查看那里之后,很明显,使显示器使用用户可调也可能很好。 SM_CMONITORS应该返回一个计数,但我想知道如何将这些数字转换为获取监视器特定信息所需的HMONITOR数据。

::编辑::

在这里进行编辑是因为"注释"功能没有为我提供足够的空间来放置请求的代码片段

另外,我正在使用GNU GCC和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;
}
如果要

使用Windows 95/Windows NT 4之后出现的功能,则必须在编译之前指定WINVER。

Windows 2000是WINVER 0x0500,所以编译行需要添加-DWINVER=0x500才能看到MONITOR_DEFAULTTOPRIMARY常量。

您需要分配一个MONITORINFO结构,而不是指向MONITORINFO结构的指针,并初始化cbSize字段,以便 Windows 知道要填充哪些信息,因此在您的代码中:

MONITORINFO target;
target.cbSize = sizeof(MONITORINFO);
HMONITOR hMon = MonitorFromWindow(GetDesktopWindow(), MONITOR_DEFAULTTOPRIMARY);
GetMonitorInfo(hMon, &target);

然后使用以下方法显示:

 target.rcMonitor

而不是

target->rcMonitor
使用SetProcessDPIAware()是Windows Vista

的一项功能,因此WINVER需要设置为0x0600,但是MinGW附带的标头似乎不是Windows Vista的完整标头集 - 该函数定义丢失,但存在于Windows 7 SDK标头中(我手头没有Windows Vista SDK来检查它)。

因此,使用清单似乎比拉取较新的 API 更容易。

监视器

句柄是监视器的不透明表示形式 - 即,您获得的值不应用于其他监视器功能以外的任何内容。如果要遍历监视器结构,则应使用 EnumDisplayMonitors 函数和适当的回调例程。