要获取扩展屏幕相对于主屏幕的位置,可以使用 Windows API 中的 EnumDisplayMonitors 函数来遍历所有显示器,并使用 GetMonitorInfo 函数来获取显示器的信息。

下面是一个示例代码,可以获取扩展屏幕相对于主屏幕的位置:

#include <Windows.h>

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) {
    MONITORINFOEX monitorInfo;
    monitorInfo.cbSize = sizeof(MONITORINFOEX);
    GetMonitorInfo(hMonitor, &monitorInfo);

    // 判断是否为扩展屏幕
    if ((monitorInfo.dwFlags & MONITORINFOF_PRIMARY) == 0) {
        // 扩展屏幕的位置
        int x = monitorInfo.rcMonitor.left;
        int y = monitorInfo.rcMonitor.top;

        // 输出扩展屏幕的位置
        printf('Extension Monitor Position: (%d, %d)\n', x, y);
    }

    return TRUE;
}

int main() {
    EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, 0);

    return 0;
}

这段代码使用 EnumDisplayMonitors 函数遍历所有显示器,然后在回调函数 MonitorEnumProc 中获取每个显示器的位置信息。其中,如果显示器的 dwFlags 字段不包含 MONITORINFOF_PRIMARY 标志,就表示该显示器为扩展屏幕,可以通过 rcMonitor 结构体中的 left 和 top 字段获取其相对于主屏幕的位置。

请注意,上述代码中的 printf 函数是用于控制台输出,如果是在 GUI 应用程序中使用,可以根据具体情况替换为适合的输出方式。

C++ 获取扩展屏幕相对于主屏幕的位置

原文地址: http://www.cveoy.top/t/topic/pb7R 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录