截图效果:
1、截图获取
该截图实现的是静态截图,实际就是先调用grabWindow函数获取窗口图片然后设置到截图控件中进行操作。
//通过调用事件循环来保证当前截图获取完整
QEventLoop loop;
QTimer::singleShot(100, &loop, SLOT(quit()));
loop.exec();
QPixmap pix=QApplication::primaryScreen()->grabWindow(0);
if(pix.isNull())
{
}
QImage image = pix.toImage();
grabWindow()函数官方解释:创建并返回一个像素图,该像素图是通过抓取受QRect(x, y, width, height)限制的给定窗口的内容构造的。
参数(x, y)指定窗口中的偏移量,而(width, height)指定要复制的区域。如果width为负值,则该函数将所有内容复制到窗口的右边框。如果height为负,则该函数将所有内容复制到窗口的底部。
可以使用QWidget::winId()函数检索窗口系统标识符(WId)。使用窗口标识符而不是QWidget的基本原理是允许抓取不属于应用程序、窗口系统框架等的窗口。
警告:抓取不属于应用程序的窗口在iOS等系统上是不支持的,其中沙箱/安全会阻止读取不属于应用程序的窗口的像素。
grabWindow()函数从屏幕上抓取像素,而不是从窗口中,也就是说,如果有另一个窗口部分或全部在你抓取的窗口上,你也会从覆盖的窗口中获取像素。鼠标光标一般不会被抓取。
注意在X11上,如果给定的窗口与根窗口的深度不同,并且另一个窗口部分或完全遮蔽了您抓取的窗口,那么您将无法从覆盖的窗口获得像素。像素图中模糊区域的内容将未定义且未初始化。
在Windows Vista及以上抓取分层窗口,这是通过设置Qt::WA_TranslucentBackground属性创建的,将不工作。相反,抓取桌面小部件应该可以工作。
警告:一般来说,抓取屏幕外的区域是不安全的。这取决于底层的窗口系统。
QApplication::primaryScreen()->grabWindow(0);表示当前应用最底层窗口,通常是桌面。
2、对截图区域进行编辑
1、设计截图处理类
ScreenshotToolWidget.h
#ifndef PRINTSCREENDISCUSSIONWIDGET_H
#define PRINTSCREENDISCUSSIONWIDGET_H
#include <QWidget>
#include "ScreenshotToolOperatioWidget.h"
#include "readqss.h"
class ScreenshotToolWidget : public QWidget
{
Q_OBJECT
public:
explicit ScreenshotToolWidget(QWidget *parent = 0);
~ScreenshotToolWidget();
public:
void setImage(QImage image); // 设置图片
void setIscut(bool isCut); // 设置是否截屏
public slots:
void slot_cancelScreenshotToolWidget(void); // 取消截屏
void slot_sendCutImage(void); // 发送截图
private:
enum SCALE_TYPE
{
SCALE_TYPE_NOSCALE, // 无缩放操作
SCALE_TYPE_LEFTTOP, // 左上角缩放
SCALE_TYPE_LEFTBOTTOM, // 左下角缩放
SCALE_TYPE_RIGTHTOP, // 右上角缩放
SCALE_TYPE_RIGHTBOTTOM, // 右下角缩放
SCALE_TYPE_MIDUP, // 上中角缩放
SCALE_TYPE_MIDBELLOW, // 下中缩放
SCALE_TYPE_MIDLEFT, // 左中角缩放
SCALE_TYPE_MIDRIGHT // 右中角缩放
};
signals:
void signal_cancelScreenshotToolWidget(QString imageFullName); // 取消截图
void signal_sendPrintScreenWidget(QString imageFullName); // 发送
protected:
void initMenuWidget();
void moveTargetArea(QPoint movePos); // 移动目标区域
void scaleTargetArea(ScreenshotToolWidget::SCALE_TYPE type, QPoint point); // 缩放目标区域
void changeMenuPos(void); // 操作菜单位置显示
void setControlPoint(void); // 设置控制点
protected:
void paintEvent(QPaintEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseMoveEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void resizeEvent(QResizeEvent *event);
private:
SCALE_TYPE _scaleType; // 缩放方向
QImage *_pImageBuffer; // 背景图像缓存
QImage _targetImage; // 选中的目标区域的图片
QPoint _lastMousePos; // 鼠标最后停留的位置
bool _mousePressed; // 鼠标被按下
bool _isCut; // 是否截取
ScreenshotToolOperatioWidget *_pScreenshotToolOperatioWidget; // 截图菜单界面
QRect _targetRect; // 目标区域
QRect _leftTopControlRect; // 左上角控制点
QRect _rightTopControlRect; // 右上角控制点
QRect _leftBottomControlRect; // 左下角控制点
QRect _rightBottomControlRect; // 右下角控制点
QRect _midUpControlRect; // 中上控制点
QRect _midBelowControlRect; // 中下控制点
QRect _midLeftControlRect; // 左中控制点
QRect _midRightControlRect; // 中右控制点
};
#endif // PRINTSCREENDISCUSSIONWIDGET_H
ScreenshotToolWidget.cpp
#include "ScreenshotToolWidget.h"
#include <assert.h>
#include <QStylePainter>
#include <QStyleOption>
#include <QPainter>
#include <QImage>
#include <QPen>
#include <QMouseEvent>
#include <windows.h>
#include "SimpleFun.h"
#include "FuncRunDataManager.h"
#include "DesktopResize.h"
#include <QDir>
#include <Common.h>
ScreenshotToolWidget::ScreenshotToolWidget(QWidget *parent) :
QWidget(parent),
_mousePressed(false),
_isCut(false),
_pScreenshotToolOperatioWidget(NULL)
{
setWindowFlags(Qt::FramelessWindowHint | Qt::WindowStaysOnTopHint);
this->setAttribute(Qt::WA_TranslucentBackground);
setMouseTracking(true);// 鼠标追踪
// 创建一个QImage缓存
_pImageBuffer=new QImage();
assert(0 != _pImageBuffer);
initMenuWidget();
}
void ScreenshotToolWidget::paintEvent(QPaintEvent *event)
{
if(!_isCut)
{
if (!_pImageBuffer->isNull())
{
// 样式表用于Qwidget加载样式表
QStylePainter stylePainter(this);
QStyleOption opt;
opt.initFrom(this);
opt.rect = rect();
stylePainter.drawPrimitive(QStyle::PE_Widget, opt);
// 半透明背景,设置一个黑色蒙层
QPainter dousePainter(this);
//先绘图获取截图图片
dousePainter.drawImage(this->rect(), *_pImageBuffer);
dousePainter.setBrush(QBrush(QColor(0, 0, 0, 100)));
dousePainter.drawRect(this->rect());
// 绘制截图目标选定区域
QPainter magnifyingPainter(this);
magnifyingPainter.setRenderHint(QPainter::Antialiasing, true);
QPen magnifyingPen;
magnifyingPen.setStyle(Qt::SolidLine);
magnifyingPen.setWidth(4);
magnifyingPen.setBrush(QBrush(QColor(65, 149, 235, 255)));
magnifyingPen.setCapStyle(Qt::RoundCap);
magnifyingPen.setJoinStyle(Qt::RoundJoin);
magnifyingPainter.setPen(magnifyingPen);
magnifyingPainter.drawRect(_targetRect);
// 绘制目标区域图片
QPainter targetPainter(this);
targetPainter.setRenderHint(QPainter::Antialiasing, true);
// 返回图像的子区域作为新图像。返回的图像是从该图像中的位置(rectangle.x(),
// rectangle.y())复制的,并且始终具有给定矩形的大小。
QImage targetImage = _pImageBuffer->copy(_targetRect);
targetPainter.drawImage(_targetRect, targetImage);
// 绘制圆形控制点
QPainter ctrlPainter(this);
ctrlPainter.setRenderHint(QPainter::Antialiasing, true);
QPen ctrlPen;
ctrlPen.setStyle(Qt::SolidLine);
ctrlPen.setWidth(2);
ctrlPen.setBrush(QBrush(QColor(12, 157, 250, 255)));
ctrlPen.setCapStyle(Qt::RoundCap);
ctrlPen.setJoinStyle(Qt::RoundJoin);
ctrlPainter.setPen(ctrlPen);
ctrlPainter.setBrush(QBrush(QColor(12, 157, 250)));
ctrlPainter.drawRoundedRect(_leftTopControlRect, _leftTopControlRect.width() / 2, _leftTopControlRect.height() / 2);
ctrlPainter.drawRoundedRect(_leftBottomControlRect, _leftBottomControlRect.width() / 2, _leftBottomControlRect.height() / 2);
ctrlPainter.drawRoundedRect(_rightTopControlRect, _rightTopControlRect.width() / 2, _rightTopControlRect.height() / 2);
ctrlPainter.drawRoundedRect(_rightBottomControlRect, _rightBottomControlRect.width() / 2, _rightBottomControlRect.height() / 2);
ctrlPainter.drawRoundedRect(_midBelowControlRect, _midBelowControlRect.width() / 2, _midBelowControlRect.height() / 2);
ctrlPainter.drawRoundedRect(_midLeftControlRect, _midLeftControlRect.width() / 2, _midLeftControlRect.height() / 2);
ctrlPainter.drawRoundedRect(_midRightControlRect, _midRightControlRect.width() / 2, _midRightControlRect.height() / 2);
ctrlPainter.drawRoundedRect(_midUpControlRect, _midUpControlRect.width() / 2, _midUpControlRect.height() / 2);
}
}
QWidget::paintEvent(event);
}
void ScreenshotToolWidget::initMenuWidget()
{
_pScreenshotToolOperatioWidget = new ScreenshotToolOperatioWidget(this);
assert(0 != _pScreenshotToolOperatioWidget);
// 创建简单的截图操作按钮,此处为了方便管理将操作按钮和截图操作分开创建
_pScreenshotToolOperatioWidget->resize(342 * Common::instance()->getHorizontalRatio(),
90 * Common::instance()->getVerticalRatio());
_pScreenshotToolOperatioWidget->setVisible(false);
_isCut = false;
connect(_pScreenshotToolOperatioWidget, &ScreenshotToolOperatioWidget::signal_closeCutImage,
this, &ScreenshotToolWidget::slot_cancelScreenshotToolWidget);
connect(_pScreenshotToolOperatioWidget, &ScreenshotToolOperatioWidget::signal_sendCutImage,
this, &ScreenshotToolWidget::slot_sendCutImage);
}
void ScreenshotToolWidget::mouseMoveEvent(QMouseEvent *event)
{
if (event->buttons() & Qt::LeftButton && _mousePressed)
{
if (_scaleType == ScreenshotToolWidget::SCALE_TYPE_NOSCALE)
{
moveTargetArea(event->pos() - _lastMousePos);
_lastMousePos = event->pos();
}
else
{
scaleTargetArea(_scaleType, event->pos());
}
update();
event->accept();
}
// 设置鼠标形状
setCursor(Qt::ArrowCursor);
QPoint mousePos = event->pos();
if(_pScreenshotToolOperatioWidget->geometry().contains(mousePos))
{
setCursor(Qt::ArrowCursor);
}
else if (_targetRect.contains(mousePos))
{
setCursor(Qt::SizeAllCursor);
}
if (_leftTopControlRect.contains(mousePos))
{
setCursor(Qt::SizeFDiagCursor);
}
else if(_rightBottomControlRect.contains(mousePos))
{
setCursor(Qt::SizeFDiagCursor);
}
else if(_rightTopControlRect.contains(mousePos))
{
setCursor(Qt::SizeBDiagCursor);
}
else if(_leftBottomControlRect.contains(mousePos))
{
setCursor(Qt::SizeBDiagCursor);
}
else if(_midBelowControlRect.contains(mousePos))
{
setCursor(Qt::SizeVerCursor);
}
else if(_midLeftControlRect.contains(mousePos))
{
setCursor(Qt::SizeHorCursor);
}
else if(_midRightControlRect.contains(mousePos))
{
setCursor(Qt::SizeHorCursor);
}
else if(_midUpControlRect.contains(mousePos))
{
setCursor(Qt::SizeVerCursor);
}
}
void ScreenshotToolWidget::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
_lastMousePos = event->pos();
_mousePressed = true;
_scaleType = SCALE_TYPE_NOSCALE;
// 如果在控制点
QPoint pressedPos = event->pos();
if (_leftTopControlRect.contains(pressedPos))
{
setCursor(Qt::SizeFDiagCursor);
_scaleType = SCALE_TYPE_LEFTTOP;
}
else if(_rightBottomControlRect.contains(pressedPos))
{
_scaleType = SCALE_TYPE_RIGHTBOTTOM;
}
else if(_rightTopControlRect.contains(pressedPos))
{
_scaleType = SCALE_TYPE_RIGTHTOP;
}
else if(_leftBottomControlRect.contains(pressedPos))
{
_scaleType = SCALE_TYPE_LEFTBOTTOM;
}
else if(_midBelowControlRect.contains(pressedPos))
{
setCursor(Qt::SizeVerCursor);
_scaleType = SCALE_TYPE_MIDBELLOW;
}
else if(_midLeftControlRect.contains(pressedPos))
{
setCursor(Qt::SizeHorCursor);
_scaleType = SCALE_TYPE_MIDLEFT;
}
else if(_midRightControlRect.contains(pressedPos))
{
setCursor(Qt::SizeHorCursor);
_scaleType = SCALE_TYPE_MIDRIGHT;
}
else if(_midUpControlRect.contains(pressedPos))
{
setCursor(Qt::SizeVerCursor);
_scaleType = SCALE_TYPE_MIDUP;
}
event->accept();
}
}
void ScreenshotToolWidget::mouseReleaseEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton)
{
_mousePressed = false;
_targetImage = _pImageBuffer->copy(_targetRect);
event->accept();
}
}
void ScreenshotToolWidget::moveTargetArea(QPoint movePos)
{
QRect newRect = _targetRect;
newRect.moveTo(_targetRect.topLeft() + movePos);
if(newRect.topLeft() == QPoint(0,0)
|| newRect.topLeft().x() < 0
|| newRect.topLeft().y() < 0
|| newRect.bottomLeft().y() > Common::instance()->getScreenHeight()
|| newRect.topRight().x() > Common::instance()->getScreenWidth()
|| newRect.topRight().y()<0)
{
return;
}
_targetRect.moveTo(_targetRect.topLeft() + movePos);
setControlPoint();
changeMenuPos();
}
void ScreenshotToolWidget::setImage(QImage image)
{
if(_pImageBuffer != Q_NULLPTR)
{
delete _pImageBuffer;
_pImageBuffer = Q_NULLPTR;
}
_pImageBuffer = new QImage(image);
assert(0 != _pImageBuffer);
this->resize(image.size());
// 设置目标区域
_targetRect.setSize(QSize(650 * Common::instance()->getHorizontalRatio(), 450 * Common::instance()->getVerticalRatio()));
_targetRect.moveTo((this->width() - _targetRect.width()) / 2, (this->height() - _targetRect.height()) / 2);
_targetImage = _pImageBuffer->copy(_targetRect);
// 设置控制点
setControlPoint();
changeMenuPos();
_pScreenshotToolOperatioWidget->setVisible(true);
update();
}
void ScreenshotToolWidget::setIscut(bool isCut)
{
_isCut = isCut;
}
void ScreenshotToolWidget::slot_cancelScreenshotToolWidget()
{
emit signal_cancelScreenshotToolWidget(_pImageFullName);
_targetRect = QRect(this->rect());
_targetImage = _pImageBuffer->copy(_targetRect);
this->close();
}
void ScreenshotToolWidget::slot_sendCutImage()
{
if(this->isVisible())
{
_isCut = true;
this->repaint();
QString dirStr = QString("%1").arg(_pImagePath);
QDir dir(dirStr);
if (!dir.exists())
{
dir.mkpath(dirStr);
}
QString imagefilePath = QString("%1%2").arg(_pImagePath).arg(_pImageFullName);
QByteArray extension=_pImageExtension.toLatin1();
char * imgExtension = extension.data();
QPixmap::fromImage(_targetImage).save(imagefilePath, imgExtension, 100);
emit signal_sendPrintScreenWidget(_pImageFullName);
_targetRect = QRect(this->rect());
_targetImage = _pImageBuffer->copy(_targetRect);
this->close();
}
}
void ScreenshotToolWidget::scaleTargetArea(ScreenshotToolWidget::SCALE_TYPE type, QPoint point)
{
switch (type)
{
case ScreenshotToolWidget::SCALE_TYPE_LEFTTOP:
if(point.x() < _targetRect.right() - 50 * Common::instance()->getHorizontalRatio()
&& point.y() < _targetRect.bottom() - 50 * Common::instance()->getVerticalRatio())
{
_targetRect.setTopLeft(point);
}
break;
case ScreenshotToolWidget::SCALE_TYPE_LEFTBOTTOM:
if (point.x() < _targetRect.right() - 50 * Common::instance()->getHorizontalRatio()
&& point.y() > _targetRect.top() + 50 * Common::instance()->getVerticalRatio())
{
if(point.y() > Common::instance()->getScreenHeight())
{
_targetRect.setBottomLeft(QPoint(point.x(),
Common::instance()->getScreenHeight()));
}
else
{
_targetRect.setBottomLeft(point);
}
}
break;
case ScreenshotToolWidget::SCALE_TYPE_RIGTHTOP:
if (point.x() > _targetRect.left() + 50 * Common::instance()->getHorizontalRatio()
&& point.y() < _targetRect.bottom() - 50 * Common::instance()->getVerticalRatio())
{
_targetRect.setTopRight(point);
}
break;
case ScreenshotToolWidget::SCALE_TYPE_RIGHTBOTTOM:
if(_targetRect.width()>=50 * Common::instance()->getHorizontalRatio()
&& point.x() >=_targetRect.left()+50 * Common::instance()->getHorizontalRatio()
&& _targetRect.height()>200 * Common::instance()->getVerticalRatio()
&& point.y()>_targetRect.top()+50 * Common::instance()->getVerticalRatio())
{
if(point.y() > Common::instance()->getScreenHeight())
{
_targetRect.setBottomRight(QPoint(point.x(),
Common::instance()->getScreenHeight()));
}
else
{
_targetRect.setBottomRight(point);
}
}
break;
case ScreenshotToolWidget::SCALE_TYPE_MIDBELLOW:
if(_targetRect.height() > 50 * Common::instance()->getVerticalRatio()
&& point.y()>_targetRect.top() + 50 * Common::instance()->getVerticalRatio())
{
if(point.y() > Common::instance()->getScreenHeight())
{
_targetRect.setBottomLeft(QPoint(_targetRect.bottomLeft().x(),
Common::instance()->getScreenHeight()));
_targetRect.setBottomRight(QPoint(_targetRect.bottomRight().x(),
Common::instance()->getScreenHeight()));
}
else
{
_targetRect.setBottomLeft(QPoint(_targetRect.bottomLeft().x(),point.y()));
_targetRect.setBottomRight(QPoint(_targetRect.bottomRight().x(),point.y()));
}
}
break;
case ScreenshotToolWidget::SCALE_TYPE_MIDUP:
if(_targetRect.height() >= 50 * Common::instance()->getVerticalRatio()
&& point.y() <= _targetRect.bottom() - 50 * Common::instance()->getVerticalRatio())
{
_targetRect.setTopLeft(QPoint(_targetRect.topLeft().x(), point.y()));
_targetRect.setTopRight(QPoint(_targetRect.topRight().x(), point.y()));
}
break;
case ScreenshotToolWidget::SCALE_TYPE_MIDLEFT:
if(_targetRect.width() >= 50 * Common::instance()->getHorizontalRatio()
&& point.x()<=_targetRect.right() - 50 * Common::instance()->getHorizontalRatio())
{
_targetRect.setTopLeft(QPoint(point.x(),_targetRect.topLeft().y()));
if(point.y() > Common::instance()->getScreenHeight())
{
_targetRect.setBottomLeft(QPoint(point.x(),
Common::instance()->getScreenHeight()));
}
else
{
_targetRect.setBottomLeft(QPoint(point.x(),_targetRect.bottomLeft().y()));
}
}
break;
case ScreenshotToolWidget::SCALE_TYPE_MIDRIGHT:
if(_targetRect.width() >= 50 * Common::instance()->getHorizontalRatio()
&& point.x() >= _targetRect.left() + 50 * Common::instance()->getHorizontalRatio())
{
_targetRect.setTopRight(QPoint(point.x(),_targetRect.topRight().y()));
if(point.y() > Common::instance()->getScreenHeight())
{
_targetRect.setBottomRight(QPoint(point.x(),
Common::instance()->getScreenHeight()));
}
else
{
_targetRect.setBottomRight(QPoint(point.x(),_targetRect.bottomRight().y()));
}
}
break;
default:
break;
}
setControlPoint();
changeMenuPos();
}
void ScreenshotToolWidget::changeMenuPos()
{
int x;
int y;
if(_targetRect.height() >= _pScreenshotToolOperatioWidget->height() && _targetRect.width() >= _pScreenshotToolOperatioWidget->width())
{
if(_targetRect.bottom() < (Common::instance()->getScreenHeight() - _pScreenshotToolOperatioWidget->height()
- 20 * Common::instance()->getVerticalRatio()))
{
x = _targetRect.right() - _pScreenshotToolOperatioWidget->width();
y = _targetRect.bottom() + 20 * Common::instance()->getVerticalRatio();
}
else
{
x = _targetRect.right() - _pScreenshotToolOperatioWidget->width();
y = _targetRect.bottom() - _pScreenshotToolOperatioWidget->height();
}
}
else if(_targetRect.height() < _pScreenshotToolOperatioWidget->height()
&& _targetRect.width() < _pScreenshotToolOperatioWidget->width())
{
if(_targetRect.bottom() > (Common::instance()->getScreenHeight() - _pScreenshotToolOperatioWidget->height()
- 20 * Common::instance()->getVerticalRatio()))
{
x = _targetRect.right() + 20 * Common::instance()->getHorizontalRatio();
y = _targetRect.bottom() - _pScreenshotToolOperatioWidget->height();
if(_targetRect.right() > (Common::instance()->getScreenWidth() - _pScreenshotToolOperatioWidget->width()
- 20 * Common::instance()->getHorizontalRatio()))
{
x = _targetRect.left() - _pScreenshotToolOperatioWidget->width() - 20 * Common::instance()->getVerticalRatio();
}
}
else if( _targetRect.bottom() < (Common::instance()->getScreenHeight() - _pScreenshotToolOperatioWidget->height()
- 20 * Common::instance()->getVerticalRatio()))
{
x = _targetRect.right() - _pScreenshotToolOperatioWidget->width();
y = _targetRect.bottom() + 20 * Common::instance()->getVerticalRatio();
if(x < 0)
{
x = _targetRect.right() + 20 * Common::instance()->getVerticalRatio();
y = _targetRect.bottom() - _pScreenshotToolOperatioWidget->height();
}
}
}
else if(_targetRect.height() < _pScreenshotToolOperatioWidget->height()
&& _targetRect.width() > _pScreenshotToolOperatioWidget->width())
{
if(_targetRect.bottom() > (Common::instance()->getScreenHeight() - _pScreenshotToolOperatioWidget->height()
- 20 * Common::instance()->getVerticalRatio()))
{
x = _targetRect.right() - _pScreenshotToolOperatioWidget->width();
y = _targetRect.top() - _pScreenshotToolOperatioWidget->height();
}
else if( _targetRect.bottom() < (Common::instance()->getScreenHeight() - _pScreenshotToolOperatioWidget->height()
- 20 * Common::instance()->getVerticalRatio()))
{
x = _targetRect.right() - _pScreenshotToolOperatioWidget->width();
y = _targetRect.bottom() + 20 * Common::instance()->getVerticalRatio();
}
}
else if(_targetRect.height() >= _pScreenshotToolOperatioWidget->height()
&& _targetRect.width() < _pScreenshotToolOperatioWidget->width())
{
if(_targetRect.top() < _pScreenshotToolOperatioWidget->height() + 20 * Common::instance()->getVerticalRatio()
&& _targetRect.bottom() > (Common::instance()->getScreenHeight() - _pScreenshotToolOperatioWidget->height()
- 20 * Common::instance()->getVerticalRatio()))
{
x = _targetRect.right() + 20 * Common::instance()->getVerticalRatio();
y = _targetRect.bottom() - _pScreenshotToolOperatioWidget->height();
if(_targetRect.right() > (Common::instance()->getScreenWidth() - _pScreenshotToolOperatioWidget->width()
- 20 * Common::instance()->getHorizontalRatio()))
{
x = _targetRect.left() - _pScreenshotToolOperatioWidget->width() - 20 * Common::instance()->getVerticalRatio();
}
}
else if(_targetRect.top() > _pScreenshotToolOperatioWidget->height() + 20 * Common::instance()->getVerticalRatio()
&& _targetRect.bottom() > (Common::instance()->getScreenHeight() - _pScreenshotToolOperatioWidget->height()
- 20 * Common::instance()->getVerticalRatio()))
{
x = _targetRect.right() + 20 * Common::instance()->getHorizontalRatio();
y = _targetRect.bottom() - _pScreenshotToolOperatioWidget->height();
if(_targetRect.right() > (Common::instance()->getScreenWidth() - _pScreenshotToolOperatioWidget->width()
- 20 * Common::instance()->getHorizontalRatio()))
{
x = _targetRect.left() - _pScreenshotToolOperatioWidget->width() - 20 * Common::instance()->getHorizontalRatio();
y = _targetRect.bottom() - _pScreenshotToolOperatioWidget->height();
}
}
else if(_targetRect.top() > _pScreenshotToolOperatioWidget->height() + 20 * Common::instance()->getVerticalRatio()
&& _targetRect.bottom() < (Common::instance()->getScreenHeight() - _pScreenshotToolOperatioWidget->height()
- 20 * Common::instance()->getVerticalRatio()))
{
x = _targetRect.right() - _pScreenshotToolOperatioWidget->width();
y = _targetRect.bottom() + 20 * Common::instance()->getVerticalRatio();
if(x < 0)
{
x = _targetRect.right() + 20 * Common::instance()->getVerticalRatio();
y = _targetRect.bottom() - _pScreenshotToolOperatioWidget->height();
}
}
else if(_targetRect.top() < _pScreenshotToolOperatioWidget->height() + 20 * Common::instance()->getVerticalRatio()
&& _targetRect.bottom() < (Common::instance()->getScreenHeight() - _pScreenshotToolOperatioWidget->height()
- 20 * Common::instance()->getVerticalRatio()))
{
x = _targetRect.right() - _pScreenshotToolOperatioWidget->width();
y = _targetRect.bottom() + 20 * Common::instance()->getVerticalRatio();
if(x < 0)
{
x = _targetRect.right() + 20 * Common::instance()->getHorizontalRatio();
}
}
}
_pScreenshotToolOperatioWidget->move(x, y);
}
void ScreenshotToolWidget::setControlPoint()
{
_leftTopControlRect = QRect(_targetRect.topLeft() - QPoint(11 * Common::instance()->getHorizontalRatio(),
11 * Common::instance()->getVerticalRatio()),
QSize(20 * Common::instance()->getHorizontalRatio(),
20 * Common::instance()->getVerticalRatio()));
_leftBottomControlRect = QRect(_targetRect.bottomLeft() - QPoint(11 * Common::instance()->getHorizontalRatio(),
11 * Common::instance()->getVerticalRatio()),
QSize(20 * Common::instance()->getHorizontalRatio(),
20 * Common::instance()->getVerticalRatio()));
_rightBottomControlRect= QRect(_targetRect.bottomRight() - QPoint(9 * Common::instance()->getHorizontalRatio(),
11 * Common::instance()->getVerticalRatio()),
QSize(20 * Common::instance()->getHorizontalRatio(),
20 * Common::instance()->getVerticalRatio()));
_rightTopControlRect = QRect(_targetRect.topRight() - QPoint(9 * Common::instance()->getHorizontalRatio(),
11 * Common::instance()->getVerticalRatio()),
QSize(20 * Common::instance()->getHorizontalRatio(),
20 * Common::instance()->getVerticalRatio()));
_midLeftControlRect = QRect(_targetRect.topLeft() + QPoint(-11 * Common::instance()->getHorizontalRatio(),
(_targetRect.height() / 2) - 5 * Common::instance()->getVerticalRatio()),
QSize(20 * Common::instance()->getHorizontalRatio(),
20 * Common::instance()->getVerticalRatio()));
_midBelowControlRect = QRect(_targetRect.topLeft() + QPoint((_targetRect.width() / 2 - 10 * Common::instance()->getVerticalRatio()),
(_targetRect.height() - 11 * Common::instance()->getVerticalRatio())),
QSize(20 * Common::instance()->getHorizontalRatio(),
20 * Common::instance()->getVerticalRatio()));
_midRightControlRect = QRect(_targetRect.bottomRight() + QPoint(-8 * Common::instance()->getHorizontalRatio(),
(-_targetRect.height() / 2 - 5 * Common::instance()->getVerticalRatio())),
QSize(20 * Common::instance()->getHorizontalRatio(),
20 * Common::instance()->getVerticalRatio()));
_midUpControlRect = QRect(_targetRect.topLeft() + QPoint((_targetRect.width()/2) - 10 * Common::instance()->getVerticalRatio(),
- 11 * Common::instance()->getVerticalRatio()),
QSize(20 * Common::instance()->getHorizontalRatio(),
20 * Common::instance()->getVerticalRatio()));
}
void ScreenshotToolWidget::resizeEvent(QResizeEvent * event)
{
if (!_pImageBuffer->isNull())
{
*_pImageBuffer = _pImageBuffer->scaled(this->size());
}
QWidget::resizeEvent(event);
}
ScreenshotToolWidget::~ScreenshotToolWidget()
{
if(!_pImageBuffer->isNull())
{
delete _pImageBuffer;
}
}
截取的操作按钮较为简单就不多做描述了,至此就简单的截图工具就完成了