improved custom window handling

This commit is contained in:
fourtf 2018-04-08 17:07:40 +02:00
parent f6a0ac1bda
commit 46a457ea65

View file

@ -13,6 +13,7 @@
#ifdef USEWINSDK #ifdef USEWINSDK
#include <ObjIdl.h> #include <ObjIdl.h>
#include <VersionHelpers.h>
#include <Windows.h> #include <Windows.h>
#include <dwmapi.h> #include <dwmapi.h>
#include <gdiplus.h> #include <gdiplus.h>
@ -60,7 +61,7 @@ void BaseWindow::init()
if (this->hasCustomWindowFrame()) { if (this->hasCustomWindowFrame()) {
// CUSTOM WINDOW FRAME // CUSTOM WINDOW FRAME
QVBoxLayout *layout = new QVBoxLayout(); QVBoxLayout *layout = new QVBoxLayout();
layout->setMargin(1); layout->setContentsMargins(0, 1, 0, 0);
layout->setSpacing(0); layout->setSpacing(0);
this->setLayout(layout); this->setLayout(layout);
{ {
@ -155,7 +156,9 @@ QWidget *BaseWindow::getLayoutContainer()
bool BaseWindow::hasCustomWindowFrame() bool BaseWindow::hasCustomWindowFrame()
{ {
#ifdef USEWINSDK #ifdef USEWINSDK
return this->enableCustomFrame; static bool isWin8 = IsWindows8OrGreater();
return isWin8 && this->enableCustomFrame;
#else #else
return false; return false;
#endif #endif
@ -165,7 +168,8 @@ void BaseWindow::themeRefreshEvent()
{ {
if (this->enableCustomFrame) { if (this->enableCustomFrame) {
QPalette palette; QPalette palette;
palette.setColor(QPalette::Background, this->themeManager.window.background); // palette.setColor(QPalette::Background, this->themeManager.window.background);
palette.setColor(QPalette::Background, QColor(0, 0, 0, 0));
palette.setColor(QPalette::Foreground, this->themeManager.window.text); palette.setColor(QPalette::Foreground, this->themeManager.window.text);
this->setPalette(palette); this->setPalette(palette);
@ -283,8 +287,19 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
} }
case WM_NCCALCSIZE: { case WM_NCCALCSIZE: {
if (this->hasCustomWindowFrame()) { if (this->hasCustomWindowFrame()) {
// this kills the window frame and title bar we added with int cx = GetSystemMetrics(SM_CXSIZEFRAME);
// WS_THICKFRAME and WS_CAPTION int cy = GetSystemMetrics(SM_CYSIZEFRAME);
if (msg->wParam == TRUE) {
NCCALCSIZE_PARAMS *ncp = (reinterpret_cast<NCCALCSIZE_PARAMS *>(msg->lParam));
ncp->lppos->flags |= SWP_NOREDRAW;
RECT *clientRect = &ncp->rgrc[0];
clientRect->left += cx;
clientRect->top += 0;
clientRect->right -= cx;
clientRect->bottom -= cy;
}
*result = 0; *result = 0;
return true; return true;
} else { } else {
@ -307,21 +322,21 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
if (resizeWidth) { if (resizeWidth) {
// left border // left border
if (x >= winrect.left && x < winrect.left + border_width) { if (x < winrect.left + border_width) {
*result = HTLEFT; *result = HTLEFT;
} }
// right border // right border
if (x < winrect.right && x >= winrect.right - border_width) { if (x >= winrect.right - border_width) {
*result = HTRIGHT; *result = HTRIGHT;
} }
} }
if (resizeHeight) { if (resizeHeight) {
// bottom border // bottom border
if (y < winrect.bottom && y >= winrect.bottom - border_width) { if (y >= winrect.bottom - border_width) {
*result = HTBOTTOM; *result = HTBOTTOM;
} }
// top border // top border
if (y >= winrect.top && y < winrect.top + border_width) { if (y < winrect.top + border_width) {
*result = HTTOP; *result = HTTOP;
} }
} }
@ -374,13 +389,6 @@ bool BaseWindow::nativeEvent(const QByteArray &eventType, void *message, long *r
return QWidget::nativeEvent(eventType, message, result); return QWidget::nativeEvent(eventType, message, result);
} }
break; break;
} // end case WM_NCHITTEST
case WM_CLOSE: {
// if (this->enableCustomFrame) {
// this->close();
// }
return QWidget::nativeEvent(eventType, message, result);
break;
} }
default: default:
return QWidget::nativeEvent(eventType, message, result); return QWidget::nativeEvent(eventType, message, result);
@ -396,9 +404,6 @@ void BaseWindow::showEvent(QShowEvent *event)
const MARGINS shadow = {1, 1, 1, 1}; const MARGINS shadow = {1, 1, 1, 1};
DwmExtendFrameIntoClientArea((HWND)this->winId(), &shadow); DwmExtendFrameIntoClientArea((HWND)this->winId(), &shadow);
SetWindowPos((HWND)this->winId(), 0, 0, 0, 0, 0,
SWP_FRAMECHANGED | SWP_NOMOVE | SWP_NOSIZE);
} }
BaseWidget::showEvent(event); BaseWidget::showEvent(event);
@ -407,30 +412,12 @@ void BaseWindow::showEvent(QShowEvent *event)
void BaseWindow::paintEvent(QPaintEvent *event) void BaseWindow::paintEvent(QPaintEvent *event)
{ {
if (this->hasCustomWindowFrame()) { if (this->hasCustomWindowFrame()) {
BaseWidget::paintEvent(event);
QPainter painter(this); QPainter painter(this);
bool windowFocused = this->window() == QApplication::activeWindow(); // bool windowFocused = this->window() == QApplication::activeWindow();
// QLinearGradient gradient(0, 0, 10, 250); painter.fillRect(QRect(0, 1, this->width(), this->height()),
// gradient.setColorAt(1, this->themeManager.window.background);
// this->themeManager.tabs.selected.backgrounds.unfocused.color());
// if (windowFocused) {
// gradient.setColorAt(.4,
// this->themeManager.tabs.selected.backgrounds.regular.color());
// } else {
// gradient.setColorAt(.4,
// this->themeManager.tabs.selected.backgrounds.unfocused.color());
// }
// painter.setPen(QPen(QBrush(gradient), 1));
QColor &border = windowFocused ? this->themeManager.window.borderFocused
: this->themeManager.window.borderUnfocused;
painter.setPen(QPen(QBrush(border), 1));
painter.drawRect(0, 0, this->width() - 1, this->height() - 1);
} }
} }
#endif #endif