Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2022-03-22 17:14:03 +08:00
parent 5ecc06df2f
commit d3157699ef
3 changed files with 20 additions and 9 deletions

View File

@ -26,6 +26,7 @@
#include "framelesshelpercore_global.h"
#include <QtGui/qwindowdefs.h>
#include <functional>
FRAMELESSHELPER_BEGIN_NAMESPACE
@ -67,6 +68,9 @@ enum class DwmColorizationArea : int
Q_ENUM_NS(DwmColorizationArea)
#endif
using GetWindowFlagsCallback = std::function<Qt::WindowFlags()>;
using SetWindowFlagsCallback = std::function<void(Qt::WindowFlags)>;
namespace Utils
{
@ -114,7 +118,8 @@ FRAMELESSHELPER_CORE_API void fixupQtInternals(const WId winId);
[[nodiscard]] FRAMELESSHELPER_CORE_API bool isFrameBorderColorized();
FRAMELESSHELPER_CORE_API void installSystemMenuHook(const QWindow *window);
FRAMELESSHELPER_CORE_API void uninstallSystemMenuHook(const WId winId);
FRAMELESSHELPER_CORE_API void tryToBeCompatibleWithQtFramelessWindowHint(QWindow *window, const bool enable);
FRAMELESSHELPER_CORE_API void tryToBeCompatibleWithQtFramelessWindowHint(const WId winId,
const GetWindowFlagsCallback &getWindowFlags, const SetWindowFlagsCallback &setWindowFlags, const bool enable);
#endif // Q_OS_WINDOWS
} // namespace Utils

View File

@ -1078,13 +1078,17 @@ void Utils::sendMouseReleaseEvent()
}
}
void Utils::tryToBeCompatibleWithQtFramelessWindowHint(QWindow *window, const bool enable)
void Utils::tryToBeCompatibleWithQtFramelessWindowHint(const WId winId,
const GetWindowFlagsCallback &getWindowFlags,
const SetWindowFlagsCallback &setWindowFlags,
const bool enable)
{
Q_ASSERT(window);
if (!window) {
Q_ASSERT(winId);
Q_ASSERT(getWindowFlags);
Q_ASSERT(setWindowFlags);
if (!winId || !getWindowFlags || !setWindowFlags) {
return;
}
const WId winId = window->winId();
const auto hwnd = reinterpret_cast<HWND>(winId);
SetLastError(ERROR_SUCCESS);
const LONG_PTR originalWindowStyle = GetWindowLongPtrW(hwnd, GWL_STYLE);
@ -1092,10 +1096,10 @@ void Utils::tryToBeCompatibleWithQtFramelessWindowHint(QWindow *window, const bo
qWarning() << getSystemErrorMessage(QStringLiteral("GetWindowLongPtrW"));
return;
}
const Qt::WindowFlags originalWindowFlags = window->flags();
const Qt::WindowFlags originalWindowFlags = getWindowFlags();
const Qt::WindowFlags newWindowFlags = (enable ? (originalWindowFlags | Qt::FramelessWindowHint)
: (originalWindowFlags & ~Qt::FramelessWindowHint));
window->setFlags(newWindowFlags);
setWindowFlags(newWindowFlags);
SetLastError(ERROR_SUCCESS);
if (SetWindowLongPtrW(hwnd, GWL_STYLE, originalWindowStyle) == 0) {
qWarning() << getSystemErrorMessage(QStringLiteral("SetWindowLongPtrW"));

View File

@ -309,8 +309,10 @@ void FramelessWidgetsHelper::initialize()
}
}
if (m_options & Option::BeCompatibleWithQtFramelessWindowHint) {
Utils::tryToBeCompatibleWithQtFramelessWindowHint(window, true);
Q_ASSERT(window->flags() & Qt::FramelessWindowHint);
Utils::tryToBeCompatibleWithQtFramelessWindowHint(q->winId(),
[this]() -> Qt::WindowFlags { return q->windowFlags(); },
[this](Qt::WindowFlags flags) -> void { q->setWindowFlags(flags); },
true);
}
FramelessWindowsManager *manager = FramelessWindowsManager::instance();
manager->addWindow(window);