forked from nuttyartist/notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframelesswindow.cpp
More file actions
302 lines (275 loc) · 9.75 KB
/
Copy pathframelesswindow.cpp
File metadata and controls
302 lines (275 loc) · 9.75 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
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
#include "framelesswindow.h"
#include <QApplication>
#include <QPoint>
#include <QSize>
#ifdef Q_OS_WIN
#if defined(__MINGW32__) || defined(__GNUC__)
#else
#include <windows.h>
#include <WinUser.h>
#include <windowsx.h>
#include <dwmapi.h>
#include <objidl.h> // Fixes error C2504: 'IUnknown' : base class undefined
#include <gdiplus.h>
#include <gdipluscolor.h>
#pragma comment (lib,"Dwmapi.lib") // Adds missing library, fixes error LNK2019: unresolved external symbol __imp__DwmExtendFrameIntoClientArea
#pragma comment (lib,"user32.lib")
CFramelessWindow::CFramelessWindow(QWidget *parent)
: QMainWindow(parent),
m_titlebar(Q_NULLPTR),
m_borderWidth(5),
m_bJustMaximized(false),
m_bResizeable(true)
{
// setWindowFlag(Qt::Window,true);
// setWindowFlag(Qt::FramelessWindowHint, true);
// setWindowFlag(Qt::WindowSystemMenuHint, true);
// setWindowFlag() is not avaliable before Qt v5.9, so we should use setWindowFlags instead
setWindowFlags(windowFlags() | Qt::Window | Qt::FramelessWindowHint | Qt::WindowSystemMenuHint);
setResizeable(m_bResizeable);
}
void CFramelessWindow::setResizeable(bool resizeable)
{
bool visible = isVisible();
m_bResizeable = resizeable;
if (m_bResizeable){
setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint);
// setWindowFlag(Qt::WindowMaximizeButtonHint);
//此行代码可以带回Aero效果,同时也带回了标题栏和边框,在nativeEvent()会再次去掉标题栏
//
//this line will get titlebar/thick frame/Aero back, which is exactly what we want
//we will get rid of titlebar and thick frame again in nativeEvent() later
HWND hwnd = (HWND)this->winId();
DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
::SetWindowLong(hwnd, GWL_STYLE, style | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CAPTION);
}else{
setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
// setWindowFlag(Qt::WindowMaximizeButtonHint,false);
HWND hwnd = (HWND)this->winId();
DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);
::SetWindowLong(hwnd, GWL_STYLE, style & ~WS_MAXIMIZEBOX & ~WS_CAPTION);
}
//保留一个像素的边框宽度,否则系统不会绘制边框阴影
//
//we better left 1 piexl width of border untouch, so OS can draw nice shadow around it
const MARGINS shadow = { 1, 1, 1, 1 };
DwmExtendFrameIntoClientArea(HWND(winId()), &shadow);
setVisible(visible);
}
void CFramelessWindow::setResizeableAreaWidth(int width)
{
if (1 > width) width = 1;
m_borderWidth = width;
}
void CFramelessWindow::setTitleBar(QWidget* titlebar)
{
m_titlebar = titlebar;
if (!titlebar) return;
connect(titlebar, SIGNAL(destroyed(QObject*)), this, SLOT(onTitleBarDestroyed()));
}
void CFramelessWindow::onTitleBarDestroyed()
{
if (m_titlebar == QObject::sender())
{
m_titlebar = Q_NULLPTR;
}
}
void CFramelessWindow::addIgnoreWidget(QWidget* widget)
{
if (!widget) return;
if (m_whiteList.contains(widget)) return;
m_whiteList.append(widget);
}
bool CFramelessWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
//Workaround for known bug -> check Qt forum : https://forum.qt.io/topic/93141/qtablewidget-itemselectionchanged/13
#if (QT_VERSION == QT_VERSION_CHECK(5, 11, 1))
MSG* msg = *reinterpret_cast<MSG**>(message);
#else
MSG* msg = reinterpret_cast<MSG*>(message);
#endif
switch (msg->message)
{
case WM_NCCALCSIZE:
{
NCCALCSIZE_PARAMS& params = *reinterpret_cast<NCCALCSIZE_PARAMS*>(msg->lParam);
if (params.rgrc[0].top != 0)
params.rgrc[0].top -= 1;
//this kills the window frame and title bar we added with WS_THICKFRAME and WS_CAPTION
*result = WVR_REDRAW;
return true;
}
case WM_NCHITTEST:
{
*result = 0;
const LONG border_width = m_borderWidth;
RECT winrect;
GetWindowRect(HWND(winId()), &winrect);
long x = GET_X_LPARAM(msg->lParam);
long y = GET_Y_LPARAM(msg->lParam);
if(m_bResizeable)
{
bool resizeWidth = minimumWidth() != maximumWidth();
bool resizeHeight = minimumHeight() != maximumHeight();
if(resizeWidth)
{
//left border
if (x >= winrect.left && x < winrect.left + border_width)
{
*result = HTLEFT;
}
//right border
if (x < winrect.right && x >= winrect.right - border_width)
{
*result = HTRIGHT;
}
}
if(resizeHeight)
{
//bottom border
if (y < winrect.bottom && y >= winrect.bottom - border_width)
{
*result = HTBOTTOM;
}
//top border
if (y >= winrect.top && y < winrect.top + border_width)
{
*result = HTTOP;
}
}
if(resizeWidth && resizeHeight)
{
//bottom left corner
if (x >= winrect.left && x < winrect.left + border_width &&
y < winrect.bottom && y >= winrect.bottom - border_width)
{
*result = HTBOTTOMLEFT;
}
//bottom right corner
if (x < winrect.right && x >= winrect.right - border_width &&
y < winrect.bottom && y >= winrect.bottom - border_width)
{
*result = HTBOTTOMRIGHT;
}
//top left corner
if (x >= winrect.left && x < winrect.left + border_width &&
y >= winrect.top && y < winrect.top + border_width)
{
*result = HTTOPLEFT;
}
//top right corner
if (x < winrect.right && x >= winrect.right - border_width &&
y >= winrect.top && y < winrect.top + border_width)
{
*result = HTTOPRIGHT;
}
}
}
if (0!=*result) return true;
//*result still equals 0, that means the cursor locate OUTSIDE the frame area
//but it may locate in titlebar area
if (!m_titlebar) return false;
//support highdpi
double dpr = this->devicePixelRatioF();
QPoint pos = m_titlebar->mapFromGlobal(QPoint(x/dpr,y/dpr));
if (!m_titlebar->rect().contains(pos)) return false;
QWidget* child = m_titlebar->childAt(pos);
if (!child)
{
*result = HTCAPTION;
return true;
}else{
if (m_whiteList.contains(child))
{
*result = HTCAPTION;
return true;
}
}
return false;
} //end case WM_NCHITTEST
case WM_GETMINMAXINFO:
{
if (::IsZoomed(msg->hwnd)) {
RECT frame = { 0, 0, 0, 0 };
AdjustWindowRectEx(&frame, WS_OVERLAPPEDWINDOW, FALSE, 0);
//record frame area data
double dpr = this->devicePixelRatioF();
m_frames.setLeft(abs(frame.left)/dpr+0.5);
m_frames.setTop(abs(frame.bottom)/dpr+0.5);
m_frames.setRight(abs(frame.right)/dpr+0.5);
m_frames.setBottom(abs(frame.bottom)/dpr+0.5);
QMainWindow::setContentsMargins(m_frames.left()+m_margins.left(), \
m_frames.top()+m_margins.top(), \
m_frames.right()+m_margins.right(), \
m_frames.bottom()+m_margins.bottom());
m_bJustMaximized = true;
}else {
if (m_bJustMaximized)
{
QMainWindow::setContentsMargins(m_margins);
m_frames = QMargins();
m_bJustMaximized = false;
}
}
return false;
}
default:
return QMainWindow::nativeEvent(eventType, message, result);
}
}
void CFramelessWindow::setContentsMargins(const QMargins &margins)
{
QMainWindow::setContentsMargins(margins+m_frames);
m_margins = margins;
}
void CFramelessWindow::setContentsMargins(int left, int top, int right, int bottom)
{
QMainWindow::setContentsMargins(left+m_frames.left(),\
top+m_frames.top(), \
right+m_frames.right(), \
bottom+m_frames.bottom());
m_margins.setLeft(left);
m_margins.setTop(top);
m_margins.setRight(right);
m_margins.setBottom(bottom);
}
QMargins CFramelessWindow::contentsMargins() const
{
QMargins margins = QMainWindow::contentsMargins();
margins -= m_frames;
return margins;
}
void CFramelessWindow::getContentsMargins(int *left, int *top, int *right, int *bottom) const
{
QMainWindow::getContentsMargins(left,top,right,bottom);
if (!(left&&top&&right&&bottom)) return;
if (isMaximized())
{
*left -= m_frames.left();
*top -= m_frames.top();
*right -= m_frames.right();
*bottom -= m_frames.bottom();
}
}
QRect CFramelessWindow::contentsRect() const
{
QRect rect = QMainWindow::contentsRect();
int width = rect.width();
int height = rect.height();
rect.setLeft(rect.left() - m_frames.left());
rect.setTop(rect.top() - m_frames.top());
rect.setWidth(width);
rect.setHeight(height);
return rect;
}
void CFramelessWindow::showFullScreen()
{
if (isMaximized())
{
QMainWindow::setContentsMargins(m_margins);
m_frames = QMargins();
}
QMainWindow::showFullScreen();
}
#endif
#endif //Q_OS_WIN