This repository has been archived by the owner on Aug 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
widget.cpp
334 lines (275 loc) · 8.16 KB
/
widget.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#include "widget.h"
#include "ui_widget.h"
#include <QFileDialog>
#include <QDir>
#include <QSlider>
#include <QDebug>
#include <QRgb>
#include <QColor>
#include <QScreen>
#include <QFile>
#include <QByteArray>
#include <QPixmap>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
#include <QJsonValue>
#ifdef _WIN32
#include "windows.h"
#else
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#endif
#ifdef _WIN32
HHOOK mouseHook=NULL;
extern Widget* w;
LRESULT CALLBACK mouseProc(int nCode,WPARAM wParam,LPARAM lParam )
{
if(nCode == HC_ACTION) //当nCode等于HC_ACTION时,要求得到处理
{
if(wParam==WM_MOUSEMOVE)//鼠标的移动
{
POINT p;
GetCursorPos(&p);//获取鼠标坐标
CMask* mask = w->getMask();
mask->setMask(p.x,p.y);
//刷新壁纸
mask->update();
}
}
//qDebug()<<nCode<<","<<wParam<<","<<lParam;
return CallNextHookEx(mouseHook,nCode,wParam,lParam);//返回给下一个钩子子程处理
}
#endif
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);
m_mask = new CMask(nullptr);
m_mask->setWindowFlags(Qt::FramelessWindowHint);
QList<QScreen *> screenList = QGuiApplication::screens();
if(screenList.size()>0){
m_mask->setFixedSize(screenList.at(0)->size());
}else{
return;
}
qDebug()<<"width:"<<ui->lb_cover->width();
ui->lb_cover->setFixedHeight(432);
connect(ui->btn_load,SIGNAL(clicked(bool)),this,SLOT(slt_load()));
connect(ui->btn_make,SIGNAL(clicked(bool)),this,SLOT(slt_make()));
connect(ui->btn_close,SIGNAL(clicked(bool)),this,SLOT(slt_windowClose()));
connect(ui->btn_min,SIGNAL(clicked(bool)),this,SLOT(slt_windowMin()));
//qDebug() << "winid:" << this->winId();
}
Widget::~Widget()
{
delete ui;
}
void Widget::slt_make(){
QString dirpath = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
QDir::currentPath(),
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
qDebug()<<"path:"<<dirpath;
if(m_thread!=nullptr || m_maker!=nullptr){
return;
}
m_thread = new QThread(this);
m_maker = new CMaker(dirpath);
m_maker->moveToThread(m_thread);
connect(m_thread,SIGNAL(started()),m_maker,SLOT(start()));
connect(m_maker,SIGNAL(end()),m_thread,SLOT(quit()));
connect(m_thread,SIGNAL(finished()),this,SLOT(slt_threadDestory()));
m_thread->start();
}
void Widget::slt_windowClose(){
delete m_mask;
#ifdef _WIN32
//调用该函数刷新壁纸,使得壁纸恢复
SystemParametersInfoA(SPI_SETDESKWALLPAPER,0,NULL,SPIF_SENDWININICHANGE);
#endif
//关闭窗口
this->close();
}
void Widget::slt_windowMax(){
}
void Widget::slt_windowMin(){
this->showMinimized();
}
void Widget::slt_load(){
QString dirpath = QFileDialog::getExistingDirectory(this, tr("Open Directory"),
QDir::currentPath(),
QFileDialog::ShowDirsOnly
| QFileDialog::DontResolveSymlinks);
QFile file(dirpath+"/package.json");
if(!file.exists()){
return;
}
bool ok = file.open(QIODevice::ReadOnly);
if(!ok){
return;
}
QByteArray ba = file.readAll();
file.close();
QJsonParseError err;
QJsonDocument doc = QJsonDocument::fromJson(ba,&err);
if(doc.isNull()){
qDebug()<<"err:"<<err.errorString();
return;
}
if(!doc.isObject()){
return;
}
QJsonObject obj = doc.object();
QJsonValue val = obj.value("title");
if(!val.isString()){
return;
}
ui->lb_titleContent->setText(val.toString());
val = obj.value("auther");
if(!val.isString()){
return;
}
ui->lb_autherContent->setText(val.toString());
val = obj.value("email");
if(!val.isString()){
return;
}
ui->lb_emailContent->setText(val.toString());
val = obj.value("cover");
if(!val.isString()){
return;
}
QPixmap cover(dirpath+"/"+val.toString());
ui->lb_cover->setPixmap(cover.scaled(ui->lb_cover->width(),ui->lb_cover->width()));
val = obj.value("meta");
if(!val.isObject()){
return;
}
QJsonObject metaObj = val.toObject();
if(metaObj.isEmpty()){
return;
}
QJsonValue metaBase = metaObj.value("base");
if(!metaBase.isString()){
return;
}
QJsonValue metaCanvas = metaObj.value("canvas");
if(!metaCanvas.isString()){
return;
}
QString maskStr = "";
QJsonValue metaMask = metaObj.value("mask");
if(metaMask.isString()){
maskStr = dirpath+"/"+metaMask.toString();
}
m_mask->loadPackage(QImage(dirpath+"/"+metaBase.toString()),QImage(dirpath+"/"+metaCanvas.toString()),maskStr);
setBackGroundWMChild(m_mask);
m_mask->show();
#ifdef _WIN32
registerGolbalMouseEvent();
#endif
}
void Widget::slt_threadDestory(){
m_thread->deleteLater();
//m_thread = nullptr;
//qDebug()<<"delete thread";
if(m_maker!=nullptr){
delete m_maker;
m_maker = nullptr;
}
qDebug()<<"end";
}
void Widget::mousePressEvent(QMouseEvent *event){
m_lastPos = event->globalPos();
QWidget::mousePressEvent(event);
}
void Widget::mouseMoveEvent(QMouseEvent *event){
move(this->pos()+(event->globalPos()-m_lastPos));
m_lastPos = event->globalPos();
QWidget::mouseMoveEvent(event);
}
#ifdef _WIN32
void Widget::setBackGroundWMChild(QWidget* widget){
HWND background = NULL;
HWND hwnd = ::FindWindowA("progman","Program Manager");
int retry = 0;
while(1){
HWND worker = NULL;
do{
worker = ::FindWindowExA(NULL,worker,"WorkerW",NULL);
if(worker!=NULL){
char buff[200] = {0};
int ret = GetClassName(worker,(WCHAR*)buff,sizeof(buff)*2);
if(ret == 0){
int err = GetLastError();
qDebug()<<"err:"<<err;
}
//QString className = QString::fromUtf16((char16_t*)buff);
}
if(GetParent(worker) == hwnd){
background = worker;
}
}while(worker !=NULL);
if(background == NULL){
SendMessageA(hwnd,0x052C,0,0);
qDebug()<<"send message to create WorkerW";
}else{
break;
}
if(retry++ > 3){
return;
}
}
HWND current = (HWND)widget->winId();
SetParent(current,background);
}
void Widget::registerGolbalMouseEvent(){
qDebug()<<"register hook";
mouseHook =SetWindowsHookEx( WH_MOUSE_LL,mouseProc,GetModuleHandle(NULL),NULL);//注册鼠标钩子
}
#else
extern Window Window_With_Name(Display *dpy,Window top, char *name);
void Widget::setBackGroundWMChild(QWidget* widget){
Q_UNUSED(widget);
auto display = XOpenDisplay(nullptr);
auto root_window = DefaultRootWindow(display);
Window w = Window_With_Name(display,root_window,"桌面");
qDebug()<<"XID:"<<w;
}
Window Window_With_Name(Display *dpy,Window top, char *name)
{
Window *children, dummy;
unsigned int nchildren;
int i;
Window w=0;
XClassHint *class_hint;
class_hint = XAllocClassHint();
int ret = 0;
// find by WM_CLASS(STRING) = "mywindow", "MyWindow"
if(XGetClassHint(dpy, top, class_hint)){
if(strstr(class_hint->res_name, name) == class_hint->res_name){
ret = 1;
}
if(!class_hint->res_class){
XFree(class_hint->res_class);
XFree(class_hint->res_name);
}
if(ret){
return(top);
}
}
if(!XQueryTree(dpy, top, &dummy, &dummy, &children, &nchildren))
return(0);
for(i=0; i<nchildren; i++)
{
w = Window_With_Name(dpy, children[i], name);
if (w)
break;
}
if(children) XFree ((char *)children);
return(w);
}
#endif