WindowsFramelessHelper.cpp 11 KB

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