Minor tweaks.

Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-10-17 13:08:47 +08:00
parent 5296fb6f90
commit 073ad117ba
4 changed files with 42 additions and 42 deletions

View File

@ -107,20 +107,7 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
connect(ui->resizableCB, &QCheckBox::stateChanged, this, [this](int state) { connect(ui->resizableCB, &QCheckBox::stateChanged, this, [this](int state) {
const bool enable = state == Qt::Checked; const bool enable = state == Qt::Checked;
ui->maximizeButton->setEnabled(enable); ui->maximizeButton->setEnabled(enable);
const auto data = WinNativeEventFilter::windowData(this);
if (data) {
data->fixedSize = !enable;
updateWindow(this);
}
if (!ui->customizeTitleBarCB->isChecked()) {
if (enable) {
setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint);
} else {
setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);
}
show();
WinNativeEventFilter::setWindowResizable(getRawHandle(this), enable); WinNativeEventFilter::setWindowResizable(getRawHandle(this), enable);
}
}); });
QStyleOption option; QStyleOption option;

View File

@ -327,7 +327,7 @@ bool FramelessWindowsManager::getResizable(QObject *window)
Q_ASSERT(window); Q_ASSERT(window);
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
const auto data = WinNativeEventFilter::windowData(window); const auto data = WinNativeEventFilter::windowData(window);
return data ? !data->fixedSize : false; return data ? !data->fixedSize : true;
#else #else
return coreData()->framelessHelper.getResizable(window); return coreData()->framelessHelper.getResizable(window);
#endif #endif
@ -337,10 +337,7 @@ void FramelessWindowsManager::setResizable(QObject *window, const bool value)
{ {
Q_ASSERT(window); Q_ASSERT(window);
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
const auto data = WinNativeEventFilter::windowData(window); WinNativeEventFilter::setWindowResizable(getRawHandleFromWindow(window), value);
if (data) {
data->fixedSize = !value;
}
#else #else
coreData()->framelessHelper.setResizable(window, value); coreData()->framelessHelper.setResizable(window, value);
#endif #endif
@ -441,7 +438,7 @@ bool FramelessWindowsManager::getTitleBarEnabled(QObject *window)
Q_ASSERT(window); Q_ASSERT(window);
#ifdef Q_OS_WINDOWS #ifdef Q_OS_WINDOWS
const auto data = WinNativeEventFilter::windowData(window); const auto data = WinNativeEventFilter::windowData(window);
return data ? !data->disableTitleBar : false; return data ? !data->disableTitleBar : true;
#else #else
return coreData()->framelessHelper.getTitleBarEnabled(window); return coreData()->framelessHelper.getTitleBarEnabled(window);
#endif #endif

View File

@ -658,8 +658,18 @@ const UINT m_defaultDotsPerInch = USER_DEFAULT_SCREEN_DPI;
const qreal m_defaultDevicePixelRatio = 1.0; const qreal m_defaultDevicePixelRatio = 1.0;
bool shouldUseNativeTitleBar()
{
return qEnvironmentVariableIsSet("WNEF_USE_NATIVE_TITLE_BAR");
}
bool shouldHaveWindowFrame() bool shouldHaveWindowFrame()
{ {
if (shouldUseNativeTitleBar()) {
// We have to use the original window frame unconditionally if we
// want to use the native title bar.
return true;
}
const bool should = qEnvironmentVariableIsSet("WNEF_PRESERVE_WINDOW_FRAME"); const bool should = qEnvironmentVariableIsSet("WNEF_PRESERVE_WINDOW_FRAME");
const bool force = qEnvironmentVariableIsSet("WNEF_FORCE_PRESERVE_WINDOW_FRAME"); const bool force = qEnvironmentVariableIsSet("WNEF_FORCE_PRESERVE_WINDOW_FRAME");
if (should || force) { if (should || force) {
@ -679,11 +689,6 @@ bool shouldHaveWindowFrame()
return false; return false;
} }
bool shouldUseNativeTitleBar()
{
return qEnvironmentVariableIsSet("WNEF_USE_NATIVE_TITLE_BAR");
}
BOOL IsDwmCompositionEnabled() BOOL IsDwmCompositionEnabled()
{ {
// Since Win8, DWM composition is always enabled and can't be disabled. // Since Win8, DWM composition is always enabled and can't be disabled.
@ -1162,7 +1167,7 @@ void install()
} }
} }
void uninstall() [[maybe_unused]] void uninstall()
{ {
if (!coreData()->m_instance.isNull()) { if (!coreData()->m_instance.isNull()) {
qApp->removeNativeEventFilter(coreData()->m_instance.data()); qApp->removeNativeEventFilter(coreData()->m_instance.data());
@ -1603,8 +1608,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
if (shouldHaveWindowFrame()) { if (shouldHaveWindowFrame()) {
*result = 0; *result = 0;
} }
if (!shouldHaveWindowFrame() && !IsFullScreen(msg->hwnd) && !IsMaximized(msg->hwnd) if (!shouldHaveWindowFrame() && !IsFullScreen(msg->hwnd) && !IsMaximized(msg->hwnd)) {
&& !IsMinimized(msg->hwnd)) {
// Fix the flickering problem when resizing. // Fix the flickering problem when resizing.
// Don't modify the left, right or bottom edge because // Don't modify the left, right or bottom edge because
// a border line will be seen (at least on Win10). // a border line will be seen (at least on Win10).
@ -1617,7 +1621,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
// area. // area.
case WM_NCUAHDRAWCAPTION: case WM_NCUAHDRAWCAPTION:
case WM_NCUAHDRAWFRAME: { case WM_NCUAHDRAWFRAME: {
if (shouldHaveWindowFrame() || shouldUseNativeTitleBar()) { if (shouldHaveWindowFrame()) {
break; break;
} else { } else {
*result = 0; *result = 0;
@ -1627,8 +1631,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
case WM_NCPAINT: { case WM_NCPAINT: {
// 边框阴影处于非客户区的范围,因此如果直接阻止非客户区的绘制,会导致边框阴影丢失 // 边框阴影处于非客户区的范围,因此如果直接阻止非客户区的绘制,会导致边框阴影丢失
if (!IsDwmCompositionEnabled() && !shouldHaveWindowFrame() if (!IsDwmCompositionEnabled() && !shouldHaveWindowFrame()) {
&& !shouldUseNativeTitleBar()) {
// Only block WM_NCPAINT when DWM composition is disabled. If // Only block WM_NCPAINT when DWM composition is disabled. If
// it's blocked when DWM composition is enabled, the frame // it's blocked when DWM composition is enabled, the frame
// shadow won't be drawn. // shadow won't be drawn.
@ -1639,7 +1642,7 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
} }
} }
case WM_NCACTIVATE: { case WM_NCACTIVATE: {
if (shouldHaveWindowFrame() || shouldUseNativeTitleBar()) { if (shouldHaveWindowFrame()) {
break; break;
} else { } else {
if (IsDwmCompositionEnabled()) { if (IsDwmCompositionEnabled()) {
@ -2316,6 +2319,13 @@ bool WinNativeEventFilter::displaySystemMenu(void *handle,
WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_MAXIMIZE, FALSE, &mii) WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_MAXIMIZE, FALSE, &mii)
WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_MINIMIZE, FALSE, &mii) WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_MINIMIZE, FALSE, &mii)
mii.fState = MF_GRAYED; mii.fState = MF_GRAYED;
const auto data = windowData(hwnd);
const bool fixedSize = data ? data->fixedSize : false;
if (fixedSize) {
WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_SIZE, FALSE, &mii)
WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_MAXIMIZE, FALSE, &mii)
WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_RESTORE, FALSE, &mii)
} else {
if (IsFullScreen(hwnd) || IsMaximized(hwnd)) { if (IsFullScreen(hwnd) || IsMaximized(hwnd)) {
WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_SIZE, FALSE, &mii) WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_SIZE, FALSE, &mii)
WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_MOVE, FALSE, &mii) WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_MOVE, FALSE, &mii)
@ -2325,6 +2335,7 @@ bool WinNativeEventFilter::displaySystemMenu(void *handle,
} else { } else {
WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_RESTORE, FALSE, &mii) WNEF_EXECUTE_WINAPI(SetMenuItemInfoW, hMenu, SC_RESTORE, FALSE, &mii)
} }
}
const LPARAM cmd = WNEF_EXECUTE_WINAPI_RETURN(TrackPopupMenu, const LPARAM cmd = WNEF_EXECUTE_WINAPI_RETURN(TrackPopupMenu,
0, 0,
hMenu, hMenu,
@ -2403,10 +2414,15 @@ void WinNativeEventFilter::setWindowResizable(void *handle, const bool resizable
Q_ASSERT(handle); Q_ASSERT(handle);
const auto hwnd = reinterpret_cast<HWND>(handle); const auto hwnd = reinterpret_cast<HWND>(handle);
if (WNEF_EXECUTE_WINAPI_RETURN(IsWindow, FALSE, hwnd)) { if (WNEF_EXECUTE_WINAPI_RETURN(IsWindow, FALSE, hwnd)) {
const auto data = windowData(hwnd);
if (data) {
data->fixedSize = !resizable;
}
const auto originalStyle = WNEF_EXECUTE_WINAPI_RETURN(GetWindowLongPtrW, 0, hwnd, GWL_STYLE); const auto originalStyle = WNEF_EXECUTE_WINAPI_RETURN(GetWindowLongPtrW, 0, hwnd, GWL_STYLE);
const auto diffStyle = WS_MAXIMIZEBOX | WS_THICKFRAME; const auto keyResizeStyle = WS_MAXIMIZEBOX | WS_THICKFRAME;
const auto resizableStyle = originalStyle | diffStyle; const auto keyFixedStyle = WS_DLGFRAME;
const auto fixedSizeStyle = originalStyle & ~diffStyle; const auto resizableStyle = (originalStyle & ~keyFixedStyle) | keyResizeStyle | WS_CAPTION;
const auto fixedSizeStyle = (originalStyle & ~keyResizeStyle) | keyFixedStyle;
WNEF_EXECUTE_WINAPI(SetWindowLongPtrW, WNEF_EXECUTE_WINAPI(SetWindowLongPtrW,
hwnd, hwnd,
GWL_STYLE, GWL_STYLE,

View File

@ -146,8 +146,8 @@ public:
// Thin wrapper of DwmExtendFrameIntoClientArea(). // Thin wrapper of DwmExtendFrameIntoClientArea().
static void updateFrameMargins(void *handle /* HWND */); static void updateFrameMargins(void *handle /* HWND */);
// Helper function. Designed for normal windows only. // A resizable window can be resized and maximized, however, a fixed size
// Don't use it on frameless windows! // window can only be moved and minimized, it can't be resized and maximized.
static void setWindowResizable(void *handle /* HWND */, const bool resizable = true); static void setWindowResizable(void *handle /* HWND */, const bool resizable = true);
/////////////////////////////////////////////// ///////////////////////////////////////////////