我正在尝试从windows API获取监视器数据。GetSystemMetrics()命令以像素为单位返回错误的宽度。根据微软网站的说法,这是因为我需要SetProcessDPIAware()
这意味着我最好能够创建一个我不理解的application manifest。
在寻找同样低级的替代方案时,我找到了multiple display monitors functions and structs。我必须通过HMONITOR来访问我想要的rect结构,但是获取HMONITOR是我遇到问题的地方。
MonitorFromWindow(hwnd,MONITOR_DEFAULTTOPRIMARY)这个命令超出了范围-奇怪,因为我需要HMONITOR的GetMonitorInfo()没有引起任何问题。我已经包含了windows.h和windowsx.h。我是不是遗漏了一个库,或者是什么问题?
在另一个注释中,在查看之后,很明显,使显示器使用用户可调也是很好的。SM_CMONITORS应该返回一个计数,但是我想知道如何将这些数字转换成我需要的HMONITOR数据,以便获得监视器特定信息。
::编辑::
我之所以将编辑放在这里,是因为“注释”功能没有提供足够的空间来放置所请求的代码片段
此外,我还在使用带有MinGW的GNU GCC
#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;
}发布于 2013-02-02 08:14:20
如果要使用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的完整头集-该函数定义缺失,但在Windows7SDK头中存在(我手头没有Windows Vista SDK来检查它)。
因此,使用清单似乎比使用更新的API更容易解决问题。
监视器句柄意味着监视器的不透明表示-即您获得的值不应用于除其他监视器功能之外的任何其他用途。如果希望遍历监视器结构,则应该使用EnumDisplayMonitors函数和适当的回调例程。
https://stackoverflow.com/questions/14639332
复制相似问题