如何使用Qt在Windows窗体中显示桌面?

问题描述:

我正在开展小型个人项目。我想在窗口(表单)中显示实时桌面视图。是否有可能?我正在使用C++开发Qt Designer/Creator。请为我提供指南文件,如果有的话。如何使用Qt在Windows窗体中显示桌面?

我米试图实现这一点: enter image description here

+0

你想只显示桌面,或者你想能够点击/键入那里等?这个应用程序运行在同一个桌面上? – Ezee 2014-10-27 08:57:42

+0

我只想显示桌面的实时视图,就像电影一样,但是在一个窗口(窗体)中,并且是这是运行应用程序的相同桌面。 – Gates 2014-10-27 09:04:09

+1

这些文章可能会有帮助:[1](http://*.com/questions/531684/what-is-the-best-way-to-take-screenshots-of-a-window-with-c - 在Windows中)[2](http://*.com/questions/664841/how-to-capture-a-video-of-my-desktop-with-net) – Ezee 2014-10-27 09:13:25

你需要的是不断采取屏幕截图,并在标签上显示出来:

这里有一个小例子:

SimpleScreenCapture .pro:

QT  += core gui 

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 

TARGET = SimpleScreenCapture 
TEMPLATE = app 


SOURCES += main.cpp\ 
     widget.cpp 

HEADERS += widget.h 

main.cpp中:

#include "widget.h" 
#include <QApplication> 

int main(int argc, char *argv[]) 
{ 
    QApplication a(argc, argv); 

    Widget w; 
    w.show(); 

    return a.exec(); 
} 

widget.h:

#ifndef WIDGET_H 
#define WIDGET_H 

#include <QWidget> 

class QLabel; 
class QVBoxLayout; 
class QTimer; 

class Widget : public QWidget 
{ 
    Q_OBJECT 

public: 
    Widget(QWidget *parent = 0); 
    ~Widget(); 

private slots: 
    void takeScreenShot(); 

private: 
    QLabel *screenshotLabel; 
    QPixmap originalPixmap; 
    QVBoxLayout *mainLayout; 
    QTimer *timer; 
}; 

#endif // WIDGET_H 

widget.cpp:

#include "widget.h" 

#include <QLabel> 
#include <QVBoxLayout> 
#include <QTimer> 
#include <QScreen> 
#include <QGuiApplication> 

Widget::Widget(QWidget *parent) 
    : QWidget(parent) 
{ 
    timer = new QTimer(this); 
    timer->setInterval(2000); 

    screenshotLabel = new QLabel; 
    screenshotLabel->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding); 
    screenshotLabel->setAlignment(Qt::AlignCenter); 
    screenshotLabel->setMinimumSize(240, 160); 

    mainLayout = new QVBoxLayout; 

    mainLayout->addWidget(screenshotLabel); 
    setLayout(mainLayout); 

    connect(timer, SIGNAL(timeout()), SLOT(takeScreenShot())); 

    timer->start(); 
} 

Widget::~Widget() 
{ 

} 

void Widget::takeScreenShot() 
{ 
    originalPixmap = QPixmap(); 

    QScreen *screen = QGuiApplication::primaryScreen(); 
    if (screen) 
    { 
     originalPixmap = screen->grabWindow(0); 
    } 

    screenshotLabel->setPixmap(originalPixmap.scaled(screenshotLabel->size(), 
                Qt::KeepAspectRatio, 
                Qt::SmoothTransformation)); 
} 

很简单...你采取截图的每2000ms和将它们显示在QLabel上。 我建议你看看screenshot example。我的例子是它的简化版本。

结果是:

enter image description here

如果你正在寻找你应该实现窗口的鼠标事件,并采取点的坐标的屏幕股一样的应用。处理它们以匹配原始桌面的屏幕分辨率,并将点发送到系统进行单击。这是平台特定的,你应该根据平台检查POSIX/WinAPI函数。

+0

这正是我想要的。谢谢!但是,这是我在双显示器上工作的一个小问题,所以它将两者都截图为单一。我如何截屏主屏幕? – Gates 2014-10-28 05:27:37

+0

@门嗯...你确定吗?我也有2台显示器,它只需要在主屏幕上显示屏幕截图...代码'QScreen * screen = QGuiApplication :: primaryScreen();'也表明...你确定你的设置配置好吗?你有没有尝试过使用'QGuiApplication :: screens()'玩,并试图抓住不同的屏幕,看看会发生什么? – Iuliu 2014-10-28 09:57:34

+0

这个问题发生在ubuntu不在Windows 7上!我会弄清楚。非常感谢你! – Gates 2014-10-28 12:12:02