我正在尝试获取左上角的 x,y 和右下角的 x,y。并计算显示器的宽度和高度。
我的辅助显示器是 1920x1080,如我的显示设置屏幕截图所示:
![](https://i.stack.imgur.com/PC1vh.png)
我通过两种方式获取显示器尺寸。下面的代码是 js-ctypes,但我简化了所有错误检查和其他 ctypes 内容,并尝试使其看起来像 c。但这是一个 winapi 问题,而不是 ctypes,因此我没有用它来标记该主题。
第一种方法:
cPoint = POINT();
GetCursorPos(&cPoint);
cMon = MonitorFromPoint(cPoint, MONITOR_DEFAULTTONEAREST);
cMonInfo = MONITORINFOEX();
cMonInfo.cbSize = MONITORINFOEX.size;
GetMonitorInfo(cMon, &cMonInfo);
lpszDriver = null;
lpszDevice = cMonInfo.szDevice;
xTopLeft = cMonInfo.rcMonitor.left;
yTopLeft = cMonInfo.rcMonitor.top;
nWidth = cMonInfo.rcMonitor.right - xTopLeft;
nHeight = cMonInfo.rcMonitor.bottom - yTopLeft;
这给了我以下的正确信息:
_RECT(-1920, -1080, -640, -360)
向右-向左做得到 1280
做从下到上的结果是 720
尺寸肯定是错误的。它的宽度应该是 1920,高度应该是 1080。
然后我尝试第二种方法:
hdcScreen = CreateDC(lpszDriver, lpszDevice, null, null);
nWidth = GetDeviceCaps(hdcScreen, HORZRES);
nHeight = GetDeviceCaps(hdcScreen, VERTRES);
这给了我同样的东西,宽度为 1280,高度为 720。我的头脑感到困惑!我怎样才能获得 1920x1080 的分辨率?
同样的方法为我提供了主显示器的正确尺寸,所以我很困惑。
EDIT
我刚刚尝试了第三种方法,但仍然存在同样的问题:
var jsMonitorEnumProc = function(hMonitor, hdcMonitor, lprcMonitor, dwData) {
xTopLeft = lprcMonitor.contents.left;
yTopLeft = lprcMonitor.contents.top;
nWidth = lprcMonitor.contents.right - xTopLeft;
nHeight = lprcMonitor.contents.bottom - yTopLeft;
return true;
}
EnumDisplayMonitors(null, null, jsMonitorEnumProc, 0);
这给了我以下的正确信息:
_RECT(0, 0, 1280, 1024)
_RECT(-1920, -1080, -640, -360)
第一个是我的主显示器,我们看到从下到上给出 1280,从右到左给出 1024,这是正确的,我的主显示器是 1280 x 1024。
但第二台显示器的高度为 -360 - -1080,宽度为 720,宽度为 -640 - -1920。我用它来截取所有显示器的屏幕截图,第二个显示器被剪掉了。