From 6a12cb1a30a4bbec53c3bf7996222fa6e6fc960b Mon Sep 17 00:00:00 2001 From: Yuhang Zhao <2546789017@qq.com> Date: Wed, 21 Oct 2020 21:21:56 +0800 Subject: [PATCH] Minor improvements. Signed-off-by: Yuhang Zhao <2546789017@qq.com> --- examples/Win32Demo/widget.cpp | 91 +++++++++++++++++++++++------------ examples/Win32Demo/widget.h | 4 +- examples/Win32Demo/widget.ui | 13 +---- winnativeeventfilter.cpp | 11 +++-- 4 files changed, 71 insertions(+), 48 deletions(-) diff --git a/examples/Win32Demo/widget.cpp b/examples/Win32Demo/widget.cpp index 100b99c..b02ffbd 100644 --- a/examples/Win32Demo/widget.cpp +++ b/examples/Win32Demo/widget.cpp @@ -50,6 +50,7 @@ namespace { const char useNativeTitleBar[] = "WNEF_USE_NATIVE_TITLE_BAR"; const char preserveWindowFrame[] = "WNEF_FORCE_PRESERVE_WINDOW_FRAME"; const char forceUseAcrylicEffect[] = "WNEF_FORCE_ACRYLIC_ON_WIN10"; +const char dontExtendFrame[] = "WNEF_DO_NOT_EXTEND_FRAME"; const QLatin1String systemButtonsStyleSheet(R"( #iconButton, #minimizeButton, #maximizeButton, #closeButton { @@ -89,7 +90,7 @@ void updateWindow(QWidget *widget) Q_ASSERT(widget); if (widget->isTopLevel()) { void *handle = getRawHandle(widget); - //WinNativeEventFilter::updateFrameMargins(handle); + WinNativeEventFilter::updateFrameMargins(handle); WinNativeEventFilter::updateWindow(handle, true, true); widget->update(); } @@ -100,7 +101,7 @@ bool isWin10OrGreater() return QOperatingSystemVersion::current() >= QOperatingSystemVersion::Windows10; } -bool isGreaterThanWin10_1803() +bool isWin101803OrGreater() { return QOperatingSystemVersion::current() >= QOperatingSystemVersion(QOperatingSystemVersion::Windows, 10, 0, 17134); @@ -147,17 +148,14 @@ QColor getThemeColor() Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) { m_bIsWin10OrGreater = isWin10OrGreater(); - + m_bIsWin101803OrGreater = isWin101803OrGreater(); m_cThemeColor = getThemeColor(); ui->setupUi(this); - - ui->forceAcrylicCB->setEnabled(isGreaterThanWin10_1803()); - + ui->forceAcrylicCB->setEnabled(m_bIsWin101803OrGreater); if (shouldDrawBorder()) { layout()->setContentsMargins(1, 1, 1, 1); } - updateTitleBar(); connect(ui->iconButton, &QPushButton::clicked, this, [this]() { @@ -166,7 +164,6 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) const auto hwnd = reinterpret_cast(getRawHandle(this)); SendMessageW(hwnd, WM_CONTEXTMENU, reinterpret_cast(hwnd), MAKELPARAM(pos.x, pos.y)); }); - connect(ui->minimizeButton, &QPushButton::clicked, this, &Widget::showMinimized); connect(ui->maximizeButton, &QPushButton::clicked, this, [this]() { if (isMaximized()) { @@ -176,17 +173,14 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) } }); connect(ui->closeButton, &QPushButton::clicked, this, &Widget::close); - connect(ui->moveCenterButton, &QPushButton::clicked, this, [this]() { WinNativeEventFilter::moveWindowToDesktopCenter(getRawHandle(this)); }); - connect(this, &Widget::windowTitleChanged, ui->titleLabel, &QLabel::setText); connect(this, &Widget::windowIconChanged, ui->iconButton, &QPushButton::setIcon); - connect(ui->customizeTitleBarCB, &QCheckBox::stateChanged, this, [this](int state) { const bool enable = state == Qt::Checked; - ui->removeWindowFrameCB->setEnabled(enable); + ui->preserveWindowFrameCB->setEnabled(enable); WinNativeEventFilter::updateQtFrame(windowHandle(), enable ? WinNativeEventFilter::getSystemMetric( getRawHandle(this), @@ -200,25 +194,28 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) } updateWindow(this); }); - connect(ui->removeWindowFrameCB, &QCheckBox::stateChanged, this, [this](int state) { + connect(ui->preserveWindowFrameCB, &QCheckBox::stateChanged, this, [this](int state) { const bool enable = state == Qt::Checked; if (enable) { - qunsetenv(preserveWindowFrame); - } else { qputenv(preserveWindowFrame, "1"); + qputenv(dontExtendFrame, "1"); + } else { + qunsetenv(preserveWindowFrame); + qunsetenv(dontExtendFrame); } - if (enable && shouldDrawBorder()) { + if (!enable && shouldDrawBorder()) { layout()->setContentsMargins(1, 1, 1, 1); } else { layout()->setContentsMargins(0, 0, 0, 0); } + updateTitleBar(); updateWindow(this); }); connect(ui->blurEffectCB, &QCheckBox::stateChanged, this, [this](int state) { const bool enable = state == Qt::Checked; QColor color = Qt::white; - if (isGreaterThanWin10_1803() && ui->forceAcrylicCB->isChecked()) { - if (enable) { + if (m_bIsWin101803OrGreater && ui->forceAcrylicCB->isChecked()) { + if (enable && m_bShowColorDialog) { color = QColorDialog::getColor(color, this, tr("Please select a gradient color"), @@ -233,6 +230,9 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) updateTitleBar(); }); connect(ui->forceAcrylicCB, &QCheckBox::stateChanged, this, [this](int state) { + if (!m_bIsWin101803OrGreater) { + return; + } if (state == Qt::Checked) { qputenv(forceUseAcrylicEffect, "1"); } else { @@ -255,6 +255,8 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget) WinNativeEventFilter::addFramelessWindow(this, &data); installEventFilter(this); + + initWindow(); } Widget::~Widget() @@ -270,7 +272,7 @@ bool Widget::isNormaled() const bool Widget::shouldDrawBorder(const bool ignoreWindowState) const { return m_bIsWin10OrGreater && (ignoreWindowState ? true : isNormaled()) - && ui->removeWindowFrameCB->isChecked() && ui->customizeTitleBarCB->isChecked(); + && !ui->preserveWindowFrameCB->isChecked() && ui->customizeTitleBarCB->isChecked(); } bool Widget::shouldDrawThemedBorder(const bool ignoreWindowState) const @@ -405,28 +407,53 @@ void Widget::updateTitleBar() ui->maximizeButton->setIcon(QIcon(QLatin1String(":/images/button_maximize_black.svg"))); } } - const QColor titleBarColor = m_bExtendToTitleBar ? Qt::transparent - : (themedTitleBar ? m_cThemeColor : Qt::white); - const QColor titleTextColor = isActiveWindow() - ? ((!themedTitleBar || m_bExtendToTitleBar) ? Qt::black - : Qt::white) - : QColor("#999999"); + const QColor titleBarBackgroundColor = m_bExtendToTitleBar + ? Qt::transparent + : (themedTitleBar ? m_cThemeColor : Qt::white); + const QColor titleBarTextColor = isActiveWindow() + ? ((!themedTitleBar || m_bExtendToTitleBar) ? Qt::black + : Qt::white) + : QColor("#999999"); + const QColor titleBarBorderColor = (!m_bIsWin10OrGreater || shouldDrawBorder() || isMaximized() + || isFullScreen()) + ? Qt::transparent + : borderColor(); ui->titleBarWidget->setStyleSheet(systemButtonsStyleSheet + QLatin1String(R"( #titleLabel { color: rgb(%1, %2, %3); } )") - .arg(QString::number(titleTextColor.red()), - QString::number(titleTextColor.green()), - QString::number(titleTextColor.blue())) + .arg(QString::number(titleBarTextColor.red()), + QString::number(titleBarTextColor.green()), + QString::number(titleBarTextColor.blue())) + QLatin1String(R"( #titleBarWidget { background-color: rgba(%1, %2, %3, %4); + border-top: 1px solid rgba(%5, %6, %7, %8); } )") - .arg(QString::number(titleBarColor.red()), - QString::number(titleBarColor.green()), - QString::number(titleBarColor.blue()), - QString::number(titleBarColor.alpha()))); + .arg(QString::number(titleBarBackgroundColor.red()), + QString::number(titleBarBackgroundColor.green()), + QString::number(titleBarBackgroundColor.blue()), + QString::number(titleBarBackgroundColor.alpha()), + QString::number(titleBarBorderColor.red()), + QString::number(titleBarBorderColor.green()), + QString::number(titleBarBorderColor.blue()), + QString::number(titleBarBorderColor.alpha()))); +} + +void Widget::initWindow() +{ + if (m_bIsWin10OrGreater) { + ui->preserveWindowFrameCB->click(); + if (m_bIsWin101803OrGreater) { + ui->forceAcrylicCB->click(); + } + } + ui->customizeTitleBarCB->click(); + ui->extendToTitleBarCB->click(); + ui->blurEffectCB->click(); + ui->resizableCB->click(); + m_bShowColorDialog = true; } diff --git a/examples/Win32Demo/widget.h b/examples/Win32Demo/widget.h index 5256ecb..9eec8d1 100644 --- a/examples/Win32Demo/widget.h +++ b/examples/Win32Demo/widget.h @@ -71,10 +71,12 @@ protected: private: void updateTitleBar(); + void initWindow(); private: Ui::Widget *ui = nullptr; - bool m_bIsWin10OrGreater = false, m_bExtendToTitleBar = false; + bool m_bIsWin10OrGreater = false, m_bIsWin101803OrGreater = false, m_bExtendToTitleBar = false, + m_bShowColorDialog = false; const QColor m_cDefaultActiveBorderColor = {"#707070"} /*Qt::darkGray*/; const QColor m_cDefaultInactiveBorderColor = {"#aaaaaa"} /*Qt::gray*/; QColor m_cThemeColor = Qt::white; diff --git a/examples/Win32Demo/widget.ui b/examples/Win32Demo/widget.ui index d0c6b96..95b22b7 100644 --- a/examples/Win32Demo/widget.ui +++ b/examples/Win32Demo/widget.ui @@ -280,13 +280,10 @@ Enable customized title bar - - true - - + 15 @@ -295,10 +292,7 @@ - Remove window frame - - - true + Preserve window frame @@ -356,9 +350,6 @@ Resizable - - true - diff --git a/winnativeeventfilter.cpp b/winnativeeventfilter.cpp index c3f9413..7dd35bb 100644 --- a/winnativeeventfilter.cpp +++ b/winnativeeventfilter.cpp @@ -662,6 +662,7 @@ const char envVarUseNativeTitleBar[] = "WNEF_USE_NATIVE_TITLE_BAR"; const char envVarPreserveWindowFrame[] = "WNEF_PRESERVE_WINDOW_FRAME"; const char envVarForceWindowFrame[] = "WNEF_FORCE_PRESERVE_WINDOW_FRAME"; const char envVarForceAcrylic[] = "WNEF_FORCE_ACRYLIC_ON_WIN10"; +const char envVarNoExtendFrame[] = "WNEF_DO_NOT_EXTEND_FRAME"; bool shouldUseNativeTitleBar() { @@ -699,6 +700,11 @@ bool forceEnableAcrylicOnWin10() return qEnvironmentVariableIsSet(envVarForceAcrylic); } +bool dontExtendFrame() +{ + return qEnvironmentVariableIsSet(envVarNoExtendFrame); +} + BOOL IsDwmCompositionEnabled() { // Since Win8, DWM composition is always enabled and can't be disabled. @@ -999,7 +1005,7 @@ void UpdateFrameMarginsForWindow(const HWND handle) } else { margins.cyTopHeight = 1; } - if (shouldUseNativeTitleBar()) { + if (shouldUseNativeTitleBar() || dontExtendFrame()) { // If we are going to use the native title bar, // we should use the original window frame as well. margins = {0, 0, 0, 0}; @@ -1621,9 +1627,6 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType, // area. *result = 0; } - if (shouldHaveWindowFrame()) { - *result = 0; - } /* // It does solve the flickering issue indeed, however, it also // causes a lot of new issues when we are trying to draw