-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.cpp
More file actions
187 lines (164 loc) · 5.11 KB
/
MainWindow.cpp
File metadata and controls
187 lines (164 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*************************************************************************
*
* This file is part of tgtsml_tools
*
* Create Date 2020/10/18
*
* Copyright: tgtsml
*
* Contact email: time_forget@outlook.com
*
* GitHub: www.github.com/tgtsml
*
*************************************************************************/
#include "MainWindow.h"
#include <QApplication>
#include <QScreen>
#include <QDesktopWidget>
#include <QPainter>
#include <QPixmap>
#include <QDebug>
#include <QMouseEvent>
#include <QContextMenuEvent>
#include <QMenu>
#include <QDateTime>
#include <QDir>
#define CURRENT_DATETIME QDateTime::currentDateTime().toString("yyyyMMddhhmmsszzz")
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), m_isMousePressed(false), m_startPos(1,1), m_endPos(1,1)
{
this->setWindowFlag(Qt::WindowStaysOnTopHint);
m_screenPicture = new QPixmap();
m_screenMenu = new QMenu(this);
m_screenMenu->addAction("保存截图", this, &MainWindow::slot_saveCapturedScreen);
m_screenMenu->addAction("保存全屏截图", this, &MainWindow::slot_saveFullScreen);
m_screenMenu->addAction("退出", this, &MainWindow::close);
QIcon appIcon(":/app.png");
this->setWindowIcon(appIcon);
m_trayMenu = new QMenu(this);
m_trayMenu->addAction("新建截图", this, &MainWindow::showFullScreen);
m_trayMenu->addAction("退出", this, &MainWindow::close);
QSystemTrayIcon *m_trayIcon = new QSystemTrayIcon(appIcon, this);
m_trayIcon->setContextMenu(m_trayMenu);
m_trayIcon->show();
connect(m_trayIcon, &QSystemTrayIcon::activated, this, &MainWindow::activateTrayIcon);
}
void MainWindow::activateTrayIcon(QSystemTrayIcon::ActivationReason reason)
{
if(reason == QSystemTrayIcon::DoubleClick){
this->showFullScreen();
}
}
MainWindow::~MainWindow()
{
if(m_screenPicture){
delete m_screenPicture;
}
}
void MainWindow::showEvent(QShowEvent *)
{
QSize desktopSize = QApplication::desktop()->size();
QScreen *pscreen = QApplication::primaryScreen();
*m_screenPicture = pscreen->grabWindow(QApplication::desktop()->winId(), 0, 0, desktopSize.width(), desktopSize.height());
QPixmap pix(desktopSize.width(), desktopSize.height());
pix.fill((QColor(0, 0, 0, 150)));
backgroundPicture = new QPixmap(*m_screenPicture);
QPainter painter(backgroundPicture);
painter.drawPixmap(0, 0, pix);
}
QRect MainWindow::getCapturedRect(QPoint startpos, QPoint endpos)
{
if(startpos == endpos){
return QRect();
}
QPoint tmpTopLeftPos = startpos, tmpBottomRightPos = endpos;
if(endpos.x() < startpos.x()){
tmpBottomRightPos.setX(startpos.x());
tmpTopLeftPos.setX(endpos.x());
}
if(endpos.y() < startpos.y()){
tmpBottomRightPos.setY(startpos.y());
tmpTopLeftPos.setY(endpos.y());
}
return QRect(tmpTopLeftPos, tmpBottomRightPos);
}
void MainWindow::paintEvent(QPaintEvent *)
{
QPainter painter(this);
QPen pen;
pen.setColor(Qt::red);
pen.setWidth(1);
painter.setPen(pen);
painter.drawPixmap(0, 0, *backgroundPicture);
QRect rect(getCapturedRect(m_startPos, m_endPos));
if (rect.isValid()) {
painter.drawPixmap(rect.x(), rect.y(), m_screenPicture->copy(rect));
}
pen.setColor(Qt::green);
painter.setPen(pen);
painter.drawText(rect.x(), rect.y() - 8, tr("截图范围:(%1 x %2) - (%3 x %4) 图片大小:(%5 x %6)")
.arg(rect.x())
.arg(rect.y())
.arg(rect.bottomRight().x())
.arg(rect.bottomRight().y())
.arg(rect.width())
.arg(rect.height()));
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
if(m_isMousePressed){
QPoint tmpPos = event->pos();
m_endPos = tmpPos;
this->update();
}
}
void MainWindow::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
m_isMousePressed = true;
m_startPos = event->pos();
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *)
{
m_isMousePressed = false;
}
void MainWindow::contextMenuEvent(QContextMenuEvent *)
{
this->setCursor(Qt::ArrowCursor);
m_screenMenu->exec(cursor().pos());
}
void MainWindow::clearScreenCaptureInfo()
{
m_startPos = m_endPos;
}
QString MainWindow::getSaveFileName()
{
QString saveDirPath = qApp->applicationDirPath();
QString picutreSaveDir("screenshot");
QDir saveDir(saveDirPath);
if(!saveDir.exists(picutreSaveDir)){
saveDir.mkdir(picutreSaveDir);
}
return saveDirPath + "/" + picutreSaveDir + "/screenshot_" + CURRENT_DATETIME + ".png";
}
void MainWindow::slot_saveCapturedScreen()
{
QRect rect(getCapturedRect(m_startPos, m_endPos));
m_screenPicture->copy(rect).save(getSaveFileName(), "png");
this->hide();
clearScreenCaptureInfo();
}
void MainWindow::slot_saveFullScreen()
{
m_screenPicture->save(getSaveFileName(), "png");
this->hide();
clearScreenCaptureInfo();
}
void MainWindow::keyPressEvent(QKeyEvent *event)
{
if(event->key() == Qt::Key_Escape){
this->hide();
clearScreenCaptureInfo();
}
}