C Qt

思路

首先先获取到当前桌面的完整图片,然后将其放到一个全屏的透明窗口之中,之后,在窗口上进行绘制矩形操作,然后获取到绘制的区域即可。

核心代码

新建一个screenview的界面,设置为全屏窗口模式

  1. setWindowState(Qt::WindowActive|Qt::WindowFullScreen);

2021-08-04-23-29-59-102532.png
获取到桌面的完成图片,保存图片,之前已经知道如何获取全屏图片。

  1. screen =QGuiApplication::primaryScreen();
  2. if(const QWindow *window=windowHandle())
  3. screen=window->screen();
  4. if(!screen)
  5. return ;
  6. originalPixmap=screen->grabWindow(0);

重写四个方法paintEventmousePressEventmouseMoveEventmouseReleaseEvent,后面的3个鼠标方法,主要用来获取鼠标的位置,以便能够在绘制方法里头绘制出矩形区域。

  1. //截图
  2. void screenview::paintEvent(QPaintEvent *event){
  3. painter.begin(this);
  4. painter.setPen(QPen(Qt::red,2));
  5. painter.drawPixmap(0,0,originalPixmap);
  6. if(sx>=0&&sy>=0)
  7. {
  8. painter.drawRect(QRect(sx,sy,ex-sx,ey-sy));
  9. }
  10. painter.end();
  11. }
  12. void screenview::mousePressEvent(QMouseEvent *event)
  13. {
  14. if(event->button()==Qt::LeftButton){
  15. sx=event->x();
  16. sy=event->y();
  17. startpoint=event->pos();
  18. }
  19. update();
  20. }
  21. void screenview::mouseMoveEvent(QMouseEvent *event){
  22. ex=event->x();
  23. ey=event->y();
  24. endpoint=event->pos();
  25. update();
  26. }
  27. void screenview::mouseReleaseEvent(QMouseEvent *event){
  28. this->close();
  29. ex=event->x();
  30. ey=event->y();
  31. //获取到区域截图
  32. sourcePixmap=originalPixmap.copy(sx*Scale,sy*Scale,(ex-sx)*Scale,(ey-sy)*Scale);
  33. }

