Qt-Qt之grabWindow实现截图功能
相关资料:
https://download.csdn.net/download/zhujianqiangqq/86539726 CSDN代码包下载
实例代码;
.pro
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 QT += core gui 2 3 greaterThan(QT_MAJOR_VERSION, 4): QT += widgets 4 5 CONFIG += c++11 6 7 # The following define makes your compiler emit warnings if you use 8 # any Qt feature that has been marked deprecated (the exact warnings 9 # depend on your compiler). Please consult the documentation of the 10 # deprecated API in order to know how to port your code away from it. 11 DEFINES += QT_DEPRECATED_WARNINGS 12 13 # You can also make your code fail to compile if it uses deprecated APIs. 14 # In order to do so, uncomment the following line. 15 # You can also select to disable deprecated APIs only up to a certain version of Qt. 16 #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 17 18 SOURCES += \ 19 main.cpp \ 20 mainwindow.cpp 21 22 HEADERS += \ 23 mainwindow.h 24 25 FORMS += \ 26 mainwindow.ui 27 28 LIBS += -lgdi32 29 LIBS += -luser32 30 31 # Default rules for deployment. 32 qnx: target.path = /tmp/$${TARGET}/bin 33 else: unix:!android: target.path = /opt/$${TARGET}/bin 34 !isEmpty(target.path): INSTALLS += target 35 36 RESOURCES +=
main.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "mainwindow.h" 2 3 #include <QApplication> 4 5 int main(int argc, char *argv[]) 6 { 7 QApplication a(argc, argv); 8 MainWindow w; 9 w.show(); 10 return a.exec(); 11 }
mainwindow.h
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #ifndef MAINWINDOW_H 2 #define MAINWINDOW_H 3 4 #include <QMainWindow> 5 6 #include <QGuiApplication> 7 #include <QScreen> 8 #include <QDesktopWidget> 9 #include <Qpainter> 10 11 #include "windows.h" 12 #include "winuser.h" 13 #include <QWindow> 14 #include <QPushButton> 15 #include <QVBoxLayout> 16 #include <QDebug> 17 18 QT_BEGIN_NAMESPACE 19 namespace Ui { class MainWindow; } 20 QT_END_NAMESPACE 21 22 class MainWindow : public QMainWindow 23 { 24 Q_OBJECT 25 26 public: 27 MainWindow(QWidget *parent = nullptr); 28 ~MainWindow(); 29 30 void paintEvent(QPaintEvent* e) override; 31 void timerEvent(QTimerEvent* e); 32 private: 33 Ui::MainWindow *ui; 34 QScreen* m_pScreen = nullptr; 35 QWindow *calcWin; 36 QWidget *calcWidget; 37 QWidget *owidget; 38 }; 39 #endif // MAINWINDOW_H
mainwindow.cpp
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include "mainwindow.h" 2 #include "ui_mainwindow.h" 3 4 MainWindow::MainWindow(QWidget *parent) 5 : QMainWindow(parent) 6 , ui(new Ui::MainWindow) 7 { 8 ui->setupUi(this); 9 setWindowTitle(QStringLiteral("Qt之grabWindow实现截图功能")); 10 m_pScreen = QGuiApplication::primaryScreen(); 11 startTimer(40); //1秒25帧 12 } 13 14 MainWindow::~MainWindow() 15 { 16 delete ui; 17 } 18 19 void MainWindow::paintEvent(QPaintEvent *e) 20 { 21 LPCWSTR className = TEXT("Qt5QWindowIcon");// Qt5QWindowIcon // Notepad++ 22 HWND hwnd = FindWindow(className, NULL); 23 // 截取全屏, 指定窗口Id进行截屏 24 // QPixmap pix = m_pScreen->grabWindow(QApplication::desktop()->winId()); 25 QPixmap pix = m_pScreen->grabWindow((WId)hwnd); 26 //绘制截屏 27 QPainter p; 28 p.begin(this); 29 p.drawImage(QPoint(0, 0), pix.toImage()); 30 p.end(); 31 } 32 33 void MainWindow::timerEvent(QTimerEvent *e) 34 { 35 update(); //更新窗口 36 }
mainwindow.ui
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <ui version="4.0"> 3 <class>MainWindow</class> 4 <widget class="QMainWindow" name="MainWindow"> 5 <property name="geometry"> 6 <rect> 7 <x>0</x> 8 <y>0</y> 9 <width>976</width> 10 <height>741</height> 11 </rect> 12 </property> 13 <property name="windowTitle"> 14 <string>MainWindow</string> 15 </property> 16 <widget class="QWidget" name="centralwidget"> 17 <widget class="QWidget" name="widget" native="true"> 18 <property name="geometry"> 19 <rect> 20 <x>420</x> 21 <y>120</y> 22 <width>321</width> 23 <height>251</height> 24 </rect> 25 </property> 26 </widget> 27 </widget> 28 </widget> 29 <resources/> 30 <connections/> 31 </ui>
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417075318%E5%8A%A0%E6%88%91QQ.jpg)
![](https://images.cnblogs.com/cnblogs_com/FKdelphi/1101510/o_200417080925%E5%8A%A0%E6%88%91%E5%BE%AE%E4%BF%A1.jpg)