forked from github_mirror/framelesshelper
Minor improvements.
The ignore and draggable areas/objects are limited in the title bar area only. There's no need to judge them outside the title bar area. Signed-off-by: Yuhang Zhao <2546789017@qq.com>
This commit is contained in:
parent
c4fe56345f
commit
ed6267461c
|
@ -1,7 +1,20 @@
|
||||||
#include "framelesshelper.h"
|
#include "framelesshelper.h"
|
||||||
#include <QApplication>
|
#include <QApplication>
|
||||||
|
#include <QScreen>
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
|
|
||||||
|
static void moveWindowToDesktopCenter(QWidget *const widget) {
|
||||||
|
if (widget) {
|
||||||
|
const QRect sg = widget->screen()->geometry();
|
||||||
|
const int sw = sg.width();
|
||||||
|
const int sh = sg.height();
|
||||||
|
const int ww = widget->width();
|
||||||
|
const int wh = widget->height();
|
||||||
|
widget->move(qRound(static_cast<qreal>(sw - ww) / 2.0),
|
||||||
|
qRound(static_cast<qreal>(sh - wh) / 2.0));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
// High DPI scaling is enabled by default from Qt 6
|
// High DPI scaling is enabled by default from Qt 6
|
||||||
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
#if (QT_VERSION < QT_VERSION_CHECK(6, 0, 0))
|
||||||
|
@ -27,6 +40,8 @@ int main(int argc, char *argv[]) {
|
||||||
|
|
||||||
QWidget widget;
|
QWidget widget;
|
||||||
helper.removeWindowFrame(&widget);
|
helper.removeWindowFrame(&widget);
|
||||||
|
widget.resize(800, 600);
|
||||||
|
moveWindowToDesktopCenter(&widget);
|
||||||
widget.show();
|
widget.show();
|
||||||
|
|
||||||
return QApplication::exec();
|
return QApplication::exec();
|
||||||
|
|
|
@ -132,16 +132,19 @@ int main(int argc, char *argv[]) {
|
||||||
mainLayout->addLayout(tbLayout);
|
mainLayout->addLayout(tbLayout);
|
||||||
mainLayout->addStretch();
|
mainLayout->addStretch();
|
||||||
widget.setLayout(mainLayout);
|
widget.setLayout(mainLayout);
|
||||||
WinNativeEventFilter::WINDOWDATA data_widget;
|
|
||||||
data_widget.ignoreObjects << minimizeButton << maximizeButton
|
|
||||||
<< closeButton;
|
|
||||||
const auto hWnd_widget = reinterpret_cast<HWND>(widget.winId());
|
const auto hWnd_widget = reinterpret_cast<HWND>(widget.winId());
|
||||||
|
WinNativeEventFilter::addFramelessWindow(hWnd_widget);
|
||||||
|
const auto data_widget = WinNativeEventFilter::windowData(hWnd_widget);
|
||||||
|
if (data_widget) {
|
||||||
|
data_widget->ignoreObjects << minimizeButton << maximizeButton
|
||||||
|
<< closeButton;
|
||||||
|
}
|
||||||
const int tbh_widget = WinNativeEventFilter::getSystemMetric(
|
const int tbh_widget = WinNativeEventFilter::getSystemMetric(
|
||||||
hWnd_widget, WinNativeEventFilter::SystemMetric::TitleBarHeight, false);
|
hWnd_widget, WinNativeEventFilter::SystemMetric::TitleBarHeight, false);
|
||||||
updateQtFrame(widget.windowHandle(),
|
updateQtFrame(widget.windowHandle(),
|
||||||
(tbh_widget > 0 ? tbh_widget : m_defaultTitleBarHeight));
|
(tbh_widget > 0 ? tbh_widget : m_defaultTitleBarHeight));
|
||||||
widget.resize(800, 600);
|
widget.resize(800, 600);
|
||||||
WinNativeEventFilter::addFramelessWindow(hWnd_widget, &data_widget, true);
|
WinNativeEventFilter::moveWindowToDesktopCenter(hWnd_widget);
|
||||||
widget.show();
|
widget.show();
|
||||||
|
|
||||||
#ifdef QT_QUICK_LIB
|
#ifdef QT_QUICK_LIB
|
||||||
|
@ -151,18 +154,17 @@ int main(int argc, char *argv[]) {
|
||||||
const int tbh_qml_sys = WinNativeEventFilter::getSystemMetric(
|
const int tbh_qml_sys = WinNativeEventFilter::getSystemMetric(
|
||||||
hWnd_qml, WinNativeEventFilter::SystemMetric::TitleBarHeight, false);
|
hWnd_qml, WinNativeEventFilter::SystemMetric::TitleBarHeight, false);
|
||||||
const int tbh_qml = tbh_qml_sys > 0 ? tbh_qml_sys : m_defaultTitleBarHeight;
|
const int tbh_qml = tbh_qml_sys > 0 ? tbh_qml_sys : m_defaultTitleBarHeight;
|
||||||
updateQtFrame(&view, tbh_qml);
|
|
||||||
view.rootContext()->setContextProperty(QString::fromUtf8("$TitleBarHeight"),
|
view.rootContext()->setContextProperty(QString::fromUtf8("$TitleBarHeight"),
|
||||||
tbh_qml);
|
tbh_qml);
|
||||||
view.setSource(QUrl(QString::fromUtf8("qrc:///qml/main.qml")));
|
view.setSource(QUrl(QString::fromUtf8("qrc:///qml/main.qml")));
|
||||||
QObject::connect(
|
QObject::connect(
|
||||||
&view, &MyQuickView::windowSizeChanged, [hWnd_qml](const QSize &size) {
|
&view, &MyQuickView::windowSizeChanged, [hWnd_qml](const QSize &size) {
|
||||||
const auto data = WinNativeEventFilter::windowData(hWnd_qml);
|
const auto data_qml = WinNativeEventFilter::windowData(hWnd_qml);
|
||||||
if (data) {
|
if (data_qml) {
|
||||||
const int tbh_qml = WinNativeEventFilter::getSystemMetric(
|
const int tbh_qml = WinNativeEventFilter::getSystemMetric(
|
||||||
hWnd_qml,
|
hWnd_qml,
|
||||||
WinNativeEventFilter::SystemMetric::TitleBarHeight, false);
|
WinNativeEventFilter::SystemMetric::TitleBarHeight, false);
|
||||||
data->draggableAreas = {
|
data_qml->draggableAreas = {
|
||||||
{0, 0, (size.width() - (m_defaultButtonWidth * 3)),
|
{0, 0, (size.width() - (m_defaultButtonWidth * 3)),
|
||||||
tbh_qml}};
|
tbh_qml}};
|
||||||
}
|
}
|
||||||
|
@ -179,8 +181,10 @@ int main(int argc, char *argv[]) {
|
||||||
SLOT(showNormal()));
|
SLOT(showNormal()));
|
||||||
QObject::connect(rootObject, SIGNAL(closeButtonClicked()), &view,
|
QObject::connect(rootObject, SIGNAL(closeButtonClicked()), &view,
|
||||||
SLOT(close()));
|
SLOT(close()));
|
||||||
|
WinNativeEventFilter::addFramelessWindow(hWnd_qml);
|
||||||
|
updateQtFrame(&view, tbh_qml);
|
||||||
view.resize(800, 600);
|
view.resize(800, 600);
|
||||||
WinNativeEventFilter::addFramelessWindow(hWnd_qml, nullptr, true);
|
WinNativeEventFilter::moveWindowToDesktopCenter(hWnd_qml);
|
||||||
view.show();
|
view.show();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -1358,15 +1358,12 @@ bool WinNativeEventFilter::nativeEventFilter(const QByteArray &eventType,
|
||||||
return HTCLIENT;
|
return HTCLIENT;
|
||||||
}
|
}
|
||||||
const bool isTop = (mouse.y <= bh) && isResizePermitted;
|
const bool isTop = (mouse.y <= bh) && isResizePermitted;
|
||||||
const bool isBottom =
|
const bool isBottom = (mouse.y >= (wh - bh));
|
||||||
(mouse.y >= (wh - bh)) && isResizePermitted;
|
|
||||||
// Make the border a little wider to let the user easy to resize
|
// Make the border a little wider to let the user easy to resize
|
||||||
// on corners.
|
// on corners.
|
||||||
const int factor = (isTop || isBottom) ? 2 : 1;
|
const int factor = (isTop || isBottom) ? 2 : 1;
|
||||||
const bool isLeft =
|
const bool isLeft = (mouse.x <= (bw * factor));
|
||||||
(mouse.x <= (bw * factor)) && isResizePermitted;
|
const bool isRight = (mouse.x >= (ww - (bw * factor)));
|
||||||
const bool isRight =
|
|
||||||
(mouse.x >= (ww - (bw * factor))) && isResizePermitted;
|
|
||||||
const bool fixedSize = _data->windowData.fixedSize;
|
const bool fixedSize = _data->windowData.fixedSize;
|
||||||
const auto getBorderValue = [fixedSize](int value) -> int {
|
const auto getBorderValue = [fixedSize](int value) -> int {
|
||||||
// HTBORDER: non-resizeable window border.
|
// HTBORDER: non-resizeable window border.
|
||||||
|
|
Loading…
Reference in New Issue