我正在開展小型個人項目。我想在窗口(表單)中顯示實時桌面視圖。是否有可能?我正在使用C++開發Qt Designer/Creator。請爲我提供指南文件,如果有的話。如何使用Qt在Windows窗體中顯示桌面?
我米試圖實現這一點:
我正在開展小型個人項目。我想在窗口(表單)中顯示實時桌面視圖。是否有可能?我正在使用C++開發Qt Designer/Creator。請爲我提供指南文件,如果有的話。如何使用Qt在Windows窗體中顯示桌面?
我米試圖實現這一點:
你需要的是不斷採取屏幕截圖,並在標籤上顯示出來:
這裏有一個小例子:
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。我的例子是它的簡化版本。
結果是:
如果你正在尋找你應該實現窗口的鼠標事件,並採取點的座標的屏幕股一樣的應用。處理它們以匹配原始桌面的屏幕分辨率,並將點發送到系統進行單擊。這是平臺特定的,你應該根據平臺檢查POSIX/WinAPI函數。
這正是我想要的。謝謝!但是,這是我在雙顯示器上工作的一個小問題,所以它將兩者都截圖爲單一。我如何截屏主屏幕? – Gates 2014-10-28 05:27:37
@門嗯...你確定嗎?我也有2臺顯示器,它只需要在主屏幕上顯示屏幕截圖...代碼'QScreen * screen = QGuiApplication :: primaryScreen();'也表明...你確定你的設置配置好嗎?你有沒有嘗試過使用'QGuiApplication :: screens()'玩,並試圖抓住不同的屏幕,看看會發生什麼? – Iuliu 2014-10-28 09:57:34
這個問題發生在ubuntu不在Windows 7上!我會弄清楚。非常感謝你! – Gates 2014-10-28 12:12:02
你想只顯示桌面,或者你想能夠點擊/鍵入那裏等?這個應用程序運行在同一個桌面上? – Ezee 2014-10-27 08:57:42
我只想顯示桌面的實時視圖,就像電影一樣,但是在一個窗口(窗體)中,並且是這是運行應用程序的相同桌面。 – Gates 2014-10-27 09:04:09
這些文章可能會有幫助:[1](http://stackoverflow.com/questions/531684/what-is-the-best-way-to-take-screenshots-of-a-window-with-c - 在Windows中)[2](http://stackoverflow.com/questions/664841/how-to-capture-a-video-of-my-desktop-with-net) – Ezee 2014-10-27 09:13:25