Windows API MONITORINFO Structure
Windows API MONITORINFO Structure
我正在尝试从Windows API获取监视器数据。GetSystemMetrics()
命令返回错误的宽度(以像素为单位)。根据Microsoft的网站,这是因为我需要SetProcessDPIAware()
这意味着我最好能够创建一个我不理解的应用程序清单。
在寻找同样低级的替代方案时,我发现了多个显示器的功能和结构。我必须通过HMONITOR
才能访问我想要的矩形结构,但HMONITOR
是我遇到问题的地方。
MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY)
这个命令超出了范围 - 奇怪GetMonitorInfo()
因为[我需要HMONITOR
]不会导致任何问题。 我已经包括windows.h
和windowsx.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
函数和适当的回调例程。
- Python to C++ Data structure API
- CMakeLists.txt structure
- 使用C++将UDP数据包存储在Structure中
- JNA从DLL调用C++对象-java.lang.IollegalArgumentException:Structure
- bmiColors field of BITMAPINFO structure
- 如何使用jna Structure类加载DEVMODE而不出现乱码
- structure sockaddr_un:初始化正确
- cvLogPolar 导致 opencv 错误"Bad flag (parameter or structure field)"
- 通过使用structure的元素来获取一个结构数组
- Structure As构造函数参数(在main中声明)
- "container"和"data structure"有什么区别?
- 不断收到错误"member reference base type 'string [1000]' is not a structure or union"不知道如何解决?
- 使用Structure卡在C++代码上
- 设置 AD 标志时获取"The security ID structure is invalid"
- 实现观察者模式的问题:"Member reference base type ________ is not a structure or union"
- 带有模板化类型structure灾难的C++函数模板参数
- Windows API MONITORINFO Structure