#ifndef WANGCHUAN_H
#define WANGCHUAN_H
#include <QGuiApplication>
#include <QPixmap>
#include <QWidget>
#include <QMenu>
#include <QPoint>
#include <QSize>
#include <QMouseEvent>
#include <QPushButton>
#include <QtDebug>
#include <QDesktopWidget>
#include <QPixmap>
#include <QMutex>
#include <QApplication>
#include <QPainter>
#include <QDesktopWidget>
#include <QFileDialog>
#include <QEvent>
#include <QDateTime>
#include <QStringList>
#include <QScreen>
namespace wangchuan{
namespace Screenshot
{
// 截取屏幕工具
// 使用 : ScreenWidget::Instance()->showFullScreen(); //直接调用实例
// 可以在程序重写一个键盘事件,通过快捷键来调用 该实例
// eg:
// virtual void keyPressEvent(QKeyEvent * event) override
// {
// switch (event->key())
// {
// // F1键
// case Qt::Key_F1:
// qDebug() <<"F1";
// wangchuan::Screenshot::ScreenWidget::Instance()->showFullScreen(); //直接调用实例
// break;
// }
// }
#define STRDATETIME qPrintable (QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss"))
//截屏对象类
class Screen
{
public:
enum STATUS {SELECT, MOV, SET_W_H};
Screen() {}
Screen(QSize size)
{
maxWidth = size.width();
maxHeight = size.height();
startPos = QPoint(-1, -1);
endPos = startPos;
leftUpPos = startPos;
rightDownPos = startPos;
status = SELECT;
}
void setStart(QPoint pos)
{
startPos = pos;
}
void setEnd(QPoint pos)
{
endPos = pos;
leftUpPos = startPos;
rightDownPos = endPos;
cmpPoint(leftUpPos, rightDownPos);
}
QPoint getStart()
{
return startPos;
}
QPoint getEnd()
{
return endPos;
}
QPoint getLeftUp()
{
return leftUpPos;
}
QPoint getRightDown()
{
return rightDownPos;
}
STATUS getStatus()
{
return status;
}
void setStatus(STATUS status)
{
this->status = status;
}
int width()
{
return maxWidth;
}
int height()
{
return maxHeight;
}
bool isInArea(QPoint pos) // 检测pos是否在截图区域内
{
if (pos.x() > leftUpPos.x() && pos.x() < rightDownPos.x() && pos.y() > leftUpPos.y() && pos.y() < rightDownPos.y()) {
return true;
}
return false;
}
void move(QPoint p) // 按 p 移动截图区域
{
int lx = leftUpPos.x() + p.x();
int ly = leftUpPos.y() + p.y();
int rx = rightDownPos.x() + p.x();
int ry = rightDownPos.y() + p.y();
if (lx < 0) {
lx = 0;
rx -= p.x();
}
if (ly < 0) {
ly = 0;
ry -= p.y();
}
if (rx > maxWidth) {
rx = maxWidth;
lx -= p.x();
}
if (ry > maxHeight) {
ry = maxHeight;
ly -= p.y();
}
leftUpPos = QPoint(lx, ly);
rightDownPos = QPoint(rx, ry);
startPos = leftUpPos;
endPos = rightDownPos;
}
private:
QPoint leftUpPos, rightDownPos; //记录 截图区域 左上角、右下角
QPoint startPos, endPos; //记录 鼠标开始位置、结束位置
int maxWidth, maxHeight; //记录屏幕大小
STATUS status; //三个状态: 选择区域、移动区域、设置width height
void cmpPoint(QPoint &leftTop, QPoint &rightDown)//比较两位置,判断左上角、右下角
{
QPoint l = leftTop;
QPoint r = rightDown;
if (l.x() <= r.x()) {
if (l.y() <= r.y()) {
;
} else {
leftTop.setY(r.y());
rightDown.setY(l.y());
}
} else {
if (l.y() < r.y()) {
leftTop.setX(r.x());
rightDown.setX(l.x());
} else {
QPoint tmp;
tmp = leftTop;
leftTop = rightDown;
rightDown = tmp;
}
}
}
};
//截屏窗口类
class ScreenWidget : public QWidget
{
Q_OBJECT
public:
static ScreenWidget *Instance()
{
// if (self.isNull()) {
// static QMutex mutex;
// QMutexLocker locker(&mutex);
// if (self.isNull()) {
// self.reset(new ScreenWidget);
// }
// }
// return self.data();
static auto screenw = new ScreenWidget;
return screenw;
}
explicit ScreenWidget(QWidget *parent = 0)
{
//this->setWindowFlags(Qt::Tool | Qt::WindowStaysOnTopHint | Qt::FramelessWindowHint | Qt::X11BypassWindowManagerHint);
menu = new QMenu(this);
menu->addAction("保存当前截图", this, SLOT(saveScreen()));
menu->addAction("保存全屏截图", this, SLOT(saveFullScreen()));
menu->addAction("截图另存为", this, SLOT(saveScreenOther()));
menu->addAction("全屏另存为", this, SLOT(saveFullOther()));
menu->addAction("退出截图", this, SLOT(hide()));
//取得屏幕大小
screen = new Screen(QApplication::desktop()->size());
//保存全屏图像
fullScreen = new QPixmap();
}
private:
// static QScopedPointer<ScreenWidget> self;
QMenu *menu; //右键菜单对象
Screen *screen; //截屏对象
QPixmap *fullScreen; //保存全屏图像
QPixmap *bgScreen; //模糊背景图
QPoint movPos; //坐标
protected:
void contextMenuEvent(QContextMenuEvent *e)
{
this->setCursor(Qt::ArrowCursor);
menu->exec(cursor().pos());
}
void mousePressEvent(QMouseEvent *e)
{
int status = screen->getStatus();
if (status == Screen::SELECT) {
screen->setStart(e->pos());
} else if (status == Screen::MOV) {
if (screen->isInArea(e->pos()) == false) {
screen->setStart(e->pos());
screen->setStatus(Screen::SELECT);
} else {
movPos = e->pos();
this->setCursor(Qt::SizeAllCursor);
}
}
this->update();
}
void mouseMoveEvent(QMouseEvent *e)
{
if (screen->getStatus() == Screen::SELECT) {
screen->setEnd(e->pos());
} else if (screen->getStatus() == Screen::MOV) {
QPoint p(e->x() - movPos.x(), e->y() - movPos.y());
screen->move(p);
movPos = e->pos();
}
this->update();
}
void mouseReleaseEvent(QMouseEvent *e)
{
if (screen->getStatus() == Screen::SELECT) {
screen->setStatus(Screen::MOV);
} else if (screen->getStatus() == Screen::MOV) {
this->setCursor(Qt::ArrowCursor);
}
}
void paintEvent(QPaintEvent *)
{
int x = screen->getLeftUp().x();
int y = screen->getLeftUp().y();
int w = screen->getRightDown().x() - x;
int h = screen->getRightDown().y() - y;
QPainter painter(this);
QPen pen;
pen.setColor(Qt::green);
pen.setWidth(2);
pen.setStyle(Qt::DotLine);
painter.setPen(pen);
painter.drawPixmap(0, 0, *bgScreen);
if (w != 0 && h != 0) {
painter.drawPixmap(x, y, fullScreen->copy(x, y, w, h));
}
painter.drawRect(x, y, w, h);
pen.setColor(Qt::yellow);
painter.setPen(pen);
painter.drawText(x + 2, y - 8, tr("截图范围:( %1 x %2 ) - ( %3 x %4 ) 图片大小:( %5 x %6 )")
.arg(x).arg(y).arg(x + w).arg(y + h).arg(w).arg(h));
}
void showEvent(QShowEvent *)
{
QPoint point(-1, -1);
screen->setStart(point);
screen->setEnd(point);
#if (QT_VERSION <= QT_VERSION_CHECK(5,0,0))
*fullScreen = fullScreen->grabWindow(QApplication::desktop()->winId(), 0, 0, screen->width(), screen->height());
#else
QScreen *pscreen = QApplication::primaryScreen();
*fullScreen = pscreen->grabWindow(QApplication::desktop()->winId(), 0, 0, screen->width(), screen->height());
#endif
//设置透明度实现模糊背景
QPixmap pix(screen->width(), screen->height());
pix.fill((QColor(160, 160, 160, 200)));
bgScreen = new QPixmap(*fullScreen);
QPainter p(bgScreen);
p.drawPixmap(0, 0, pix);
}
private slots:
void saveScreen()
{
int x = screen->getLeftUp().x();
int y = screen->getLeftUp().y();
int w = screen->getRightDown().x() - x;
int h = screen->getRightDown().y() - y;
QString fileName = QString("%1/screen_%2.png").arg(qApp->applicationDirPath()).arg(STRDATETIME);
fullScreen->copy(x, y, w, h).save(fileName, "png");
close();
}
void saveFullScreen()
{
QString fileName = QString("%1/full_%2.png").arg(qApp->applicationDirPath()).arg(STRDATETIME);
fullScreen->save(fileName, "png");
close();
}
void saveScreenOther()
{
QString name = QString("%1.png").arg(STRDATETIME);
QString fileName = QFileDialog::getSaveFileName(this, "保存图片", name, "png Files (*.png)");
if (!fileName.endsWith(".png")) {
fileName += ".png";
}
if (fileName.length() > 0) {
int x = screen->getLeftUp().x();
int y = screen->getLeftUp().y();
int w = screen->getRightDown().x() - x;
int h = screen->getRightDown().y() - y;
fullScreen->copy(x, y, w, h).save(fileName, "png");
close();
}
}
void saveFullOther()
{
QString name = QString("%1.png").arg(STRDATETIME);
QString fileName = QFileDialog::getSaveFileName(this, "保存图片", name, "png Files (*.png)");
if (!fileName.endsWith(".png")) {
fileName += ".png";
}
if (fileName.length() > 0) {
fullScreen->save(fileName, "png");
close();
}
}
};
//QScopedPointer<ScreenWidget> ScreenWidget::self;
}
}
#endif // WANGCHUAN_H