Skip to content

Commit e8e61f6

Browse files
committed
[optimize|chore]截取光标时背景色为透明;重构截图代码
1 parent 643a0e9 commit e8e61f6

18 files changed

+414
-286
lines changed

SmartScreenSnapper/SmartScreenSnapper.pro.user

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<!DOCTYPE QtCreatorProject>
3-
<!-- Written by QtCreator 4.11.1, 2022-10-03T20:44:18. -->
3+
<!-- Written by QtCreator 4.11.1, 2022-10-06T12:42:40. -->
44
<qtcreator>
55
<data>
66
<variable>EnvironmentId</variable>
@@ -310,7 +310,7 @@
310310
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DefaultDisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
311311
<value type="QString" key="ProjectExplorer.ProjectConfiguration.DisplayName">Desktop Qt 5.14.2 MinGW 64-bit</value>
312312
<value type="QString" key="ProjectExplorer.ProjectConfiguration.Id">qt.qt5.5142.win64_mingw73_kit</value>
313-
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">0</value>
313+
<value type="int" key="ProjectExplorer.Target.ActiveBuildConfiguration">1</value>
314314
<value type="int" key="ProjectExplorer.Target.ActiveDeployConfiguration">0</value>
315315
<value type="int" key="ProjectExplorer.Target.ActiveRunConfiguration">0</value>
316316
<valuemap type="QVariantMap" key="ProjectExplorer.Target.BuildConfiguration.0">

SmartScreenSnapper/res/RCData.rc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ DAZIJI SOUND "sound/DAZIJI.wav"
1111
#endif
1212

1313
VS_VERSION_INFO VERSIONINFO
14-
FILEVERSION 1,1,1,0
15-
PRODUCTVERSION 1,1,1,0
14+
FILEVERSION 1,2,0,0
15+
PRODUCTVERSION 1,2,0,0
1616
FILEFLAGSMASK 0x3fL
1717
#ifdef _DEBUG
1818
FILEFLAGS VS_FF_DEBUG
@@ -29,8 +29,8 @@ VS_VERSION_INFO VERSIONINFO
2929
BEGIN
3030
VALUE "CompanyName", "Sky_D\0"
3131
VALUE "FileDescription", "SmartScreenSnapper\0"
32-
VALUE "FileVersion", "1.1.1.0\0"
33-
VALUE "ProductVersion", "1.1.1.0\0"
32+
VALUE "FileVersion", "1.2.0.0\0"
33+
VALUE "ProductVersion", "1.2.0.0\0"
3434
VALUE "LegalCopyright", "\0"
3535
VALUE "LegalTrademarks", "\0"
3636
VALUE "OriginalFilename", "SmartScreenSnapper.exe\0"

SmartScreenSnapper/src/MyGlobalShortcut/MyGlobalShortCut.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,11 @@ MyGlobalShortCut::MyGlobalShortCut(QString key, QWidget *parent, bool reportErro
2222
tr("失败,请检查该热键是否被占用!"));
2323
}
2424

