From 5a2d498445559321ed69bd8f9a90466070a9fcb2 Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Wed, 11 Aug 2021 17:58:47 +0800 Subject: [PATCH] Minor tweaks Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- CMakeLists.txt | 16 +++++++++--- examples/common.pri | 2 +- examples/mainwindow/CMakeLists.txt | 8 +++--- examples/quick/CMakeLists.txt | 8 +++--- examples/widget/CMakeLists.txt | 8 +++--- framelesshelper.cpp | 21 ++++++++++++---- framelesshelper_win32.cpp | 40 ++++++++++++++++++------------ lib.pro | 6 +++-- 8 files changed, 72 insertions(+), 37 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b5a69b..b2cd9cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -94,8 +94,10 @@ if(NOT BUILD_SHARED_LIBS) endif() if(MSVC) - target_compile_options(${PROJECT_NAME} PRIVATE /utf-8) - if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")) + target_compile_options(${PROJECT_NAME} PRIVATE /utf-8 /permissive-) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options(${PROJECT_NAME} PRIVATE /JMC) + else() target_compile_options(${PROJECT_NAME} PRIVATE /guard:cf) target_link_options(${PROJECT_NAME} PRIVATE /GUARD:CF) endif() @@ -111,18 +113,24 @@ target_compile_definitions(${PROJECT_NAME} PRIVATE ) if(TEST_UNIX) - target_compile_definitions(${PROJECT_NAME} PRIVATE FRAMELESSHELPER_TEST_UNIX) + target_compile_definitions(${PROJECT_NAME} PRIVATE + FRAMELESSHELPER_TEST_UNIX + ) endif() if(WIN32) + set(NTDDI_WIN10_19H1 0x0A000007) target_compile_definitions(${PROJECT_NAME} PRIVATE WIN32_LEAN_AND_MEAN _CRT_SECURE_NO_WARNINGS UNICODE _UNICODE + WINVER=${NTDDI_WIN10_19H1} + _WIN32_WINNT=${NTDDI_WIN10_19H1} ) + unset(NTDDI_WIN10_19H1) target_link_libraries(${PROJECT_NAME} PRIVATE - user32 shell32 gdi32 dwmapi + dwmapi ) endif() diff --git a/examples/common.pri b/examples/common.pri index 7fb877d..857499f 100644 --- a/examples/common.pri +++ b/examples/common.pri @@ -15,7 +15,7 @@ win32 { _UNICODE CONFIG += windeployqt CONFIG -= embed_manifest_exe - LIBS += -luser32 -lshell32 -lgdi32 -ldwmapi + LIBS += -ldwmapi RC_FILE = $$PWD/windows.rc OTHER_FILES += $$PWD/windows.manifest } diff --git a/examples/mainwindow/CMakeLists.txt b/examples/mainwindow/CMakeLists.txt index e69e6d5..0dc2744 100644 --- a/examples/mainwindow/CMakeLists.txt +++ b/examples/mainwindow/CMakeLists.txt @@ -37,13 +37,15 @@ target_compile_definitions(MainWindow PRIVATE ) if(MSVC) - target_compile_options(MainWindow PRIVATE /utf-8) - if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")) + target_compile_options(MainWindow PRIVATE /utf-8 /permissive-) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options(MainWindow PRIVATE /JMC) + else() target_compile_options(MainWindow PRIVATE /guard:cf) target_link_options(MainWindow PRIVATE /GUARD:CF) endif() endif() if(WIN32) - target_link_libraries(MainWindow PRIVATE user32 shell32 gdi32 dwmapi) + target_link_libraries(MainWindow PRIVATE dwmapi) endif() diff --git a/examples/quick/CMakeLists.txt b/examples/quick/CMakeLists.txt index 24795eb..74a03c8 100644 --- a/examples/quick/CMakeLists.txt +++ b/examples/quick/CMakeLists.txt @@ -35,13 +35,15 @@ target_compile_definitions(Quick PRIVATE ) if(MSVC) - target_compile_options(Quick PRIVATE /utf-8) - if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")) + target_compile_options(Quick PRIVATE /utf-8 /permissive-) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options(Quick PRIVATE /JMC) + else() target_compile_options(Quick PRIVATE /guard:cf) target_link_options(Quick PRIVATE /GUARD:CF) endif() endif() if(WIN32) - target_link_libraries(Quick PRIVATE user32 shell32 gdi32 dwmapi) + target_link_libraries(Quick PRIVATE dwmapi) endif() diff --git a/examples/widget/CMakeLists.txt b/examples/widget/CMakeLists.txt index c5489fe..97f1d98 100644 --- a/examples/widget/CMakeLists.txt +++ b/examples/widget/CMakeLists.txt @@ -35,13 +35,15 @@ target_compile_definitions(Widget PRIVATE ) if(MSVC) - target_compile_options(Widget PRIVATE /utf-8) - if(NOT (CMAKE_BUILD_TYPE STREQUAL "Debug")) + target_compile_options(Widget PRIVATE /utf-8 /permissive-) + if(CMAKE_BUILD_TYPE STREQUAL "Debug") + target_compile_options(Widget PRIVATE /JMC) + else() target_compile_options(Widget PRIVATE /guard:cf) target_link_options(Widget PRIVATE /GUARD:CF) endif() endif() if(WIN32) - target_link_libraries(Widget PRIVATE user32 shell32 gdi32 dwmapi) + target_link_libraries(Widget PRIVATE dwmapi) endif() diff --git a/framelesshelper.cpp b/framelesshelper.cpp index c70d8ed..c8637e7 100644 --- a/framelesshelper.cpp +++ b/framelesshelper.cpp @@ -114,11 +114,22 @@ bool FramelessHelper::eventFilter(QObject *object, QEvent *event) return Qt::Edges{}; } (); const bool hitTestVisible = Utilities::isHitTestVisibleInChrome(window); - const bool isInTitlebarArea = (localMousePosition.y() > resizeBorderHeight) - && (localMousePosition.y() <= (titleBarHeight + resizeBorderHeight)) - && (localMousePosition.x() > resizeBorderWidth) - && (localMousePosition.x() < (windowWidth - resizeBorderWidth)) - && !hitTestVisible; + bool isInTitlebarArea = false; + if ((window->windowState() == Qt::WindowMaximized) + || (window->windowState() == Qt::WindowFullScreen)) { + isInTitlebarArea = (localMousePosition.y() >= 0) + && (localMousePosition.y() <= titleBarHeight) + && (localMousePosition.x() >= 0) + && (localMousePosition.x() <= windowWidth) + && !hitTestVisible; + } + if (window->windowState() == Qt::WindowNoState) { + isInTitlebarArea = (localMousePosition.y() > resizeBorderHeight) + && (localMousePosition.y() <= (titleBarHeight + resizeBorderHeight)) + && (localMousePosition.x() > resizeBorderWidth) + && (localMousePosition.x() < (windowWidth - resizeBorderWidth)) + && !hitTestVisible; + } const auto mouseEvent = static_cast(event); if (type == QEvent::MouseButtonDblClick) { if (mouseEvent->button() != Qt::MouseButton::LeftButton) { diff --git a/framelesshelper_win32.cpp b/framelesshelper_win32.cpp index a41a1e5..7078fd2 100644 --- a/framelesshelper_win32.cpp +++ b/framelesshelper_win32.cpp @@ -33,27 +33,27 @@ #ifndef WM_NCUAHDRAWCAPTION // Not documented, only available since Windows Vista -#define WM_NCUAHDRAWCAPTION 0x00AE +#define WM_NCUAHDRAWCAPTION (0x00AE) #endif #ifndef WM_NCUAHDRAWFRAME // Not documented, only available since Windows Vista -#define WM_NCUAHDRAWFRAME 0x00AF +#define WM_NCUAHDRAWFRAME (0x00AF) #endif #ifndef ABM_GETAUTOHIDEBAREX // Only available since Windows 8.1 -#define ABM_GETAUTOHIDEBAREX 0x0000000b +#define ABM_GETAUTOHIDEBAREX (0x0000000b) #endif #ifndef IsMinimized // Only available since Windows 2000 -#define IsMinimized(window) IsIconic(window) +#define IsMinimized(window) (IsIconic(window)) #endif #ifndef IsMaximized // Only available since Windows 2000 -#define IsMaximized(window) IsZoomed(window) +#define IsMaximized(window) (IsZoomed(window)) #endif #ifndef GET_X_LPARAM @@ -315,13 +315,12 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me // have the WS_POPUP size, so we don't have to worry about // borders, and the default frame will be fine. if (IsMaximized(msg->hwnd) && (window->windowState() != Qt::WindowFullScreen)) { - // Windows automatically adds a standard width border to all - // sides when a window is maximized. We have to remove it - // otherwise the content of our window will be cut-off from - // the screen. - // The value of border width and border height should be - // identical in most cases, when the scale factor is 1.0, it - // should be eight pixels. + // When a window is maximized, its size is actually a little bit more + // than the monitor's work area. The window is positioned and sized in + // such a way that the resize handles are outside of the monitor and + // then the window is clipped to the monitor so that the resize handle + // do not appear because you don't need them (because you can't resize + // a window when it's maximized unless you restore it). const int rbh = Utilities::getSystemMetric(window, SystemMetric::ResizeBorderHeight, true); clientRect->top += rbh; if (!shouldHaveWindowFrame()) { @@ -338,7 +337,7 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me // Make sure to use MONITOR_DEFAULTTONEAREST, so that this will // still find the right monitor even when we're restoring from // minimized. - if (IsMaximized(msg->hwnd)) { + if (IsMaximized(msg->hwnd) || (window->windowState() == Qt::WindowFullScreen)) { APPBARDATA abd; SecureZeroMemory(&abd, sizeof(abd)); abd.cbSize = sizeof(abd); @@ -573,9 +572,18 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me const int rbw = Utilities::getSystemMetric(window, SystemMetric::ResizeBorderWidth, true); const int rbh = Utilities::getSystemMetric(window, SystemMetric::ResizeBorderHeight, true); const int tbh = Utilities::getSystemMetric(window, SystemMetric::TitleBarHeight, true); - const bool isTitleBar = (localMouse.y() > rbh) && (localMouse.y() <= (rbh + tbh)) - && (localMouse.x() > rbw) && (localMouse.x() < (ww - rbw)) - && !Utilities::isHitTestVisibleInChrome(window); + bool isTitleBar = false; + if ((window->windowState() == Qt::WindowMaximized) + || (window->windowState() == Qt::WindowFullScreen)) { + isTitleBar = (localMouse.y() >= 0) && (localMouse.y() <= tbh) + && (localMouse.x() >= 0) && (localMouse.x() <= ww) + && !Utilities::isHitTestVisibleInChrome(window); + } + if (window->windowState() == Qt::WindowNoState) { + isTitleBar = (localMouse.y() > rbh) && (localMouse.y() <= (rbh + tbh)) + && (localMouse.x() > rbw) && (localMouse.x() < (ww - rbw)) + && !Utilities::isHitTestVisibleInChrome(window); + } const bool isTop = localMouse.y() <= rbh; if (shouldHaveWindowFrame()) { // This will handle the left, right and bottom parts of the frame diff --git a/lib.pro b/lib.pro index 4e34d84..96c19eb 100644 --- a/lib.pro +++ b/lib.pro @@ -30,11 +30,13 @@ win32 { WIN32_LEAN_AND_MEAN \ _CRT_SECURE_NO_WARNINGS \ UNICODE \ - _UNICODE + _UNICODE \ + WINVER=0x0A000007 \ + _WIN32_WINNT=0x0A000007 HEADERS += framelesshelper_win32.h SOURCES += \ utilities_win32.cpp \ framelesshelper_win32.cpp - LIBS += -luser32 -lshell32 -lgdi32 -ldwmapi + LIBS += -ldwmapi RC_FILE = framelesshelper.rc }