-
Notifications
You must be signed in to change notification settings - Fork 0
/
BorderlessWindow.cpp
146 lines (119 loc) · 4 KB
/
BorderlessWindow.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
#include "BorderlessWindow.hpp"
#include <QMargins>
#include <Windowsx.h>
#include <Windows.h>
#include <errno.h>
#include <QDebug>
namespace {
const auto AERO_STYLE = (WS_POPUP | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CLIPCHILDREN);
}
BorderlessWindow::BorderlessWindow(QWindow *parent) : QQuickWindow(parent)
{
modifyWindowStyle();
}
struct HitRegion
{
RECT bounds;
LRESULT region;
bool contains(const POINT& point) const
{
return (
(point.x >= bounds.left && point.x < bounds.right) &&
(point.y >= bounds.top && point.y < bounds.bottom)
);
}
};
LRESULT BorderlessWindow::hitTest(POINT point) const
{
const auto borderX = GetSystemMetrics(SM_CYFRAME);
const auto borderY = GetSystemMetrics(SM_CYFRAME);
const auto xPos = x();
const auto yPos = y();
RECT winrect{xPos, yPos, xPos+width(), yPos+height()};
const auto centerRegionSpacing = 20;
if ((point.x >= xPos + width() / 2 - centerRegionSpacing && point.x <= xPos + width() / 2 + centerRegionSpacing) &&
(point.y >= yPos + height() / 2 - centerRegionSpacing && point.y <= yPos + height() / 2 + centerRegionSpacing))
{
return HTCAPTION;
}
const HitRegion hitregions[]
{
{
{ winrect.left, winrect.bottom - borderY, winrect.left + borderX, winrect.bottom }, HTBOTTOMLEFT
},
{
{ winrect.right - borderX, winrect.bottom - borderY, winrect.right, winrect.bottom }, HTBOTTOMRIGHT
},
{
{ winrect.left, winrect.top, winrect.left + borderX , winrect.top + borderY }, HTTOPLEFT
},
{
{ winrect.right - borderX , winrect.top, winrect.right, winrect.top + borderY }, HTTOPRIGHT
},
{
{ winrect.left, winrect.top, winrect.left + borderX , winrect.bottom }, HTLEFT
},
{
{ winrect.right - borderX , winrect.top, winrect.right, winrect.bottom }, HTRIGHT
},
{
{ winrect.left, winrect.top, winrect.right, winrect.top + borderY }, HTTOP
},
{
{ winrect.left, winrect.bottom - borderY, winrect.right, winrect.bottom }, HTBOTTOM
}
};
for (auto &&hr : hitregions)
{
if ( hr.contains(point) )
return hr.region;
}
return HTCLIENT;
}
bool BorderlessWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
MSG *msg = static_cast<MSG *>(message);
switch (msg->message)
{
case WM_SHOWWINDOW:
{
modifyWindowStyle();
return false;
}
break;
case WM_NCCALCSIZE:
{
// This is what kills the border and extends the client rect to the size of the window rect.
*result = 0;
return true;
}
break;
case WM_NCHITTEST:
{
// When we have no border or title bar, we need to perform our
// own hit testing to allow resizing and moving.
const POINT cursor {
GET_X_LPARAM(msg->lParam),
GET_Y_LPARAM(msg->lParam)
};
const auto ht = hitTest(cursor);
if (ht != HTNOWHERE)
{
*result = ht;
return true;
}
}
break;
}
return QQuickWindow::nativeEvent(eventType, message, result);
}
void BorderlessWindow::modifyWindowStyle()
{
// setFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::Window | Qt::WindowMinimizeButtonHint | Qt::WindowMaximizeButtonHint);
const auto hwnd = reinterpret_cast<HWND>(winId());
SetWindowLong(hwnd, GWL_STYLE, AERO_STYLE);
auto extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
extendedStyle &= ~(WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE | WS_EX_STATICEDGE);
SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle);
SetWindowPos(hwnd, 0, 0, 0, 0, 0, SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
}