WindowsFramelessHelper.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. #include "WindowsFramelessHelper.h"
  2. #include <Windows.h>
  3. #include <windowsx.h>
  4. #include <QCoreApplication>
  5. #include <QScreen>
  6. #include <QOperatingSystemVersion>
  7. #include <QGraphicsDropShadowEffect>
  8. #include <QtWin>
  9. #include <qdebug.h>
  10. #include "../titlebar/TitleBar.h"
  11. QScopedPointer<NativeWindowFilter> NativeWindowFilter::s_instance;
  12. QHash<WindowsFramelessHelper *, WId> NativeWindowFilter::s_windows;
  13. QHash<QWindow *, WId> NativeWindowFilter::s_winIds;
  14. QHash<WId, WindowsFramelessHelper *> NativeWindowFilter::s_helpers;
  15. void NativeWindowFilter::deliver(QWindow *window, WindowsFramelessHelper *helper)
  16. {
  17. Q_CHECK_PTR(window);
  18. if (!s_instance) {
  19. QCoreApplication *appc = QCoreApplication::instance();
  20. if (appc) {
  21. auto filter = new NativeWindowFilter();
  22. appc->installNativeEventFilter(filter);
  23. }
  24. }
  25. if (helper) {
  26. WId newId = window->winId();
  27. WId oldId = s_windows.value(helper);
  28. if (newId != oldId) {
  29. s_helpers.insert(newId, helper);
  30. s_helpers.remove(oldId);
  31. s_windows.insert(helper, newId);
  32. s_winIds.insert(window, newId);
  33. }
  34. } else {
  35. WId oldId = s_winIds.take(window);
  36. WindowsFramelessHelper *helper = s_helpers.take(oldId);
  37. s_windows.remove(helper);
  38. }
  39. }
  40. bool NativeWindowFilter::nativeEventFilter(const QByteArray &eventType, void *message, long *result)
  41. {
  42. LPMSG msg = reinterpret_cast<LPMSG>(message);
  43. if (auto h = s_helpers.value(reinterpret_cast<WId>(msg->hwnd)))
  44. return h->nativeEventFilter(eventType, msg, result);
  45. return false;
  46. }
  47. WindowsFramelessHelper::WindowsFramelessHelper(QWidget *widget) : QObject(widget)
  48. {
  49. Q_CHECK_PTR(widget);
  50. m_widget = widget;
  51. if (QOperatingSystemVersion::current().majorVersion() != QOperatingSystemVersion::Windows7.majorVersion()) {
  52. m_widget->setWindowFlags(m_widget->windowFlags() | Qt::FramelessWindowHint);
  53. } else if (m_widget->parentWidget()) {
  54. m_widget->setWindowFlags(m_widget->parentWidget()->windowFlags() | Qt::FramelessWindowHint
  55. | Qt::WindowMinMaxButtonsHint);
  56. } else {
  57. m_widget->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowMinMaxButtonsHint);
  58. }
  59. m_widget->installEventFilter(this);
  60. m_titleBar = new TitleBar(m_widget);
  61. m_widget->resize(500, 500);
  62. m_titleBar->raise();
  63. m_titleBarHeight = m_titleBar->height();
  64. }
  65. WindowsFramelessHelper::~WindowsFramelessHelper()
  66. {
  67. if (m_window)
  68. NativeWindowFilter::deliver(m_window, nullptr);
  69. }
  70. void WindowsFramelessHelper::setMaximizedMargins(int left, int top, int right, int bottom)
  71. {
  72. m_maximizedMargins = QMargins(left, top, right, bottom);
  73. }
  74. void WindowsFramelessHelper::setMaximizedMargins(const QMargins &margins)
  75. {
  76. m_maximizedMargins = margins;
  77. }
  78. void WindowsFramelessHelper::setDraggableMargins(int left, int top, int right, int bottom)
  79. {
  80. m_draggableMargins = QMargins(left, top, right, bottom);
  81. }
  82. void WindowsFramelessHelper::setDraggableMargins(const QMargins &margins)
  83. {
  84. m_draggableMargins = margins;
  85. }
  86. void WindowsFramelessHelper::setTitleBar(TitleBar *titleBar)
  87. {
  88. if (titleBar != m_titleBar) {
  89. m_titleBar->deleteLater();
  90. m_titleBar = titleBar;
  91. m_titleBar->setParent(m_widget);
  92. m_titleBar->raise();
  93. m_titleBarHeight = m_titleBar->height();
  94. }
  95. }
  96. void WindowsFramelessHelper::setResizeEnabled(bool isEnabled)
  97. {
  98. m_isResizeEnabled = isEnabled;
  99. }
  100. bool WindowsFramelessHelper::getResizeEnabled() const
  101. {
  102. return m_isResizeEnabled;
  103. }
  104. bool WindowsFramelessHelper::nativeEventFilter(const QByteArray &eventType, void *msg, long *result)
  105. {
  106. Q_CHECK_PTR(m_window);
  107. LPMSG lpMsg = reinterpret_cast<LPMSG>(msg);
  108. WPARAM wParam = lpMsg->wParam;
  109. LPARAM lParam = lpMsg->lParam;
  110. if (WM_NCHITTEST == lpMsg->message) {
  111. *result = hitTest(GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam));
  112. return true;
  113. } else if (WM_NCACTIVATE == lpMsg->message) {
  114. if (!QtWin::isCompositionEnabled()) {
  115. if (result)
  116. *result = 1;
  117. return true;
  118. }
  119. } else if (WM_NCCALCSIZE == lpMsg->message) {
  120. if (TRUE == wParam) {
  121. if (isMaximized()) {
  122. NCCALCSIZE_PARAMS &params = *reinterpret_cast<NCCALCSIZE_PARAMS *>(lParam);
  123. QRect g = availableGeometry();
  124. QMargins m = maximizedMargins();
  125. params.rgrc[0].top = g.top() - m.top();
  126. params.rgrc[0].left = g.left() - m.left();
  127. params.rgrc[0].right = g.right() + m.right() + 1;
  128. params.rgrc[0].bottom = g.bottom() + m.bottom() + 1;
  129. }
  130. if (result)
  131. *result = 0;
  132. return true;
  133. }
  134. } else if (WM_GETMINMAXINFO == lpMsg->message) {
  135. LPMINMAXINFO lpMinMaxInfo = reinterpret_cast<LPMINMAXINFO>(lParam);
  136. QRect g = availableGeometry();
  137. QMargins m = maximizedMargins();
  138. lpMinMaxInfo->ptMaxPosition.x = -m.left();
  139. lpMinMaxInfo->ptMaxPosition.y = -m.top();
  140. lpMinMaxInfo->ptMaxSize.x = g.right() - g.left() + 1 + m.left() + m.right();
  141. lpMinMaxInfo->ptMaxSize.y = g.bottom() - g.top() + 1 + m.top() + m.bottom();
  142. lpMinMaxInfo->ptMinTrackSize.x = m_window->minimumWidth();
  143. lpMinMaxInfo->ptMinTrackSize.y = m_window->minimumHeight();
  144. lpMinMaxInfo->ptMaxTrackSize.x = m_window->maximumWidth();
  145. lpMinMaxInfo->ptMaxTrackSize.y = m_window->maximumHeight();
  146. if (result)
  147. *result = 0;
  148. return true;
  149. } else if (WM_NCLBUTTONDBLCLK == lpMsg->message) {
  150. auto minimumSize = m_window->minimumSize();
  151. auto maximumSize = m_window->maximumSize();
  152. if ((minimumSize.width() >= maximumSize.width()) || (minimumSize.height() >= maximumSize.height())) {
  153. if (result)
  154. *result = 0;
  155. return true;
  156. }
  157. } else if (WM_MOVE == lpMsg->message) {
  158. RECT rcClient;
  159. GetWindowRect(lpMsg->hwnd, &rcClient);
  160. SetWindowPos(lpMsg->hwnd, NULL, rcClient.left, rcClient.top, rcClient.right - rcClient.left,
  161. rcClient.bottom - rcClient.top, SWP_FRAMECHANGED);
  162. }
  163. #ifdef Q_CC_MSVC
  164. else if (WM_DPICHANGED == lpMsg->message) {
  165. qreal old = m_scaleFactor;
  166. if (HIWORD(wParam) < 144) {
  167. m_scaleFactor = 1.0;
  168. } else {
  169. m_scaleFactor = 2.0;
  170. }
  171. if (!qFuzzyCompare(old, m_scaleFactor)) {
  172. emit scaleFactorChanged(m_scaleFactor);
  173. }
  174. auto hWnd = reinterpret_cast<HWND>(m_window->winId());
  175. auto rect = reinterpret_cast<LPRECT>(lParam);
  176. SetWindowPos(hWnd, NULL, rect->left, rect->top, rect->right - rect->left, rect->bottom - rect->top,
  177. SWP_NOZORDER | SWP_NOACTIVATE);
  178. }
  179. #endif
  180. return false;
  181. }
  182. bool WindowsFramelessHelper::eventFilter(QObject *obj, QEvent *ev)
  183. {
  184. if (obj == m_widget && ev->type() == QEvent::WinIdChange) {
  185. initWindow();
  186. } else if (obj == m_widget && ev->type() == QEvent::Resize) {
  187. m_titleBar->resize(m_widget->width(), m_titleBar->height());
  188. m_titleBar->raise();
  189. }
  190. if (m_window == obj && ev->type() == QEvent::WinIdChange) {
  191. updateWindowStyle();
  192. } else if (m_window == obj && ev->type() == QEvent::Show) {
  193. updateWindowStyle();
  194. }
  195. return QObject::eventFilter(obj, ev);
  196. }
  197. qreal WindowsFramelessHelper::scaleFactor() const
  198. {
  199. return m_scaleFactor;
  200. }
  201. void WindowsFramelessHelper::initWindow()
  202. {
  203. QWindow *window = m_widget->windowHandle();
  204. Q_CHECK_PTR(window);
  205. m_window = window;
  206. if (m_window) {
  207. m_scaleFactor = m_window->screen()->devicePixelRatio();
  208. if (m_window->flags() & Qt::FramelessWindowHint) {
  209. m_window->installEventFilter(this);
  210. updateWindowStyle();
  211. }
  212. }
  213. }
  214. void WindowsFramelessHelper::updateWindowStyle()
  215. {
  216. Q_CHECK_PTR(m_window);
  217. HWND hWnd = reinterpret_cast<HWND>(m_window->winId());
  218. if (NULL == hWnd)
  219. return;
  220. else if (m_oldWindow == hWnd) {
  221. return;
  222. }
  223. m_oldWindow = hWnd;
  224. NativeWindowFilter::deliver(m_window, this);
  225. QOperatingSystemVersion currentVersion = QOperatingSystemVersion::current();
  226. if (currentVersion < QOperatingSystemVersion::Windows8) {
  227. return;
  228. }
  229. LONG oldStyle = WS_OVERLAPPEDWINDOW | WS_THICKFRAME | WS_CAPTION | WS_SYSMENU | WS_MAXIMIZEBOX | WS_MINIMIZEBOX;
  230. ;
  231. LONG newStyle = WS_POPUP | WS_THICKFRAME;
  232. if (QtWin::isCompositionEnabled())
  233. newStyle |= WS_CAPTION;
  234. if (m_window->flags() & Qt::CustomizeWindowHint) {
  235. if (m_window->flags() & Qt::WindowSystemMenuHint)
  236. newStyle |= WS_SYSMENU;
  237. if (m_window->flags() & Qt::WindowMinimizeButtonHint)
  238. newStyle |= WS_MINIMIZEBOX;
  239. if (m_window->flags() & Qt::WindowMaximizeButtonHint)
  240. newStyle |= WS_MAXIMIZEBOX;
  241. } else {
  242. newStyle |= WS_SYSMENU | WS_MINIMIZEBOX | WS_MAXIMIZEBOX;
  243. }
  244. LONG currentStyle = GetWindowLong(hWnd, GWL_STYLE);
  245. SetWindowLong(hWnd, GWL_STYLE, (currentStyle & ~oldStyle) | newStyle);
  246. SetWindowPos(hWnd, NULL, 0, 0, 0, 0, SWP_NOOWNERZORDER | SWP_NOZORDER | SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
  247. if (QtWin::isCompositionEnabled())
  248. QtWin::extendFrameIntoClientArea(m_window, 1, 1, 1, 1);
  249. }
  250. bool WindowsFramelessHelper::isMaximized() const
  251. {
  252. Q_CHECK_PTR(m_window);
  253. HWND hWnd = reinterpret_cast<HWND>(m_window->winId());
  254. if (NULL == hWnd)
  255. return false;
  256. WINDOWPLACEMENT windowPlacement;
  257. if (!GetWindowPlacement(hWnd, &windowPlacement))
  258. return false;
  259. return (SW_MAXIMIZE == windowPlacement.showCmd);
  260. }
  261. QRect WindowsFramelessHelper::availableGeometry() const
  262. {
  263. MONITORINFO mi { 0 };
  264. mi.cbSize = sizeof(MONITORINFO);
  265. auto hWnd = reinterpret_cast<HWND>(m_window->winId());
  266. auto hMonitor = MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST);
  267. if (!hMonitor || !GetMonitorInfoW(hMonitor, &mi)) {
  268. Q_ASSERT(NULL != hMonitor);
  269. return m_window->screen()->availableGeometry();
  270. }
  271. return QRect(mi.rcWork.left, mi.rcWork.top, mi.rcWork.right - mi.rcWork.left, mi.rcWork.bottom - mi.rcWork.top);
  272. }
  273. int WindowsFramelessHelper::hitTest(int x, int y) const
  274. {
  275. Q_CHECK_PTR(m_window);
  276. x = x / m_window->devicePixelRatio();
  277. y = y / m_window->devicePixelRatio();
  278. enum RegionMask
  279. {
  280. Client = 0x0000,
  281. Top = 0x0001,
  282. Left = 0x0010,
  283. Right = 0x0100,
  284. Bottom = 0x1000,
  285. };
  286. auto wfg = m_window->frameGeometry();
  287. QMargins draggableMargins = this->draggableMargins();
  288. int top = draggableMargins.top();
  289. int left = draggableMargins.left();
  290. int right = draggableMargins.right();
  291. int bottom = draggableMargins.bottom();
  292. if (top <= 0)
  293. top = GetSystemMetrics(SM_CYFRAME);
  294. if (left <= 0)
  295. left = GetSystemMetrics(SM_CXFRAME);
  296. if (right <= 0)
  297. right = GetSystemMetrics(SM_CXFRAME);
  298. if (bottom <= 0)
  299. bottom = GetSystemMetrics(SM_CYFRAME);
  300. auto result = (Top * (y < (wfg.top() + top))) | (Left * (x < (wfg.left() + left)))
  301. | (Right * (x > (wfg.right() - right))) | (Bottom * (y > (wfg.bottom() - bottom)));
  302. bool wResizable = m_isResizeEnabled ? m_window->minimumWidth() < m_window->maximumWidth() : false;
  303. bool hResizable = m_isResizeEnabled ? m_window->minimumHeight() < m_window->maximumHeight() : false;
  304. switch (result) {
  305. case Top | Left:
  306. return wResizable && hResizable ? HTTOPLEFT : HTCLIENT;
  307. case Top:
  308. return hResizable ? HTTOP : HTCLIENT;
  309. case Top | Right:
  310. return wResizable && hResizable ? HTTOPRIGHT : HTCLIENT;
  311. case Right:
  312. return wResizable ? HTRIGHT : HTCLIENT;
  313. case Bottom | Right:
  314. return wResizable && hResizable ? HTBOTTOMRIGHT : HTCLIENT;
  315. case Bottom:
  316. return hResizable ? HTBOTTOM : HTCLIENT;
  317. case Bottom | Left:
  318. return wResizable && hResizable ? HTBOTTOMLEFT : HTCLIENT;
  319. case Left:
  320. return wResizable ? HTLEFT : HTCLIENT;
  321. }
  322. auto pos = m_window->mapFromGlobal(QPoint(x, y));
  323. return HTCLIENT;
  324. }