要获取主屏幕和扩展屏幕的像素大小和位置,可以使用 Windows API 中的 GetSystemMetrics 和 EnumDisplayMonitors 函数。

以下是一个示例代码,可以获取主屏幕和扩展屏幕的像素大小和位置:

#include <Windows.h>
#include <iostream>

int main()
{
    // 获取主屏幕的像素大小和位置
    int mainScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    int mainScreenHeight = GetSystemMetrics(SM_CYSCREEN);
    int mainScreenLeft = 0;
    int mainScreenTop = 0;

    std::cout << 'Main screen width: ' << mainScreenWidth << std::endl;
    std::cout << 'Main screen height: ' << mainScreenHeight << std::endl;

    // 获取扩展屏幕的像素大小和位置
    BOOL success = EnumDisplayMonitors(NULL, NULL, [](HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData) -> BOOL
    {
        int monitorWidth = lprcMonitor->right - lprcMonitor->left;
        int monitorHeight = lprcMonitor->bottom - lprcMonitor->top;
        int monitorLeft = lprcMonitor->left;
        int monitorTop = lprcMonitor->top;

        std::cout << 'Monitor width: ' << monitorWidth << std::endl;
        std::cout << 'Monitor height: ' << monitorHeight << std::endl;
        std::cout << 'Monitor left: ' << monitorLeft << std::endl;
        std::cout << 'Monitor top: ' << monitorTop << std::endl;

        return TRUE;
    }, NULL);

    return 0;
}

在上面的代码中,首先使用 GetSystemMetrics 函数获取主屏幕的像素大小,然后使用 EnumDisplayMonitors 函数遍历所有的显示器,获取扩展屏幕的像素大小和位置。

请注意,这个示例代码只获取了扩展屏幕的像素大小和位置,如果你的系统只有一个显示器,则不会输出任何信息。

希望对你有帮助!

C++ 获取 Windows 主屏幕和扩展屏幕的像素大小和位置

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

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