-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmainwindow.cpp
167 lines (115 loc) · 3.97 KB
/
mainwindow.cpp
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
#include "mainwindow.h"
#include<QDebug>
#include"aboutdialog.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
{
topLabelList.clear();
isShotting=false;
registerGlobalKey();
initTray();
aboutDialog =new AboutDialog;
connect(this,SIGNAL(hotkeyShotBgPressed()),SLOT(hotkeyShotBgReceived()));
trayIcon->showMessage(tr("截图置顶 v1.2.0"),"程序已启动,按\"SHIFT+ALT+Z\"后拖动鼠标!");
trayIcon->setToolTip(tr("按\"SHIFT+ALT+Z\"后拖动鼠标"));
}
MainWindow::~MainWindow()
{
if(UnregisterHotKey(HWND(this->winId()), hotkeyShotBgId))
{
qDebug("SHIFT+ALT+Z IS UNREGISTED");
}
clearShots();
}
void MainWindow::registerGlobalKey()
{
hotkeyShotBgId =GlobalAddAtom(L"SsotGlobalHotKey") - 0xC000;
if(RegisterHotKey((HWND(this->winId())), hotkeyShotBgId,MOD_SHIFT|MOD_ALT,0x5A))
{
qDebug("SHIFT+ALT+Z");
}
}
bool MainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
if(eventType=="windows_generic_MSG")
{
//qDebug("This is windows MSG");
MSG* msg=static_cast<MSG*>(message);
if(msg->message==WM_HOTKEY)
{
UINT fuModifiers = (UINT) LOWORD(msg->lParam); // key-modifier flags
UINT uVirtKey = (UINT) HIWORD(msg->lParam); // virtual-key code
// qDebug("This is HotKey!");
if(fuModifiers==(MOD_SHIFT|MOD_ALT) && uVirtKey==0x5A)
{
emit hotkeyShotBgPressed();
qDebug("SHIFT+ALT+Z is Pressed");
}
return true;
}
}
return false;
}
void MainWindow::hotkeyShotBgReceived()
{
if(!isShotting)
{
//如果正在截图,则不处理,否则进入截图状态,将list位置传给CTopLabel
CTopLabel* fullScreenLabel=new CTopLabel;
topLabelList.append(fullScreenLabel);
connect(fullScreenLabel, SIGNAL(destroyed(QObject*)), SLOT(removeLabel(QObject*)));
//connect(fullScreenLabel,SIGNAL(meClosed(WId)),this,SLOT(clearShot(WId)));
connect(fullScreenLabel,SIGNAL(shotted()),this,SLOT(allowShot()));
fullScreenLabel->showFullScreen();
}
isShotting = true;
}
void MainWindow::initTray()
{
trayIcon=new QSystemTrayIcon(this);
trayIcon->setIcon(QIcon(":/icon/resource/icon.png"));
QMenu* trayMenu=new QMenu;
QAction* exitAction=new QAction(tr("退出 (&X)"),trayMenu);
QAction* aboutAciton=new QAction(tr("关于 (&A)"),trayMenu);
QAction* clearAciton=new QAction(tr("清除截图 (&C)"),trayMenu);
trayMenu->addAction(clearAciton);
connect(clearAciton,SIGNAL(triggered()),this,SLOT(clearShots()));
trayMenu->addSeparator();
trayMenu->addAction(aboutAciton);
connect(aboutAciton,SIGNAL(triggered()),this,SLOT(doAboutAction()));
trayMenu->addAction(exitAction);
connect(exitAction,SIGNAL(triggered()),this,SLOT(quitApp()));
trayIcon->setContextMenu(trayMenu);
trayIcon->show();
}
void MainWindow::quitApp(){
trayIcon->hide();
QApplication::quit();
}
void MainWindow::removeLabel(QObject *label)
{
topLabelList.removeAll(static_cast<CTopLabel*>(label));
}
void MainWindow::clearShots()
{
while(topLabelList.count()>0)
{
if(topLabelList.first()){
topLabelList.first()->deleteLater();
}
topLabelList.removeFirst();
}
}
void MainWindow::allowShot()
{
isShotting=false;
}
void MainWindow::doAboutAction()
{
qDebug()<<"about";
aboutDialog->setText(":/icon/readme.txt",true);
aboutDialog->setWindowTitle(tr("关于Ssot v1.2.0"));
aboutDialog->setLogo(":/icon/resource/about-logo.png");
aboutDialog->setInfo("<table border=\"0\"><tr height=\"22\"><td width=270 valign=\"middle\" ><b>Ssot v1.2.0</b></td><td><a href=\"https://github.com/pansinm/Ssot/\">查看源代码</a></td></tr><tr height=\"22\"><td width=300 valign=\"middle\">by pansinm</td><td>[email protected]</td></tr></table>");
aboutDialog->show();
}