From bc8868d519702c5a55f21377711cf4db54714650 Mon Sep 17 00:00:00 2001 From: SineStriker <55847490+SineStriker@users.noreply.github.com> Date: 周一, 29 1月 2024 22:03:10 +0800 Subject: [PATCH] Fix centerize issue (#34) --- src/widgets/widgetitemdelegate.cpp | 4 ++ src/widgets/widgetitemdelegate_p.h | 1 src/core/contexts/abstractwindowcontext.cpp | 8 ++-- src/core/contexts/win32windowcontext_p.h | 2 - src/core/windowitemdelegate_p.h | 1 src/quick/quickitemdelegate_p.h | 1 examples/mainwindow/mainwindow.cpp | 2 + src/quick/quickitemdelegate.cpp | 4 ++ src/core/contexts/win32windowcontext.cpp | 39 +------------------ 9 files changed, 19 insertions(+), 43 deletions(-) diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index c27bddd..9bb7ab4 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -51,6 +51,8 @@ setWindowTitle(tr("Example MainWindow")); resize(800, 600); + + windowAgent->centralize(); } static inline void emulateLeaveEvent(QWidget *widget) { diff --git a/src/core/contexts/abstractwindowcontext.cpp b/src/core/contexts/abstractwindowcontext.cpp index ff35f76..6f2e421 100644 --- a/src/core/contexts/abstractwindowcontext.cpp +++ b/src/core/contexts/abstractwindowcontext.cpp @@ -124,8 +124,7 @@ } #ifdef Q_OS_MAC - void - AbstractWindowContext::setSystemButtonAreaCallback(const ScreenRectCallback &callback) { + void AbstractWindowContext::setSystemButtonAreaCallback(const ScreenRectCallback &callback) { m_systemButtonAreaCallback = callback; virtual_hook(SystemButtonAreaChangedHook, nullptr); } @@ -206,9 +205,10 @@ if (!m_windowHandle) return; + QRect windowGeometry = m_delegate->getGeometry(m_host); QRect screenGeometry = m_windowHandle->screen()->geometry(); - int x = (screenGeometry.width() - m_windowHandle->width()) / 2; - int y = (screenGeometry.height() - m_windowHandle->height()) / 2; + int x = (screenGeometry.width() - windowGeometry.width()) / 2; + int y = (screenGeometry.height() - windowGeometry.height()) / 2; QPoint pos(x, y); pos += screenGeometry.topLeft(); m_windowHandle->setPosition(pos); diff --git a/src/core/contexts/win32windowcontext.cpp b/src/core/contexts/win32windowcontext.cpp index 3f6623e..4a859c7 100644 --- a/src/core/contexts/win32windowcontext.cpp +++ b/src/core/contexts/win32windowcontext.cpp @@ -98,18 +98,6 @@ return monitorInfo; } - static inline void moveWindowToDesktopCenter(HWND hwnd) { - MONITORINFOEXW monitorInfo = getMonitorForWindow(hwnd); - RECT windowRect{}; - ::GetWindowRect(hwnd, &windowRect); - const auto newX = monitorInfo.rcMonitor.left + - (RECT_WIDTH(monitorInfo.rcMonitor) - RECT_WIDTH(windowRect)) / 2; - const auto newY = monitorInfo.rcMonitor.top + - (RECT_HEIGHT(monitorInfo.rcMonitor) - RECT_HEIGHT(windowRect)) / 2; - ::SetWindowPos(hwnd, nullptr, newX, newY, 0, 0, - SWP_NOACTIVATE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOOWNERZORDER); - } - static inline void moveWindowToMonitor(HWND hwnd, const MONITORINFOEXW &activeMonitor) { RECT currentMonitorRect = getMonitorForWindow(hwnd).rcMonitor; RECT activeMonitorRect = activeMonitor.rcMonitor; @@ -563,14 +551,6 @@ void Win32WindowContext::virtual_hook(int id, void *data) { switch (id) { - case CentralizeHook: { - if (!windowId) - return; - const auto hwnd = reinterpret_cast<HWND>(windowId); - moveWindowToDesktopCenter(hwnd); - return; - } - case RaiseWindowHook: { if (!windowId) return; @@ -606,8 +586,8 @@ return; } - case DrawWindows10BorderHook: { #if QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS) + case DrawWindows10BorderHook: { if (!windowId) return; @@ -647,12 +627,10 @@ QPoint{m_windowHandle->width(), 0} }); painter.restore(); -#endif return; } case DrawWindows10BorderHook2: { -#if QWINDOWKIT_CONFIG(ENABLE_WINDOWS_SYSTEM_BORDERS) if (!m_windowHandle) return; @@ -673,9 +651,9 @@ ::FillRect(hdc, &rcTopBorder, reinterpret_cast<HBRUSH>(::GetStockObject(BLACK_BRUSH))); ::ReleaseDC(hWnd, hdc); -#endif return; } +#endif default: break; @@ -1328,19 +1306,6 @@ bool Win32WindowContext::customWindowHandler(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam, LRESULT *result) { switch (message) { - case WM_SHOWWINDOW: { - if (!initialCentered) { - // If wParam is TRUE, the window is being shown. - // If lParam is zero, the message was sent because of a call to the ShowWindow - // function. - if (wParam && !lParam) { - initialCentered = true; - moveWindowToDesktopCenter(hWnd); - } - } - break; - } - case WM_NCHITTEST: { // 鍘熺敓Win32绐楀彛鍙湁椤惰竟鏄湪绐楀彛鍐呴儴resize鐨勶紝鍏朵綑涓夎竟閮芥槸鍦ㄧ獥鍙� // 澶栭儴杩涜resize鐨勶紝鍏跺師鐞嗘槸锛學S_THICKFRAME杩欎釜绐楀彛鏍峰紡浼氬湪绐� diff --git a/src/core/contexts/win32windowcontext_p.h b/src/core/contexts/win32windowcontext_p.h index 39a2767..b99c90d 100644 --- a/src/core/contexts/win32windowcontext_p.h +++ b/src/core/contexts/win32windowcontext_p.h @@ -72,8 +72,6 @@ // Whether the last mouse leave message is blocked, mainly for handling the unexpected // WM_MOUSELEAVE. bool mouseLeaveBlocked = false; - - bool initialCentered = false; }; } diff --git a/src/core/windowitemdelegate_p.h b/src/core/windowitemdelegate_p.h index b2c7520..1c57c36 100644 --- a/src/core/windowitemdelegate_p.h +++ b/src/core/windowitemdelegate_p.h @@ -40,6 +40,7 @@ virtual bool isWindowActive(const QObject *host) const = 0; virtual Qt::WindowStates getWindowState(const QObject *host) const = 0; virtual Qt::WindowFlags getWindowFlags(const QObject *host) const = 0; + virtual QRect getGeometry(const QObject *host) const = 0; // Callbacks virtual void resetQtGrabbedControl(QObject *host) const; diff --git a/src/quick/quickitemdelegate.cpp b/src/quick/quickitemdelegate.cpp index c370931..3a0a3dd 100644 --- a/src/quick/quickitemdelegate.cpp +++ b/src/quick/quickitemdelegate.cpp @@ -68,6 +68,10 @@ return static_cast<const QQuickWindow *>(host)->flags(); } + QRect QuickItemDelegate::getGeometry(const QObject *host) const { + return static_cast<const QQuickWindow *>(host)->geometry(); + } + void QuickItemDelegate::setWindowFlags(QObject *host, Qt::WindowFlags flags) const { static_cast<QQuickWindow *>(host)->setFlags(flags); } diff --git a/src/quick/quickitemdelegate_p.h b/src/quick/quickitemdelegate_p.h index 55f2586..3dde59d 100644 --- a/src/quick/quickitemdelegate_p.h +++ b/src/quick/quickitemdelegate_p.h @@ -38,6 +38,7 @@ bool isWindowActive(const QObject *host) const override; Qt::WindowStates getWindowState(const QObject *host) const override; Qt::WindowFlags getWindowFlags(const QObject *host) const override; + QRect getGeometry(const QObject *host) const override; void setWindowState(QObject *host, Qt::WindowStates state) const override; void setCursorShape(QObject *host, Qt::CursorShape shape) const override; diff --git a/src/widgets/widgetitemdelegate.cpp b/src/widgets/widgetitemdelegate.cpp index c81e6fb..4c7a40b 100644 --- a/src/widgets/widgetitemdelegate.cpp +++ b/src/widgets/widgetitemdelegate.cpp @@ -92,6 +92,10 @@ return static_cast<const QWidget *>(host)->windowFlags(); } + QRect WidgetItemDelegate::getGeometry(const QObject *host) const { + return static_cast<const QWidget *>(host)->geometry(); + } + void WidgetItemDelegate::setWindowFlags(QObject *host, Qt::WindowFlags flags) const { static_cast<QWidget *>(host)->setWindowFlags(flags); } diff --git a/src/widgets/widgetitemdelegate_p.h b/src/widgets/widgetitemdelegate_p.h index f63077c..71ff94f 100644 --- a/src/widgets/widgetitemdelegate_p.h +++ b/src/widgets/widgetitemdelegate_p.h @@ -38,6 +38,7 @@ bool isWindowActive(const QObject *host) const override; Qt::WindowStates getWindowState(const QObject *host) const override; Qt::WindowFlags getWindowFlags(const QObject *host) const override; + QRect getGeometry(const QObject *host) const override; void resetQtGrabbedControl(QObject *host) const override; void setWindowState(QObject *host, Qt::WindowStates state) const override; -- Gitblit v1.9.1