Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-03-16 10:12:12 +08:00
parent 7694a10e87
commit d6fe3fd8bd
8 changed files with 42 additions and 50 deletions

View File

@ -11,7 +11,7 @@
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>MainWindow</string> <string>Hello, World!</string>
</property> </property>
<property name="windowIcon"> <property name="windowIcon">
<iconset> <iconset>

View File

@ -130,7 +130,7 @@
</font> </font>
</property> </property>
<property name="text"> <property name="text">
<string>Window Title</string> <string>Hello, World!</string>
</property> </property>
<property name="alignment"> <property name="alignment">
<set>Qt::AlignCenter</set> <set>Qt::AlignCenter</set>

View File

@ -151,8 +151,6 @@ void MainWindow::mouseDoubleClickEvent(QMouseEvent *event)
void MainWindow::setupUi() void MainWindow::setupUi()
{ {
resize(800, 600);
appMainWindow = new Ui::MainWindow; appMainWindow = new Ui::MainWindow;
appMainWindow->setupUi(this); appMainWindow->setupUi(this);
@ -183,8 +181,6 @@ void MainWindow::setupUi()
titleBarWidget->maximizeButton->setChecked(check); titleBarWidget->maximizeButton->setChecked(check);
titleBarWidget->maximizeButton->setToolTip(check ? tr("Restore") : tr("Maximize")); titleBarWidget->maximizeButton->setToolTip(check ? tr("Restore") : tr("Maximize"));
}); });
setWindowTitle(tr("Hello, World!"));
} }
bool MainWindow::isInTitleBarDraggableArea(const QPoint &pos) const bool MainWindow::isInTitleBarDraggableArea(const QPoint &pos) const

View File

