Get Screen Resolution C ++

8

I am developing a system and would like to know if there is any function / library, anything, to get the screen resolution, if it is for example:

1280x720, 1920x1080, 1366x768, etc...

Is there any way to check this in C++ ?

    
asked by anonymous 11.03.2016 / 19:49

2 answers

9

In Windows?

Use GetSystemMetrics ().

#include <windows.h>
Int x = GetSystemMetrics(SM_CXSCREEN);
Int y = GetSystemMetrics(SM_CYSCREEN);

In Linux?

#include <X11/Xlib.h>

Display* disp = XOpenDisplay(NULL);
Screen*  scrn = DefaultScreenOfDisplay(disp);
int height = scrn->height;
int width  = scrn->width;
    
11.03.2016 / 19:53
9

The reply from the colleague @RodrigoGuiotti is very accurate and should be accepted. :)

But for the sake of completeness: If you have additional intentions (for example, capturing the screen image to do something about it), it may be easier to use some other more complete library than directly using the basic system functions operational. In that case, my suggestion would be Qt because with it it is very easy to do this kind of checking and manipulation with the same code in different platforms.

The following is a very simple example that I have prepared, which identifies the resolutions of all the screens (if you have more than one monitor, for example - as is my case) and also demonstrates how to get a screen image .

#include <QApplication>
#include <QScreen>
#include <QMessageBox>
#include <QLabel>

int main(int argc, char** argv)
{
    QApplication oApp(argc, argv);

    // Pega a lista de telas disponíveis
    QList<QScreen *> lScreens = QApplication::screens();

    // Gera o relatório de resoluções com todas as telas
    QRect oRes;
    QString sScreen;
    QString sReport = QString("Relatório de resoluções das %1 tela(s):\n\n").arg(lScreens.size());
    foreach(QScreen *pScreen, lScreens)
    {
        oRes = pScreen->availableGeometry();
        sScreen = QString("    Tela %1: %2 x %3\n").arg(pScreen->name()).arg(oRes.width()).arg(oRes.height());
        sReport += sScreen;
    }

    // Exibe as informações da resolução da tela
    QMessageBox::information(NULL, "Resolução", sReport, QMessageBox::Ok);

    // Captura a imagem da tela principal
    QPixmap oScreenImage = QApplication::primaryScreen()->grabWindow(0);

    // Exibe a imagem em uma janela
    QLabel oImage;
    oImage.setWindowTitle("Captura da imagem da tela principal");
    oImage.setPixmap(oScreenImage.scaled(QSize(800, 600), Qt::KeepAspectRatio, Qt::FastTransformation));
    oImage.show();

    return oApp.exec();
}

The result of executing this code is primarily a dialog like the following:

Andthenawindowwiththecapturedimageofyourmainmonitor,asfollows:

  

Note that I ran in Windows, but it will work the same way   on Linux, on the Mac, ...

    
11.03.2016 / 20:44