Get Screen Resolution C++

Asked

Viewed 1,023 times

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++?

2 answers

9


On Windows?

Use Getsystemmetrics().

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

On Linux?

#include <X11/Xlib.h>

Display* disp = XOpenDisplay(NULL);
Screen*  scrn = DefaultScreenOfDisplay(disp);
int height = scrn->height;
int width  = scrn->width;

9

My colleague @Rodrigoguiotti’s answer is absolutely right and should be accepted. :)

But, just to complement: if you have additional intentions (for example, to capture the screen image to do something about it), it may be easier to use some other more complete library than to directly use the basic functions of the operating system. In that case, my suggestion would be Qt, because with it it is very easy to do this kind of checking and handling with the same code on different platforms.

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

#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 the execution of this code is first a dialog like the following:

inserir a descrição da imagem aqui

And then a window with the image captured from your main monitor, as follows:

inserir a descrição da imagem aqui

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

  • 1

    +1 just because QT is cross-Platform and supports different compilers too :D - great example!

  • 1

    @Guilhermenascimento We Qt lovers are everywhere! : )rs

  • Thank you all.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.