Search code examples
qtqt5screen-captureopenprocess

open a process and capture screen of its area in c++ on Windows using Qt


how to open a process and capture the screen of its area into a image file in c++ on Windows? i am using Qt, but it seems Qt doesnot have API for this. so i want to know is there any API or lib to do this. thank you


Solution

  • My first attempt would be something like this. It only captures the content of the window and not the window by itself. Do you also want to capture the whole window or screen?

    #include <QApplication>
    #include <QTreeView>
    #include <QScreen>
    #include <QPixmap>
    #include <QTimer>
    
    int main(int argc, char** args) {
        QApplication app(argc, args);
        auto view = new QTreeView;
        view->show();
        QTimer::singleShot(10, [&]() {
            auto screen = app.primaryScreen();
            auto pixmap = screen->grabWindow(view->winId());
            pixmap.save("Screenshot.png");
        });
        app.exec();
    }