【问题标题】:How to display desktop in windows form using Qt? [closed]如何使用 Qt 以 Windows 形式显示桌面? [关闭]
【发布时间】:2014-10-27 08:52:50
【问题描述】:

我正在从事小型个人项目。我想在窗口(表单)中显示实时桌面视图。有可能吗?我正在使用 C++ 开发 Qt Designer/Creator。请提供指导文件,如果有教程。

我正在努力实现这一目标:

【问题讨论】:

  • 您想只显示桌面还是希望能够在此处单击/键入等等?它与运行应用程序的桌面相同吗?
  • 我只想显示桌面的实时视图,就像电影一样,但是在一个窗口(表单)中,是的,它是运行应用程序的同一个桌面。
  • 这些文章可能对你有用:12
  • 如果您需要真实的实时视图 - 那么您应该查看特定于平台的内容。例如,用于抓取屏幕和执行必要的缩放/转换的 directx 东西。 Qt 没有开箱即用的解决方案。

标签: c++ winforms qt winapi


【解决方案1】:

你想要的是不断截取屏幕截图并显示在标签上:

这是一个小例子:

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 嗯...你确定吗?我也有 2 台显示器,它只在主显示器上截屏...代码QScreen *screen = QGuiApplication::primaryScreen(); 还表明...您确定您的设置配置正确吗?您是否尝试过使用QGuiApplication::screens() 并尝试抓取不同的屏幕,看看会发生什么?
  • 这个问题出现在 ubuntu 而不是 windows 7 上!我会弄清楚。非常感谢!
猜你喜欢
  • 2016-07-03
  • 2021-02-12
  • 2013-09-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-01
  • 2011-07-26
相关资源
最近更新 更多