Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

c++ - How do I get the dimensions (resolution) of each display?

I need help on how to retrieve the resolutions of my screens, as shown in the image below.

one 1680x1050, another 1366x768, and a third 1280x800

I found this documentation and it was really helpful. Here's the code that I tried, based on those docs:

int numberOfScreens = GetSystemMetrics(SM_CMONITORS);
int width           = GetSystemMetrics(SM_CXSCREEN);
int height          = GetSystemMetrics(SM_CYSCREEN);

std::cout << "Number of monitors: " << numberOfScreens << "
";  // returns 3
std::cout << "Width:"               << width           << "
";
std::cout << "Height:"              << height          << "
";

However, it only identifies and gives information about the main monitor. How do I get information about the other monitors?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
#include <Windows.h>

BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor,
                              HDC      hdcMonitor,
                              LPRECT   lprcMonitor,
                              LPARAM   dwData)
{
    MONITORINFO info;
    info.cbSize = sizeof(info);
    if (GetMonitorInfo(hMonitor, &info))
    {
        std::cout << "Monitor x: "<< std::abs(info.rcMonitor.left - info.rcMonitor.right)
                  <<" y: "        << std::abs(info.rcMonitor.top  - info.rcMonitor.bottom)
                  << std::endl;
    }
    return TRUE;  // continue enumerating
}

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

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...