@ -75,12 +75,6 @@ static const QString mainStyleSheet = QStringLiteral(R"(#MainWidget {
} }
)"); )");
[[nodiscard]] static inline bool isTitleBarColorized()
{
const DwmColorizationArea area = Utilities::getDwmColorizationArea();
return ((area == DwmColorizationArea::TitleBar_WindowBorder) || (area == DwmColorizationArea::All));
}
Widget::Widget(QWidget *parent) : QWidget(parent) Widget::Widget(QWidget *parent) : QWidget(parent)
{ {
setAttribute(Qt::WA_DontCreateNativeAncestors); setAttribute(Qt::WA_DontCreateNativeAncestors);
@ -290,7 +284,7 @@ void Widget::updateStyleSheet()
{ {
const bool active = isActiveWindow(); const bool active = isActiveWindow();
const bool dark = Utilities::shouldAppsUseDarkMode(); const bool dark = Utilities::shouldAppsUseDarkMode();
const bool colorizedTitleBar = isTitleBarColorized(); const bool colorizedTitleBar = Utilities::isTitleBarColorized();
const QColor titleBarWidgetBackgroundColor = [active, colorizedTitleBar, dark]() -> QColor { const QColor titleBarWidgetBackgroundColor = [active, colorizedTitleBar, dark]() -> QColor {
if (active) { if (active) {
if (colorizedTitleBar) { if (colorizedTitleBar) {
@ -320,7 +314,7 @@ void Widget::updateStyleSheet()
void Widget::updateSystemButtonIcons() void Widget::updateSystemButtonIcons()
{ {
const QString prefix = ((Utilities::shouldAppsUseDarkMode() || isTitleBarColorized()) ? QStringLiteral("light") : QStringLiteral("dark")); const QString prefix = ((Utilities::shouldAppsUseDarkMode() || Utilities::isTitleBarColorized()) ? QStringLiteral("light") : QStringLiteral("dark"));
m_minimizeButton->setIcon(QIcon(QStringLiteral(":/images/%1/chrome-minimize.svg").arg(prefix))); m_minimizeButton->setIcon(QIcon(QStringLiteral(":/images/%1/chrome-minimize.svg").arg(prefix)));
if (isMaximized() || isFullScreen()) { if (isMaximized() || isFullScreen()) {
m_maximizeButton->setIcon(QIcon(QStringLiteral(":/images/%1/chrome-restore.svg").arg(prefix))); m_maximizeButton->setIcon(QIcon(QStringLiteral(":/images/%1/chrome-restore.svg").arg(prefix)));

View File

@ -71,7 +71,6 @@
#endif #endif
#include <windows.h> #include <windows.h>
#include <uxtheme.h>
#include <shellapi.h> #include <shellapi.h>
#include <dwmapi.h> #include <dwmapi.h>
#include <timeapi.h> #include <timeapi.h>
@ -101,6 +100,10 @@
# define SM_CXPADDEDBORDER (92) # define SM_CXPADDEDBORDER (92)
#endif #endif
#ifndef SM_CYPADDEDBORDER
# define SM_CYPADDEDBORDER SM_CXPADDEDBORDER
#endif
#ifndef ABM_GETAUTOHIDEBAREX #ifndef ABM_GETAUTOHIDEBAREX
# define ABM_GETAUTOHIDEBAREX (0x0000000b) # define ABM_GETAUTOHIDEBAREX (0x0000000b)
#endif #endif

View File

@ -135,11 +135,7 @@ QColor FramelessQuickUtils::systemAccentColor()
bool FramelessQuickUtils::titleBarColorVisible() bool FramelessQuickUtils::titleBarColorVisible()
{ {
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
if (!Utilities::isWin10OrGreater()) { return Utilities::isTitleBarColorized();
return false;
}
const DwmColorizationArea area = Utilities::getDwmColorizationArea();
return ((area == DwmColorizationArea::TitleBar_WindowBorder) || (area == DwmColorizationArea::All));
#else #else
return false; return false;
#endif #endif

View File

@ -78,6 +78,8 @@ FRAMELESSHELPER_API void showSystemMenu(const WId winId, const QPointF &pos);
FRAMELESSHELPER_API void updateWindowFrameBorderColor(const WId winId, const bool dark); FRAMELESSHELPER_API void updateWindowFrameBorderColor(const WId winId, const bool dark);
FRAMELESSHELPER_API void fixupQtInternals(const WId winId); FRAMELESSHELPER_API void fixupQtInternals(const WId winId);
[[nodiscard]] FRAMELESSHELPER_API bool isWindowFrameBorderVisible(); [[nodiscard]] FRAMELESSHELPER_API bool isWindowFrameBorderVisible();
[[nodiscard]] FRAMELESSHELPER_API bool isTitleBarColorized();
[[nodiscard]] FRAMELESSHELPER_API bool isFrameBorderColorized();
#endif // Q_OS_WINDOWS #endif // Q_OS_WINDOWS
} // namespace Utilities } // namespace Utilities

View File

@ -241,8 +241,8 @@ void Utilities::updateWindowFrameMargins(const WId winId, const bool reset)
if (!winId) { if (!winId) {
return; return;
} }
// DwmExtendFrameIntoClientArea() will always fail if DWM composition is disabled. // We can't extend the window frame when DWM composition is disabled.
// No need to try in this case. // No need to try further in this case.
if (!isDwmCompositionEnabled()) { if (!isDwmCompositionEnabled()) {
return; return;
} }
@ -252,12 +252,9 @@ void Utilities::updateWindowFrameMargins(const WId winId, const bool reset)
if (!pDwmExtendFrameIntoClientArea) { if (!pDwmExtendFrameIntoClientArea) {
return; return;
} }
const MARGINS margins = [reset, winId]() -> MARGINS { const MARGINS margins = [reset]() -> MARGINS {
if (reset) { if (reset || isWindowFrameBorderVisible()) {
return {0, 0, 0, 0}; return {0, 0, 0, 0};
}
if (isWindowFrameBorderVisible()) {
return {0, static_cast<int>(getTitleBarHeight(winId, true)), 0, 0};
} else { } else {
return {1, 1, 1, 1}; return {1, 1, 1, 1};
} }
@ -367,7 +364,7 @@ bool Utilities::shouldAppsUseDarkMode()
DwmColorizationArea Utilities::getDwmColorizationArea() DwmColorizationArea Utilities::getDwmColorizationArea()
{ {
// It's a Win10 only feature. // It's a Win10 only feature. (TO BE VERIFIED)
if (!isWin10OrGreater()) { if (!isWin10OrGreater()) {
return DwmColorizationArea::None; return DwmColorizationArea::None;
} }
@ -686,11 +683,12 @@ quint32 Utilities::getResizeBorderThickness(const WId winId, const bool horizont
if (!winId) { if (!winId) {
return 0; return 0;
} }
const int paddedBorderWidth = getSystemMetrics2(winId, SM_CXPADDEDBORDER, true, scaled);
if (horizontal) { if (horizontal) {
return (getSystemMetrics2(winId, SM_CXSIZEFRAME, true, scaled) + paddedBorderWidth); return (getSystemMetrics2(winId, SM_CXSIZEFRAME, true, scaled)
+ getSystemMetrics2(winId, SM_CXPADDEDBORDER, true, scaled));
} else { } else {
return (getSystemMetrics2(winId, SM_CYSIZEFRAME, false, scaled) + paddedBorderWidth); return (getSystemMetrics2(winId, SM_CYSIZEFRAME, false, scaled)
+ getSystemMetrics2(winId, SM_CYPADDEDBORDER, false, scaled));
} }
} }
@ -750,11 +748,10 @@ QColor Utilities::getFrameBorderColor(const bool active)
} }
const bool dark = shouldAppsUseDarkMode(); const bool dark = shouldAppsUseDarkMode();
if (active) { if (active) {
const DwmColorizationArea area = getDwmColorizationArea(); if (isFrameBorderColorized()) {
if ((area == DwmColorizationArea::TitleBar_WindowBorder) || (area == DwmColorizationArea::All)) {
return getDwmColorizationColor(); return getDwmColorizationColor();
} else { } else {
return (dark ? QColor(QStringLiteral("#4d4d4d")) : QColor(Qt::white)); return (dark ? QColor(QStringLiteral("#4d4d4d")) : QColor(Qt::black));
} }
} else { } else {
return (dark ? QColor(QStringLiteral("#575959")) : QColor(QStringLiteral("#a6a6a6"))); return (dark ? QColor(QStringLiteral("#575959")) : QColor(QStringLiteral("#a6a6a6")));
@ -771,29 +768,18 @@ void Utilities::updateWindowFrameBorderColor(const WId winId, const bool dark)
if (!isWin101809OrGreater()) { if (!isWin101809OrGreater()) {
return; return;
} }
static const auto pSetWindowTheme =
reinterpret_cast<decltype(&SetWindowTheme)>(
QSystemLibrary::resolve(QStringLiteral("uxtheme"), "SetWindowTheme"));
static const auto pDwmSetWindowAttribute = static const auto pDwmSetWindowAttribute =
reinterpret_cast<decltype(&DwmSetWindowAttribute)>( reinterpret_cast<decltype(&DwmSetWindowAttribute)>(
QSystemLibrary::resolve(QStringLiteral("dwmapi"), "DwmSetWindowAttribute")); QSystemLibrary::resolve(QStringLiteral("dwmapi"), "DwmSetWindowAttribute"));
if (!pSetWindowTheme || !pDwmSetWindowAttribute) { if (!pDwmSetWindowAttribute) {
return; return;
} }
const auto hwnd = reinterpret_cast<HWND>(winId); const auto hwnd = reinterpret_cast<HWND>(winId);
const BOOL value = (dark ? TRUE : FALSE); const BOOL value = (dark ? TRUE : FALSE);
HRESULT hr = pDwmSetWindowAttribute(hwnd, _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, &value, sizeof(value)); // Whether dark window frame is available or not depends on the runtime system version,
if (FAILED(hr)) { // it's totally OK if it's not available, so just ignore the errors.
// Just eat this error, because it only works on systems before Win10 20H1. pDwmSetWindowAttribute(hwnd, _DWMWA_USE_IMMERSIVE_DARK_MODE_BEFORE_20H1, &value, sizeof(value));
} pDwmSetWindowAttribute(hwnd, _DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));
hr = pDwmSetWindowAttribute(hwnd, _DWMWA_USE_IMMERSIVE_DARK_MODE, &value, sizeof(value));
if (FAILED(hr)) {
// Eat this error, it only works on systems greater than or equal to Win10 20H1.
}
hr = pSetWindowTheme(hwnd, (dark ? L"DarkMode_Explorer" : L" "), nullptr);
if (FAILED(hr)) {
qWarning() << __getSystemErrorMessage(QStringLiteral("SetWindowTheme"), hr);
}
} }
void Utilities::fixupQtInternals(const WId winId) void Utilities::fixupQtInternals(const WId winId)
@ -885,4 +871,19 @@ bool Utilities::isWindowFrameBorderVisible()
return result; return result;
} }
bool Utilities::isTitleBarColorized()
{
// CHECK: is it supported on win7?
if (!isWin10OrGreater()) {
return false;
}
const DwmColorizationArea area = getDwmColorizationArea();
return ((area == DwmColorizationArea::TitleBar_WindowBorder) || (area == DwmColorizationArea::All));
}
bool Utilities::isFrameBorderColorized()
{
return isTitleBarColorized();
}
FRAMELESSHELPER_END_NAMESPACE FRAMELESSHELPER_END_NAMESPACE