c++ - 如何使用Qt在windows窗体中显示桌面?

标签 c++ winforms qt winapi

<分区>

我正在做一个小的个人项目。我想在窗口(窗体)中显示实时桌面 View 。这可能吗?我正在使用 C++ 开发 Qt Designer/Creator。请提供指导文件,教程(如果有的话)。

我正在努力实现这一目标: enter image description here

最佳答案

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

这是一个小例子:

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

小部件.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));
}

这很简单...您每 2000 毫秒截取一次屏幕截图并将它们显示在 QLabel 上。 我建议你看看 screenshot example .我的例子是它的简化版本。

结果是:

enter image description here

如果您正在寻找类似屏幕共享的应用程序,您应该实现窗口的鼠标事件并获取点的坐标。然后处理它们以匹配原始桌面的屏幕分辨率并将这些点发送到系统以供点击。这是特定于平台的,您应该根据平台检查 POSIX/WinAPI 函数。

关于c++ - 如何使用Qt在windows窗体中显示桌面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26583929/

相关文章:

c++ - 确定指针在文本文件中的位置

C++0x 3d map 像 php 关联数组一样初始化

html - 在 QTWebKit 中从各种来源加载图像

qt - Qt qmake工具dry run模式如何实现

c++ - 通过指向实例的静态指针访问成员变量

C++ Dll 注入(inject)——Hello world dll 仅在注入(inject)到注入(inject)它的同一个 .exe 时才有效

c# - 通用 EventArgs 和扩展方法

C# 如何禁用 webbrowser 使用

vb.net - 为什么我的标签不显示在 VB.NET 表单中

c++ - 如何停止运行阻塞永远循环的 QThread?