完整代码

  1. //screenview.h
  2. #ifndef SCREENVIEW_H
  3. #define SCREENVIEW_H
  4. #include <QWidget>
  5. #include <QPainter>
  6. #include <QScreen>
  7. #include <QWindow>
  8. #include <QPixmap>
  9. #include <QMouseEvent>
  10. #include <QRubberBand>
  11. #include <QDateTime>
  12. #include <QLabel>
  13. #include "editwindow.h"
  14. #include "mainwindow.h"
  15. //添加
  16. class QRubberBand;
  17. namespace Ui {
  18. class screenview;
  19. }
  20. class screenview : public QWidget
  21. {
  22. Q_OBJECT
  23. public:
  24. screenview(QWidget *parent = nullptr,QList<QRect> *ListRect=nullptr,int screentype=0);
  25. ~screenview();
  26. protected:
  27. void paintEvent(QPaintEvent *event);
  28. void mousePressEvent(QMouseEvent *event);
  29. void mouseMoveEvent(QMouseEvent *event);
  30. protected slots:
  31. void mouseReleaseEvent(QMouseEvent *event);
  32. void receiveData(QString str); //接收传递过来的数据的槽
  33. private:
  34. Ui::screenview *ui;
  35. QPainter painter;
  36. QPixmap originalPixmap;
  37. QPixmap sourcePixmap;
  38. QScreen *screen;
  39. QRubberBand *rubberBand;
  40. QPoint startpoint;
  41. QPoint endpoint;
  42. //记录鼠标位置
  43. int sx,sy,ex,ey;
  44. //记录矩形的大小
  45. int rw,rh;
  46. //截图类型
  47. int shottype;
  48. //固定截图的坐标
  49. int fixedx,fixedy;
  50. //固定大小的矩形
  51. QRect fixedRect;
  52. //窗口放大倍数
  53. float Scale;
  54. //保存所有的矩形
  55. QList<QRect> *ListRect;
  56. //当前选中的窗口
  57. QRect CurrentWindow;
  58. signals:
  59. void senddata(QPixmap sourcePixmap);
  60. };
  61. #endif // SCREENVIEW_H
  1. #include "screenview.h"
  2. #include "ui_screenview.h"
  3. screenview::screenview(QWidget *parent,QList<QRect> *listRect,int screentype)
  4. :QWidget(parent),ui(new Ui::screenview)
  5. {
  6. ui->setupUi(this);
  7. rubberBand=NULL;
  8. shottype=screentype;
  9. screen =QGuiApplication::primaryScreen();
  10. if(const QWindow *window=windowHandle())
  11. screen=window->screen();
  12. if(!screen)
  13. return ;
  14. originalPixmap=screen->grabWindow(0);
  15. //全屏窗口
  16. setWindowState(Qt::WindowActive|Qt::WindowFullScreen);
  17. //解决只有鼠标按下时才捕捉鼠标移动
  18. setMouseTracking(true);
  19. //获取系统设置的显示比例
  20. float swidth=this->width();
  21. float rwidth=originalPixmap.width();
  22. Scale=rwidth/swidth;
  23. ListRect=listRect;
  24. }
  25. screenview::~screenview()
  26. {
  27. delete ui;
  28. }
  29. //截图
  30. void screenview::paintEvent(QPaintEvent *event){
  31. painter.begin(this);
  32. painter.setPen(QPen(Qt::red,2));
  33. painter.drawPixmap(0,0,originalPixmap);
  34. if(shottype==1)
  35. {
  36. //固定窗口截图
  37. rw=300;
  38. rh=200;
  39. int fixx=(ex-rw/2)>0?(ex-rw/2):0;
  40. int fixy=(ey-rh/2)>0?(ey-rh/2):0;
  41. fixedRect=QRect(fixx,fixy,rw,rh);
  42. painter.drawRect(fixedRect);
  43. painter.setPen(QPen(Qt::black,2));
  44. painter.drawText(fixx,fixy+rh+10,"F2调整大小");
  45. }else if(shottype==0)
  46. {
  47. if(sx>=0&&sy>=0)
  48. {
  49. painter.drawRect(QRect(sx,sy,ex-sx,ey-sy));
  50. }
  51. }
  52. painter.end();
  53. }
  54. void screenview::mousePressEvent(QMouseEvent *event)
  55. {
  56. if(event->button()==Qt::LeftButton){
  57. sx=event->x();
  58. sy=event->y();
  59. startpoint=event->pos();
  60. if(shottype==1){
  61. this->close();
  62. sourcePixmap=originalPixmap.copy(fixedRect.x()*Scale,fixedRect.y()*Scale,fixedRect.width()*Scale,fixedRect.height()*Scale);
  63. emit senddata(sourcePixmap);
  64. }
  65. else if(shottype==2){
  66. this->close();
  67. sourcePixmap=originalPixmap.copy(CurrentWindow.x()*Scale,CurrentWindow.y()*Scale,CurrentWindow.width()*Scale,CurrentWindow.height()*Scale);
  68. emit senddata(sourcePixmap);
  69. }
  70. }
  71. update();
  72. }
  73. void screenview::mouseMoveEvent(QMouseEvent *event){
  74. ex=event->x();
  75. ey=event->y();
  76. endpoint=event->pos();
  77. update();
  78. }
  79. void screenview::mouseReleaseEvent(QMouseEvent *event){
  80. this->close();
  81. ex=event->x();
  82. ey=event->y();
  83. sourcePixmap=originalPixmap.copy(sx*Scale,sy*Scale,(ex-sx)*Scale,(ey-sy)*Scale);
  84. //将获取到的图片内容发送到编辑窗口,可以直接保存到本地即可
  85. emit senddata(sourcePixmap);
  86. }