GetMonitorInfo 和 GetDeviceCaps 中第二个显示器的宽度和高度不正确

2023-12-02

我正在尝试获取左上角的 x,y 和右下角的 x,y。并计算显示器的宽度和高度。

我的辅助显示器是 1920x1080,如我的显示设置屏幕截图所示:

我通过两种方式获取显示器尺寸。下面的代码是 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。我用它来截取所有显示器的屏幕截图,第二个显示器被剪掉了。


在 Windows 8.1 64 位上的非 dpi 感知应用程序 32 位 Firefox 中,我能够通过使用获得正确的尺寸EnumDisplaySettings使用大小为 220 的 DISPLAY_DEVICE 结构。

js-ctypes:

// start - get all monitor resolutions
var iDevNum = -1;
while (true) {
    iDevNum++;
    var lpDisplayDevice = ostypes.TYPE.DISPLAY_DEVICE();
    lpDisplayDevice.cb = ostypes.TYPE.DISPLAY_DEVICE.size;
    var rez_EnumDisplayDevices = ostypes.API('EnumDisplayDevices')(null, iDevNum, lpDisplayDevice.address(), 0);
    //console.info('rez_EnumDisplayDevices:', rez_EnumDisplayDevices.toString(), uneval(rez_EnumDisplayDevices), cutils.jscGetDeepest(rez_EnumDisplayDevices));

    if (cutils.jscEqual(rez_EnumDisplayDevices, 0)) { // ctypes.winLastError != 0
        // iDevNum is greater than the largest device index.
        break;
    }

    console.info('lpDisplayDevice.DeviceName:', lpDisplayDevice.DeviceName.readString()); // "\\.\DISPLAY1" till "\\.\DISPLAY4"

    if (lpDisplayDevice.StateFlags & ostypes.CONST.DISPLAY_DEVICE_ATTACHED_TO_DESKTOP) {
        console.log('is monitor');

        var dm = ostypes.TYPE.DEVMODE(); // SIZEOF_DEVMODE = 148
        console.info('dm.size:', ostypes.TYPE.DEVMODE.size);
        //dm.dmFields = ostypes.CONST.DM_PELSWIDTH;
        //dm.dmSize = ostypes.TYPE.DEVMODE.size;

        console.log('iDevNum:', iDevNum, lpDisplayDevice.DeviceName.readString());
        var rez_EnumDisplaySettings = ostypes.API('EnumDisplaySettings')(lpDisplayDevice.DeviceName, ostypes.CONST.ENUM_CURRENT_SETTINGS, dm.address());
        //console.info('rez_EnumDisplaySettings:', rez_EnumDisplaySettings.toString(), uneval(rez_EnumDisplaySettings), cutils.jscGetDeepest(rez_EnumDisplaySettings));
        //console.info('dm:', dm.toString());

        collMonInfos.push({
            x: parseInt(cutils.jscGetDeepest(dm.u.dmPosition.x)),
            y: parseInt(cutils.jscGetDeepest(dm.u.dmPosition.y)),
            w: parseInt(cutils.jscGetDeepest(dm.dmPelsWidth)),
            h: parseInt(cutils.jscGetDeepest(dm.dmPelsHeight)),
            screenshot: null, // for winnt, each collMonInfos entry has screenshot data
            otherInfo: {
                nBPP: parseInt(cutils.jscGetDeepest(dm.dmBitsPerPel)),
                lpszDriver: null,
                lpszDevice: lpDisplayDevice.DeviceName
            }
        });
    }
}
// end - get all monitor resolutions
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

GetMonitorInfo 和 GetDeviceCaps 中第二个显示器的宽度和高度不正确 的相关文章

随机推荐