• 大小: 2.16M
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2024-04-22
  • 语言: C/C++
  • 标签: 屏幕  截图  

资源简介

Qt实现 屏幕截图

资源截图

代码片段和文件信息

#include “fullscreenwidget.h“
fullScreenWidget::fullScreenWidget()
{
  setWindowState(Qt::WindowActive|Qt::WindowFullScreen);
  tipWidth = 300; //温馨提示框的宽度
  tipHeight = 100; //温馨提示框的高度
  infoWidth = 100; //坐标信息框的宽度
  infoHeight = 50; //坐标信息框的高度
  initFullScreenWidget();
}

void fullScreenWidget::initSelectedMenu()
{
  savePixmapAction = new QAction(tr(“保存选区“)this);
  cancelAction = new QAction(tr(“重选“)this);
  quitAction = new QAction(tr(“退出“)this);
  contextMenu = new QMenu(this);

  connect(savePixmapActionSIGNAL(triggered())thisSLOT(savePixmap()));
  connect(cancelActionSIGNAL(triggered())thisSLOT(cancelSelectedRect()));
  connect(quitActionSIGNAL(triggered())thisSLOT(hide()));
}

void fullScreenWidget::savePixmap()
{
  QString fileName;
  fileName = QFileDialog::getSaveFileName(thistr(“保存图片“)QDir::currentPath()tr(“Images (*.jpg *.png *.bmp)“));
  if(fileName.isNull())
    return;

  shotPixmap.save(fileName);
  hide();
}

void fullScreenWidget::loadBackgroundPixmap(const QPixmap &bgPixmap)
{
  int widthheight;
  width = QApplication::desktop()->size().width();
  height = QApplication::desktop()->size().height();

  loadBackgroundPixmap(bgPixmap00widthheight);
}

void fullScreenWidget::loadBackgroundPixmap(const QPixmap &bgPixmap int x int y int width int height)
{
  loadPixmap = bgPixmap;
  screenx = x;
  screeny = y;
  screenwidth = width;
  screenheight = height;
  initFullScreenWidget();
}

QPixmap fullScreenWidget::getFullScreenPixmap()
{
  initFullScreenWidget();
  QPixmap result = QPixmap();
  result = QPixmap::grabWindow(QApplication::desktop()->winId()); //抓取当前屏幕的图片

  return result;
}

void fullScreenWidget::paintEvent(QPaintEvent *event)
{
  QColor shadowColor;
  shadowColor= QColor(000100); //阴影颜色设置
  painter.begin(this); //进行重绘

  painter.setPen(QPen(Qt::blue2Qt::SolidLineQt::FlatCap));//设置画笔
  painter.drawPixmap(screenxscreenyloadPixmap); //将背景图片画到窗体上
  painter.fillRect(screenxscreenyscreenwidthscreenheightshadowColor); //画影罩效果

  switch(currentShotState){
    case initShot:
      drawTipsText();
      break;
    case beginShot:
    case finishShot:
      selectedRect = getRect(beginPointendPoint); //获取选区
      drawSelectedPixmap();
      break;
    case beginMoveShot:
    case finishMoveShot:
      selectedRect = getMoveAllSelectedRect(); //获取选区
      drawSelectedPixmap();
      break;
    case beginControl:
    case finishControl:
      selectedRect = getMoveControlSelectedRect();
      drawSelectedPixmap();
      break;
    default:
      break;
  }
  drawXYWHInfo(); //打印坐标信息
  painter.end();  //重绘结束

  if(currentShotState == finishMoveShot || currentShotState == finishControl){
    updateBeginEndPointValue(selectedRect); //当移动完选区后,更新beginPointendPoint;为下一次移动做准备工作
  }

}

void fullScreenWidget::keyPressEvent(QKeyEvent *event)
{
  if(event->key() == Qt::Key_Escape){
    initFullScreenWidget();
    hide();
  }
}

void fullScreenWidget::mousePressEvent(QMouseEvent *event)
{
  //当开始进行拖动进行选择区域时

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2020-10-18 23:40  screenShot\
     文件        1093  2020-10-11 15:41  screenShot\.qmake.stash
     文件       34721  2020-10-11 15:41  screenShot\Makefile
     文件      142174  2020-10-11 15:41  screenShot\Makefile.Debug
     文件      142302  2020-10-11 15:41  screenShot\Makefile.Release
     文件       14426  2020-10-18 23:36  screenShot\cncT.pro.user
     文件       26550  2020-10-11 15:39  screenShot\cncT.pro.user.12
     文件       22603  2020-10-11 15:40  screenShot\cncT.pro.user.d76827c
     目录           0  2020-10-18 23:36  screenShot\debug\
     文件     3710092  2020-10-18 23:36  screenShot\debug\cncT.exe
     文件      992131  2020-10-11 15:41  screenShot\debug\fullscreenwidget.o
     文件      749117  2020-10-18 23:36  screenShot\debug\main.o
     文件        5695  2020-10-11 15:41  screenShot\debug\moc_fullscreenwidget.cpp
     文件      878874  2020-10-11 15:41  screenShot\debug\moc_fullscreenwidget.o
     文件       14934  2020-10-11 15:41  screenShot\debug\moc_predefs.h
     文件        3949  2020-10-18 23:36  screenShot\debug\moc_widget.cpp
     文件      683922  2020-10-18 23:36  screenShot\debug\moc_widget.o
     文件      787720  2020-10-18 23:36  screenShot\debug\widget.o
     文件       16364  2020-10-11 15:30  screenShot\fullscreenwidget.cpp
     文件        3813  2020-10-11 15:04  screenShot\fullscreenwidget.h
     文件         168  2020-10-11 10:04  screenShot\main.cpp
     文件         824  2020-10-12 02:24  screenShot\widget.h
     文件         397  2020-10-11 14:37  screenShot\screenShot.pro
     文件       22700  2020-10-18 23:40  screenShot\screenShot.pro.user
     文件        1506  2020-10-18 23:36  screenShot\ui_widget.h
     文件        2139  2020-10-16 01:47  screenShot\widget.cpp
     文件         701  2020-10-18 23:36  screenShot\widget.ui
     目录           0  2020-10-18 23:40  screenShot\release\

评论

共有 条评论