彻底改版2.0
This commit is contained in:
18
widget/screenwidget/frmscreenwidget.cpp
Normal file
18
widget/screenwidget/frmscreenwidget.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "frmscreenwidget.h"
|
||||
#include "ui_frmscreenwidget.h"
|
||||
#include "screenwidget.h"
|
||||
|
||||
frmScreenWidget::frmScreenWidget(QWidget *parent) : QWidget(parent), ui(new Ui::frmScreenWidget)
|
||||
{
|
||||
ui->setupUi(this);
|
||||
}
|
||||
|
||||
frmScreenWidget::~frmScreenWidget()
|
||||
{
|
||||
delete ui;
|
||||
}
|
||||
|
||||
void frmScreenWidget::on_pushButton_clicked()
|
||||
{
|
||||
ScreenWidget::Instance()->showFullScreen();
|
||||
}
|
||||
25
widget/screenwidget/frmscreenwidget.h
Normal file
25
widget/screenwidget/frmscreenwidget.h
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef FRMSCREENWIDGET_H
|
||||
#define FRMSCREENWIDGET_H
|
||||
|
||||
#include <QWidget>
|
||||
|
||||
namespace Ui {
|
||||
class frmScreenWidget;
|
||||
}
|
||||
|
||||
class frmScreenWidget : public QWidget
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
explicit frmScreenWidget(QWidget *parent = 0);
|
||||
~frmScreenWidget();
|
||||
|
||||
private slots:
|
||||
void on_pushButton_clicked();
|
||||
|
||||
private:
|
||||
Ui::frmScreenWidget *ui;
|
||||
};
|
||||
|
||||
#endif // FRMSCREENWIDGET_H
|
||||
32
widget/screenwidget/frmscreenwidget.ui
Normal file
32
widget/screenwidget/frmscreenwidget.ui
Normal file
@@ -0,0 +1,32 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<ui version="4.0">
|
||||
<class>frmScreenWidget</class>
|
||||
<widget class="QWidget" name="frmScreenWidget">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>0</x>
|
||||
<y>0</y>
|
||||
<width>800</width>
|
||||
<height>600</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="windowTitle">
|
||||
<string>Form</string>
|
||||
</property>
|
||||
<widget class="QPushButton" name="pushButton">
|
||||
<property name="geometry">
|
||||
<rect>
|
||||
<x>10</x>
|
||||
<y>10</y>
|
||||
<width>92</width>
|
||||
<height>28</height>
|
||||
</rect>
|
||||
</property>
|
||||
<property name="text">
|
||||
<string>弹出</string>
|
||||
</property>
|
||||
</widget>
|
||||
</widget>
|
||||
<resources/>
|
||||
<connections/>
|
||||
</ui>
|
||||
31
widget/screenwidget/main.cpp
Normal file
31
widget/screenwidget/main.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "frmscreenwidget.h"
|
||||
#include <QApplication>
|
||||
#include <QTextCodec>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QApplication a(argc, argv);
|
||||
a.setFont(QFont("Microsoft Yahei", 9));
|
||||
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
|
||||
#if _MSC_VER
|
||||
QTextCodec *codec = QTextCodec::codecForName("gbk");
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
#endif
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
QTextCodec::setCodecForCStrings(codec);
|
||||
QTextCodec::setCodecForTr(codec);
|
||||
#else
|
||||
QTextCodec *codec = QTextCodec::codecForName("utf-8");
|
||||
QTextCodec::setCodecForLocale(codec);
|
||||
#endif
|
||||
|
||||
frmScreenWidget w;
|
||||
w.setWindowTitle("屏幕截图");
|
||||
w.show();
|
||||
|
||||
return a.exec();
|
||||
}
|
||||
332
widget/screenwidget/screenwidget.cpp
Normal file
332
widget/screenwidget/screenwidget.cpp
Normal file
@@ -0,0 +1,332 @@
|
||||
#pragma execution_character_set("utf-8")
|
||||
|
||||
#include "screenwidget.h"
|
||||
#include "qmutex.h"
|
||||
#include "qapplication.h"
|
||||
#include "qpainter.h"
|
||||
#include "qfiledialog.h"
|
||||
#include "qevent.h"
|
||||
#include "qdatetime.h"
|
||||
#include "qstringlist.h"
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
#include "qscreen.h"
|
||||
#define deskGeometry qApp->primaryScreen()->geometry()
|
||||
#define deskGeometry2 qApp->primaryScreen()->availableGeometry()
|
||||
#else
|
||||
#include "qdesktopwidget.h"
|
||||
#define deskGeometry qApp->desktop()->geometry()
|
||||
#define deskGeometry2 qApp->desktop()->availableGeometry()
|
||||
#endif
|
||||
|
||||
#define STRDATETIME qPrintable (QDateTime::currentDateTime().toString("yyyy-MM-dd-HH-mm-ss"))
|
||||
|
||||
Screen::Screen(QSize size)
|
||||
{
|
||||
maxWidth = size.width();
|
||||
maxHeight = size.height();
|
||||
|
||||
startPos = QPoint(-1, -1);
|
||||
endPos = startPos;
|
||||
leftUpPos = startPos;
|
||||
rightDownPos = startPos;
|
||||
status = SELECT;
|
||||
}
|
||||
|
||||
int Screen::width()
|
||||
{
|
||||
return maxWidth;
|
||||
}
|
||||
|
||||
int Screen::height()
|
||||
{
|
||||
return maxHeight;
|
||||
}
|
||||
|
||||
Screen::STATUS Screen::getStatus()
|
||||
{
|
||||
return status;
|
||||
}
|
||||
|
||||
void Screen::setStatus(STATUS status)
|
||||
{
|
||||
this->status = status;
|
||||
}
|
||||
|
||||
void Screen::setEnd(QPoint pos)
|
||||
{
|
||||
endPos = pos;
|
||||
leftUpPos = startPos;
|
||||
rightDownPos = endPos;
|
||||
cmpPoint(leftUpPos, rightDownPos);
|
||||
}
|
||||
|
||||
void Screen::setStart(QPoint pos)
|
||||
{
|
||||
startPos = pos;
|
||||
}
|
||||
|
||||
QPoint Screen::getEnd()
|
||||
{
|
||||
return endPos;
|
||||
}
|
||||
|
||||
QPoint Screen::getStart()
|
||||
{
|
||||
return startPos;
|
||||
}
|
||||
|
||||
QPoint Screen::getLeftUp()
|
||||
{
|
||||
return leftUpPos;
|
||||
}
|
||||
|
||||
QPoint Screen::getRightDown()
|
||||
{
|
||||
return rightDownPos;
|
||||
}
|
||||
|
||||
bool Screen::isInArea(QPoint pos)
|
||||
{
|
||||
if (pos.x() > leftUpPos.x() && pos.x() < rightDownPos.x() && pos.y() > leftUpPos.y() && pos.y() < rightDownPos.y()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void Screen::move(QPoint 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;
|
||||
}
|
||||
|
||||
void Screen::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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
QScopedPointer<ScreenWidget> ScreenWidget::self;
|
||||
ScreenWidget *ScreenWidget::Instance()
|
||||
{
|
||||
if (self.isNull()) {
|
||||
static QMutex mutex;
|
||||
QMutexLocker locker(&mutex);
|
||||
if (self.isNull()) {
|
||||
self.reset(new ScreenWidget);
|
||||
}
|
||||
}
|
||||
|
||||
return self.data();
|
||||
}
|
||||
|
||||
ScreenWidget::ScreenWidget(QWidget *parent) : QWidget(parent)
|
||||
{
|
||||
//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(deskGeometry.size());
|
||||
//保存全屏图像
|
||||
fullScreen = new QPixmap();
|
||||
}
|
||||
|
||||
void ScreenWidget::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 ScreenWidget::showEvent(QShowEvent *)
|
||||
{
|
||||
QPoint point(-1, -1);
|
||||
screen->setStart(point);
|
||||
screen->setEnd(point);
|
||||
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
QScreen *pscreen = QApplication::primaryScreen();
|
||||
*fullScreen = pscreen->grabWindow(0, 0, 0, screen->width(), screen->height());
|
||||
#else
|
||||
*fullScreen = fullScreen->grabWindow(0, 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);
|
||||
}
|
||||
|
||||
void ScreenWidget::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 ScreenWidget::saveFullScreen()
|
||||
{
|
||||
QString fileName = QString("%1/full_%2.png").arg(qApp->applicationDirPath()).arg(STRDATETIME);
|
||||
fullScreen->save(fileName, "png");
|
||||
close();
|
||||
}
|
||||
|
||||
void ScreenWidget::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 ScreenWidget::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();
|
||||
}
|
||||
}
|
||||
|
||||
void ScreenWidget::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 ScreenWidget::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 ScreenWidget::mouseReleaseEvent(QMouseEvent *)
|
||||
{
|
||||
if (screen->getStatus() == Screen::SELECT) {
|
||||
screen->setStatus(Screen::MOV);
|
||||
} else if (screen->getStatus() == Screen::MOV) {
|
||||
this->setCursor(Qt::ArrowCursor);
|
||||
}
|
||||
}
|
||||
|
||||
void ScreenWidget::contextMenuEvent(QContextMenuEvent *)
|
||||
{
|
||||
this->setCursor(Qt::ArrowCursor);
|
||||
menu->exec(cursor().pos());
|
||||
}
|
||||
94
widget/screenwidget/screenwidget.h
Normal file
94
widget/screenwidget/screenwidget.h
Normal file
@@ -0,0 +1,94 @@
|
||||
#ifndef SCREENWIDGET_H
|
||||
#define SCREENWIDGET_H
|
||||
|
||||
/**
|
||||
* 全局截屏控件 作者:feiyangqingyun(QQ:517216493) 2016-11-11
|
||||
* 1. 鼠标右键弹出菜单。
|
||||
* 2. 支持全局截屏。
|
||||
* 3. 支持局部截屏。
|
||||
* 4. 支持截图区域拖动。
|
||||
* 5. 支持图片另存为。
|
||||
*/
|
||||
|
||||
#include <QWidget>
|
||||
#include <QMenu>
|
||||
#include <QPoint>
|
||||
#include <QSize>
|
||||
|
||||
class Screen
|
||||
{
|
||||
public:
|
||||
enum STATUS {SELECT, MOV, SET_W_H};
|
||||
Screen() {}
|
||||
Screen(QSize size);
|
||||
|
||||
void setStart(QPoint pos);
|
||||
void setEnd(QPoint pos);
|
||||
QPoint getStart();
|
||||
QPoint getEnd();
|
||||
|
||||
QPoint getLeftUp();
|
||||
QPoint getRightDown();
|
||||
|
||||
STATUS getStatus();
|
||||
void setStatus(STATUS status);
|
||||
|
||||
int width();
|
||||
int height();
|
||||
|
||||
//检测坐标点是否在截图区域内
|
||||
bool isInArea(QPoint pos);
|
||||
//按坐标移动截图区域
|
||||
void move(QPoint p);
|
||||
|
||||
private:
|
||||
//记录 截图区域 左上角、右下角
|
||||
QPoint leftUpPos, rightDownPos;
|
||||
//记录 鼠标开始位置、结束位置
|
||||
QPoint startPos, endPos;
|
||||
//记录屏幕大小
|
||||
int maxWidth, maxHeight;
|
||||
//三个状态: 选择区域、移动区域、设置width height
|
||||
STATUS status;
|
||||
|
||||
//比较两位置,判断左上角、右下角
|
||||
void cmpPoint(QPoint &s, QPoint &e);
|
||||
};
|
||||
|
||||
#ifdef quc
|
||||
class Q_DECL_EXPORT ScreenWidget : public QWidget
|
||||
#else
|
||||
class ScreenWidget : public QWidget
|
||||
#endif
|
||||
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
static ScreenWidget *Instance();
|
||||
explicit ScreenWidget(QWidget *parent = 0);
|
||||
|
||||
private:
|
||||
static QScopedPointer<ScreenWidget> self;
|
||||
QMenu *menu; //右键菜单对象
|
||||
Screen *screen; //截屏对象
|
||||
QPixmap *fullScreen; //保存全屏图像
|
||||
QPixmap *bgScreen; //模糊背景图
|
||||
QPoint movPos; //坐标
|
||||
|
||||
protected:
|
||||
void contextMenuEvent(QContextMenuEvent *);
|
||||
void mousePressEvent(QMouseEvent *);
|
||||
void mouseMoveEvent(QMouseEvent *);
|
||||
void mouseReleaseEvent(QMouseEvent *);
|
||||
void paintEvent(QPaintEvent *);
|
||||
void showEvent(QShowEvent *);
|
||||
|
||||
private slots:
|
||||
void saveScreen();
|
||||
void saveFullScreen();
|
||||
void saveScreenOther();
|
||||
void saveFullOther();
|
||||
};
|
||||
|
||||
#endif // SCREENWIDGET_H
|
||||
|
||||
17
widget/screenwidget/screenwidget.pro
Normal file
17
widget/screenwidget/screenwidget.pro
Normal file
@@ -0,0 +1,17 @@
|
||||
QT += core gui
|
||||
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
|
||||
greaterThan(QT_MAJOR_VERSION, 5): QT += core5compat
|
||||
|
||||
TARGET = screenwidget
|
||||
TEMPLATE = app
|
||||
DESTDIR = $$PWD/../bin
|
||||
CONFIG += warn_off
|
||||
|
||||
SOURCES += main.cpp
|
||||
SOURCES += frmscreenwidget.cpp
|
||||
SOURCES += screenwidget.cpp
|
||||
|
||||
HEADERS += screenwidget.h
|
||||
HEADERS += frmscreenwidget.h
|
||||
|
||||
FORMS += frmscreenwidget.ui
|
||||
Reference in New Issue
Block a user