diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index d3404dc..d106427 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -1,7 +1,11 @@ -#if(TARGET Qt${QT_VERSION_MAJOR}::Widgets) +find_package(QT NAMES Qt6 Qt5 COMPONENTS Widgets) +find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Widgets) + +if(TARGET Qt${QT_VERSION_MAJOR}::Widgets) add_subdirectory(widget) add_subdirectory(mainwindow) -#endif() +endif() + if(TARGET Qt${QT_VERSION_MAJOR}::Quick) add_subdirectory(quick) endif() diff --git a/examples/common.pri b/examples/common.pri index e9977c7..7fb877d 100644 --- a/examples/common.pri +++ b/examples/common.pri @@ -5,7 +5,7 @@ DEFINES += \ QT_NO_CAST_TO_ASCII \ QT_NO_KEYWORDS \ QT_DEPRECATED_WARNINGS \ - QT_DISABLE_DEPRECATED_BEFORE=0x060000 + QT_DISABLE_DEPRECATED_BEFORE=0x060100 RESOURCES += $$PWD/images.qrc win32 { DEFINES += \ diff --git a/examples/mainwindow/CMakeLists.txt b/examples/mainwindow/CMakeLists.txt index aa4b761..e69e6d5 100644 --- a/examples/mainwindow/CMakeLists.txt +++ b/examples/mainwindow/CMakeLists.txt @@ -33,7 +33,7 @@ target_compile_definitions(MainWindow PRIVATE QT_NO_CAST_TO_ASCII QT_NO_KEYWORDS QT_DEPRECATED_WARNINGS - QT_DISABLE_DEPRECATED_BEFORE=0x060000 + QT_DISABLE_DEPRECATED_BEFORE=0x060100 ) if(MSVC) diff --git a/examples/mainwindow/mainwindow.cpp b/examples/mainwindow/mainwindow.cpp index 92b7ac3..c2eeca3 100644 --- a/examples/mainwindow/mainwindow.cpp +++ b/examples/mainwindow/mainwindow.cpp @@ -56,7 +56,6 @@ MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(par titleBarWidget->maximizeButton->setChecked(isMaximized()); titleBarWidget->maximizeButton->setToolTip(isMaximized() ? tr("Restore") : tr("Maximize")); }); - //connect(titleBarWidget->iconButton, &QPushButton::clicked, this, &MainWindow::displaySystemMenu); } MainWindow::~MainWindow() @@ -84,8 +83,8 @@ void MainWindow::showEvent(QShowEvent *event) FramelessWindowsManager::setHitTestVisibleInChrome(win, titleBarWidget->maximizeButton, true); FramelessWindowsManager::setHitTestVisibleInChrome(win, titleBarWidget->closeButton, true); FramelessWindowsManager::setHitTestVisibleInChrome(win, appMainWindow->menubar, true); - inited = true; setContentsMargins(1, 1, 1, 1); + inited = true; } } } @@ -129,7 +128,7 @@ void MainWindow::paintEvent(QPaintEvent *event) {0, h, 0, 0} }; painter.save(); - painter.setPen({Utilities::getNativeWindowFrameColor(), 1}); + painter.setPen({Utilities::getNativeWindowFrameColor(isActiveWindow()), 1}); painter.drawLines(lines); painter.restore(); } diff --git a/examples/quick/CMakeLists.txt b/examples/quick/CMakeLists.txt index 657d2c7..24795eb 100644 --- a/examples/quick/CMakeLists.txt +++ b/examples/quick/CMakeLists.txt @@ -31,7 +31,7 @@ target_compile_definitions(Quick PRIVATE QT_NO_CAST_TO_ASCII QT_NO_KEYWORDS QT_DEPRECATED_WARNINGS - QT_DISABLE_DEPRECATED_BEFORE=0x060000 + QT_DISABLE_DEPRECATED_BEFORE=0x060100 ) if(MSVC) diff --git a/examples/quick/qml/CloseButton.qml b/examples/quick/qml/CloseButton.qml index 632c384..3e088cf 100644 --- a/examples/quick/qml/CloseButton.qml +++ b/examples/quick/qml/CloseButton.qml @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (C) 2020 by wangwenx190 (Yuhang Zhao) + * Copyright (C) 2021 by wangwenx190 (Yuhang Zhao) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/examples/quick/qml/MaximizeButton.qml b/examples/quick/qml/MaximizeButton.qml index bf1e730..17a1953 100644 --- a/examples/quick/qml/MaximizeButton.qml +++ b/examples/quick/qml/MaximizeButton.qml @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (C) 2020 by wangwenx190 (Yuhang Zhao) + * Copyright (C) 2021 by wangwenx190 (Yuhang Zhao) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/examples/quick/qml/MinimizeButton.qml b/examples/quick/qml/MinimizeButton.qml index 15bd71c..fe70bc1 100644 --- a/examples/quick/qml/MinimizeButton.qml +++ b/examples/quick/qml/MinimizeButton.qml @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (C) 2020 by wangwenx190 (Yuhang Zhao) + * Copyright (C) 2021 by wangwenx190 (Yuhang Zhao) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/examples/widget/CMakeLists.txt b/examples/widget/CMakeLists.txt index dcdf278..c5489fe 100644 --- a/examples/widget/CMakeLists.txt +++ b/examples/widget/CMakeLists.txt @@ -31,7 +31,7 @@ target_compile_definitions(Widget PRIVATE QT_NO_CAST_TO_ASCII QT_NO_KEYWORDS QT_DEPRECATED_WARNINGS - QT_DISABLE_DEPRECATED_BEFORE=0x060000 + QT_DISABLE_DEPRECATED_BEFORE=0x060100 ) if(MSVC) @@ -44,4 +44,4 @@ endif() if(WIN32) target_link_libraries(Widget PRIVATE user32 shell32 gdi32 dwmapi) -endif() \ No newline at end of file +endif() diff --git a/examples/widget/widget.cpp b/examples/widget/widget.cpp index b7fdd18..10188d1 100644 --- a/examples/widget/widget.cpp +++ b/examples/widget/widget.cpp @@ -23,7 +23,7 @@ */ #include "widget.h" -#include +#include #include #include #include @@ -51,6 +51,7 @@ void Widget::showEvent(QShowEvent *event) FramelessWindowsManager::setHitTestVisibleInChrome(win, m_minimizeButton, true); FramelessWindowsManager::setHitTestVisibleInChrome(win, m_maximizeButton, true); FramelessWindowsManager::setHitTestVisibleInChrome(win, m_closeButton, true); + setContentsMargins(1, 1, 1, 1); inited = true; } } @@ -67,10 +68,10 @@ void Widget::changeEvent(QEvent *event) bool shouldUpdate = false; if (event->type() == QEvent::WindowStateChange) { if (isMaximized() || isFullScreen()) { - layout()->setContentsMargins(0, 0, 0, 0); + setContentsMargins(0, 0, 0, 0); m_maximizeButton->setIcon(QIcon{QStringLiteral(":/images/button_restore_black.svg")}); } else if (!isMinimized()) { - layout()->setContentsMargins(1, 1, 1, 1); + setContentsMargins(1, 1, 1, 1); m_maximizeButton->setIcon(QIcon{QStringLiteral(":/images/button_maximize_black.svg")}); } shouldUpdate = true; @@ -101,7 +102,7 @@ void Widget::paintEvent(QPaintEvent *event) {0, h, 0, 0} }; painter.save(); - painter.setPen({Utilities::getNativeWindowFrameColor(), 1}); + painter.setPen({Utilities::getNativeWindowFrameColor(isActiveWindow()), 1}); painter.drawLines(lines); painter.restore(); } @@ -156,7 +157,7 @@ void Widget::setupUi() contentLayout->addWidget(m_label); contentLayout->addStretch(); const auto mainLayout = new QVBoxLayout(this); - mainLayout->setContentsMargins(1, 1, 1, 1); + mainLayout->setContentsMargins(0, 0, 0, 0); mainLayout->setSpacing(0); mainLayout->addLayout(systemButtonLayout); mainLayout->addStretch(); diff --git a/examples/windows.rc b/examples/windows.rc index bc82847..0eadf43 100644 --- a/examples/windows.rc +++ b/examples/windows.rc @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (C) 2020 by wangwenx190 (Yuhang Zhao) + * Copyright (C) 2021 by wangwenx190 (Yuhang Zhao) * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal diff --git a/framelesshelper_win32.cpp b/framelesshelper_win32.cpp index 2239d56..15eb181 100644 --- a/framelesshelper_win32.cpp +++ b/framelesshelper_win32.cpp @@ -386,10 +386,16 @@ bool FramelessHelperWin::nativeEventFilter(const QByteArray &eventType, void *me #if 0 // Fix the flickering issue while resizing. // "clientRect->right += 1;" also works. - // The only draw back of this small trick is it will affect - // Qt's coordinate system. It makes the "canvas" of the window - // larger than it should be. Be careful if you need to paint - // something manually either through QPainter or Qt Quick. + // This small technique is known to have two draw backs: + // (1) Qt's coordinate system will be confused because the canvas size + // doesn't match the client area size so you will get some warnings + // from Qt and you should also be careful when you try to draw something + // manually through QPainter or in Qt Quick, be aware of the coordinate + // mismatch issue when you calculate position yourself. + // (2) Qt's window system will take some wrong actions when the window + // is being resized. For example, the window size will become 1px smaller + // or bigger everytime when resize() is called because the client area size + // is not correct. It confuses QPA's internal logic. clientRect->bottom += 1; #endif // If the window bounds change, we're going to relayout and repaint diff --git a/utilities_win32.cpp b/utilities_win32.cpp index 495e289..aae902c 100644 --- a/utilities_win32.cpp +++ b/utilities_win32.cpp @@ -79,19 +79,21 @@ using PROCESS_DPI_AWARENESS = enum _PROCESS_DPI_AWARENESS PROCESS_PER_MONITOR_DPI_AWARE = 2 }; -using ShouldAppsUseDarkModePtr = BOOL(WINAPI *)(); -using ShouldSystemUseDarkModePtr = BOOL(WINAPI *)(); - -using GetDpiForMonitorPtr = HRESULT(WINAPI *)(HMONITOR, MONITOR_DPI_TYPE, UINT *, UINT *); -using GetProcessDpiAwarenessPtr = HRESULT(WINAPI *)(HANDLE, PROCESS_DPI_AWARENESS *); -using GetSystemDpiForProcessPtr = UINT(WINAPI *)(HANDLE); -using GetDpiForWindowPtr = UINT(WINAPI *)(HWND); -using GetDpiForSystemPtr = UINT(WINAPI *)(); -using GetSystemMetricsForDpiPtr = int(WINAPI *)(int, UINT); -using AdjustWindowRectExForDpiPtr = BOOL(WINAPI *)(LPRECT, DWORD, BOOL, DWORD, UINT); - -using Win32Data = struct _FLH_UTILITIES_WIN32_DATA +struct Win32Data { + using ShouldAppsUseDarkModePtr = BOOL(WINAPI *)(); + using ShouldSystemUseDarkModePtr = BOOL(WINAPI *)(); + + using GetDpiForMonitorPtr = HRESULT(WINAPI *)(HMONITOR, MONITOR_DPI_TYPE, UINT *, UINT *); + using GetProcessDpiAwarenessPtr = HRESULT(WINAPI *)(HANDLE, PROCESS_DPI_AWARENESS *); + using GetSystemDpiForProcessPtr = UINT(WINAPI *)(HANDLE); + using GetDpiForWindowPtr = UINT(WINAPI *)(HWND); + using GetDpiForSystemPtr = UINT(WINAPI *)(); + using GetSystemMetricsForDpiPtr = int(WINAPI *)(int, UINT); + using AdjustWindowRectExForDpiPtr = BOOL(WINAPI *)(LPRECT, DWORD, BOOL, DWORD, UINT); + + + ShouldAppsUseDarkModePtr ShouldAppsUseDarkModePFN = nullptr; ShouldSystemUseDarkModePtr ShouldSystemUseDarkModePFN = nullptr; @@ -103,7 +105,7 @@ using Win32Data = struct _FLH_UTILITIES_WIN32_DATA GetSystemMetricsForDpiPtr GetSystemMetricsForDpiPFN = nullptr; AdjustWindowRectExForDpiPtr AdjustWindowRectExForDpiPFN = nullptr; - _FLH_UTILITIES_WIN32_DATA() + explicit Win32Data() { load(); }