Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
Yuhang Zhao 2020-10-16 20:02:38 +08:00
parent 0debbb890f
commit ff10ae21f4
3 changed files with 36 additions and 4 deletions

View File

@ -73,6 +73,7 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
connect(ui->customizeTitleBarCB, &QCheckBox::stateChanged, this, [this](int state) {
const bool enable = state == Qt::Checked;
ui->windowFrameCB->setEnabled(enable);
WinNativeEventFilter::updateQtFrame(windowHandle(),
enable ? WinNativeEventFilter::getSystemMetric(
getRawHandle(this),
@ -81,7 +82,7 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
ui->titleBarWidget->setVisible(enable);
if (enable) {
qunsetenv(useNativeTitleBar);
} else if (state == Qt::Unchecked) {
} else {
qputenv(useNativeTitleBar, "1");
}
updateWindow(this);
@ -89,7 +90,7 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
connect(ui->windowFrameCB, &QCheckBox::stateChanged, this, [this](int state) {
if (state == Qt::Checked) {
qunsetenv(preserveWindowFrame);
} else if (state == Qt::Unchecked) {
} else {
qputenv(preserveWindowFrame, "1");
}
updateWindow(this);
@ -98,17 +99,28 @@ Widget::Widget(QWidget *parent) : QWidget(parent), ui(new Ui::Widget)
const bool enable = state == Qt::Checked;
QPalette palette = {};
// Choose the gradient color you like.
palette.setColor(QPalette::Window, QColor(255, 255, 255, 235));
palette.setColor(QPalette::Window, QColor(255, 255, 255, 220));
setPalette(enable ? palette : QPalette());
WinNativeEventFilter::setBlurEffectEnabled(getRawHandle(this), enable);
updateWindow(this);
});
connect(ui->resizableCB, &QCheckBox::stateChanged, this, [this](int state) {
const bool enable = state == Qt::Checked;
ui->maximizeButton->setEnabled(enable);
const auto data = WinNativeEventFilter::windowData(this);
if (data) {
data->fixedSize = state == Qt::Unchecked;
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);
}
});
QStyleOption option;

View File

@ -2397,3 +2397,20 @@ void WinNativeEventFilter::updateFrameMargins(void *handle)
UpdateFrameMarginsForWindow(hwnd);
}
}
void WinNativeEventFilter::setWindowResizable(void *handle, const bool resizable)
{
Q_ASSERT(handle);
const auto hwnd = reinterpret_cast<HWND>(handle);
if (WNEF_EXECUTE_WINAPI_RETURN(IsWindow, FALSE, hwnd)) {
const auto originalStyle = WNEF_EXECUTE_WINAPI_RETURN(GetWindowLongPtrW, 0, hwnd, GWL_STYLE);
const auto diffStyle = WS_MAXIMIZEBOX | WS_THICKFRAME;
const auto resizableStyle = originalStyle | diffStyle;
const auto fixedSizeStyle = originalStyle & ~diffStyle;
WNEF_EXECUTE_WINAPI(SetWindowLongPtrW,
hwnd,
GWL_STYLE,
resizable ? resizableStyle : fixedSizeStyle)
updateWindow(hwnd, true, false);
}
}

View File

@ -146,6 +146,9 @@ public:
// Thin wrapper of DwmExtendFrameIntoClientArea().
static void updateFrameMargins(void *handle /* HWND */);
// Helper function.
static void setWindowResizable(void *handle /* HWND */, const bool resizable = true);
///////////////////////////////////////////////
/// CORE FUNCTION - THE SOUL OF THIS CODE
///////////////////////////////////////////////