首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Windows API MONITORINFO结构

Windows API MONITORINFO结构
EN

Stack Overflow用户
提问于 2013-02-01 11:39:32
回答 1查看 7.7K关注 0票数 5

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

这意味着我最好能够创建一个我不理解的application manifest

在寻找同样低级的替代方案时,我找到了multiple display monitors functions and structs。我必须通过HMONITOR来访问我想要的rect结构,但是获取HMONITOR是我遇到问题的地方。

MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY)这个命令超出了范围-奇怪,因为我需要HMONITORGetMonitorInfo()没有引起任何问题。我已经包含了windows.hwindowsx.h。我是不是遗漏了一个库,或者是什么问题?

在另一个注释中,在查看之后,很明显,使显示器使用用户可调也是很好的。SM_CMONITORS应该返回一个计数,但是我想知道如何将这些数字转换成我需要的HMONITOR数据,以便获得监视器特定信息。

::编辑::

我之所以将编辑放在这里,是因为“注释”功能没有提供足够的空间来放置所请求的代码片段

此外,我还在使用带有MinGW的GNU GCC

代码语言:javascript
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-02 08:14:20

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

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

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

代码语言:javascript
复制
MONITORINFO target;
target.cbSize = sizeof(MONITORINFO);

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

然后使用以下命令显示:

代码语言:javascript
复制
 target.rcMonitor

而不是

代码语言:javascript
复制
target->rcMonitor

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

因此,使用清单似乎比使用更新的API更容易解决问题。

监视器句柄意味着监视器的不透明表示-即您获得的值不应用于除其他监视器功能之外的任何其他用途。如果希望遍历监视器结构,则应该使用EnumDisplayMonitors函数和适当的回调例程。

票数 9
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14639332

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档