25-
void MyGlobalShortCut::activateShortcut(int i)
25+
void MyGlobalShortCut::activateShortcut(ScreenShotHelper::ShotType shotType)
2626
{
27-
emit activatedHotKey(i);
27+
emit activatedHotKey(shotType);
2828
}
29+
2930
bool MyGlobalShortCut::registerHotKey()
3031
{
3132
Qt::KeyboardModifiers allMods = Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier;

SmartScreenSnapper/src/MyGlobalShortcut/MyGlobalShortCut.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
#include "MyWinEventFilter.h"
77
#include <QKeySequence>
88
#include <QHash>
9+
#include "src/screenshothelper.h"
10+
911
class MyGlobalShortCut;
1012

1113

1214
class MyGlobalShortCut : public QObject
1315
{
1416
Q_OBJECT
1517
signals:
16-
void activatedHotKey(int);
18+
void activatedHotKey(ScreenShotHelper::ShotType shotType);
1719
public:
1820
MyGlobalShortCut();
1921
MyGlobalShortCut(QWidget* parent);
2022
MyGlobalShortCut(QString key,QWidget* parent, bool reportError);
2123
~MyGlobalShortCut();
22-
void activateShortcut(int i);
24+
void activateShortcut(ScreenShotHelper::ShotType shotType);
2325
bool registerHotKey();
2426
bool unregisterHotKey();
2527
QHash<QPair<quint32, quint32>, MyGlobalShortCut*> shortcuts;

SmartScreenSnapper/src/MyGlobalShortcut/MyWinEventFilter.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ bool MyWinEventFilter::nativeEventFilter(const QByteArray &eventType, void *mess
2828
bool res = m_shortcut->shortcuts.value(qMakePair(keycode, modifiers));
2929
if (res) {
3030
for (int i = 0; i < PublicData::hotKey.size(); i++) {
31-
for (int j = 0; j < PublicData::hotKey.at(i).size(); j++) {
32-
if (MyGlobalShortCut::nativeKeycode(PublicData::hotKey.at(i).at(j)->key) == HIWORD(msg->lParam) &&
33-
MyGlobalShortCut::nativeModifiers(PublicData::hotKey.at(i).at(j)->mods) == LOWORD(msg->lParam)) {
34-
m_shortcut ->activateShortcut(i);
35-
//break;
31+
ScreenShotHelper::ShotType shotType = PublicData::hotKey.keys().at(i);
32+
QList<MyGlobalShortCut*> hotKey = PublicData::hotKey.value(shotType);
33+
for (int j = 0; j < hotKey.size(); j++){
34+
if (MyGlobalShortCut::nativeKeycode(hotKey.at(j)->key) == HIWORD(msg->lParam) &&
35+
MyGlobalShortCut::nativeModifiers(hotKey.at(j)->mods) == LOWORD(msg->lParam)) {
36+
m_shortcut->activateShortcut(shotType);
3637
}
3738
}
3839
}

SmartScreenSnapper/src/freesnapdialog.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
#include <QClipboard>
1313
#include <QString>
1414

15-
FreeSnapDialog::FreeSnapDialog(QPixmap picture, QPixmap*& result, QWidget *parent) :
15+
FreeSnapDialog::FreeSnapDialog(QPixmap picture, QPixmap* result, bool &captured, QWidget *parent) :
1616
QDialog(parent),
1717
ui(new Ui::FreeSnapDialog),
1818
grayColor(0, 0, 0, 180),
1919
grayItem(nullptr),
2020
previewZoomRate(3.2f),
2121
picture(picture),
2222
pixelColor(nullptr),
23-
resultRef(result),
23+
resultPixmap(result),
24+
captured(captured),
2425
rectLineWidth(1),
2526
pointRadius(9),
2627
deltaHeight(0),
@@ -96,11 +97,14 @@ FreeSnapDialog::FreeSnapDialog(QPixmap picture, QPixmap*& result, QWidget *paren
9697
setRectVisible(false);
9798
refreshPointPos();
9899
refreshGrayArea();
100+
update();
99101
}
100102

101103
FreeSnapDialog::~FreeSnapDialog()
102104
{
103-
resultRef = NULL;
105+
scene->removeItem(grayItem);
106+
delete grayItem;
107+
resultPixmap = nullptr;
104108
delete ui;
105109
}
106110

@@ -463,12 +467,12 @@ void FreeSnapDialog::refreshPreviewArea(QPoint mousePos)
463467
void FreeSnapDialog::keyPressEvent(QKeyEvent *event)
464468
{
465469
if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) {
466-
*this->resultRef = picture.copy(ui->frameRect->geometry());
470+
*this->resultPixmap = picture.copy(ui->frameRect->geometry());
471+
captured = true;
467472
close();
468473
return;
469474
} else if (event->key() == Qt::Key_Escape) {
470-
delete this->resultRef;
471-
this->resultRef = NULL;
475+
captured = false;
472476
close();
473477
return;
474478
} else if (event->key() == Qt::Key_C) { // C键复制颜色

SmartScreenSnapper/src/freesnapdialog.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ class FreeSnapDialog : public QDialog
2727
Q_OBJECT
2828

2929
public:
30-
explicit FreeSnapDialog(QPixmap picture, QPixmap*& result, QWidget *parent = nullptr);
30+
explicit FreeSnapDialog(QPixmap picture, QPixmap* result, bool &captured, QWidget *parent = nullptr);
3131
~FreeSnapDialog();
3232

3333
private:
@@ -47,7 +47,9 @@ class FreeSnapDialog : public QDialog
4747

4848
QColor* pixelColor;
4949

50-
QPixmap*& resultRef;
50+
QPixmap* resultPixmap;
51+
52+
bool &captured;
5153

5254
int rectLineWidth;
5355
int pointRadius;

SmartScreenSnapper/src/graphicsview.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
#include <QDrag>
55
#include <QBuffer>
66

7-
GraphicsView::GraphicsView(QWidget *parent): QGraphicsView(parent), mimeData(NULL)
7+
GraphicsView::GraphicsView(QWidget *parent): QGraphicsView(parent), mimeData(nullptr)
88
{
99
this->mousePressed = false;
1010
lastMousePoint.setX(-1);
@@ -42,13 +42,14 @@ void GraphicsView::mousePressEvent(QMouseEvent *event)
4242
lastMousePoint.setX(event->x());
4343
lastMousePoint.setY(event->y());
4444

45-
if (event->buttons() & Qt::RightButton) { //准备右键拖出时的图片
46-
QImage image(QSize(scene()->width(), scene()->height()), QImage::Format_ARGB32);
47-
QPainter painter(&image);
45+
if (event->buttons() & Qt::RightButton) { // 准备右键拖出时的图片
46+
QPixmap pixmap(scene()->width(), scene()->height());
47+
pixmap.fill(Qt::transparent);
48+
QPainter painter(&pixmap);
4849
scene()->render(&painter);
4950
mimeData = new QMimeData();
5051
mimeData->clear();
51-
mimeData->setImageData(QImage(image));
52+
mimeData->setImageData(pixmap.toImage());
5253
}
5354
}
5455

0 commit comments

Comments
 (0)