This commit is contained in:
朱子楚\zhuzi 2023-12-29 00:40:18 +08:00
parent 0b0fbe1e64
commit 8253fb611f
4 changed files with 53 additions and 52 deletions

View File

@ -1,7 +1,6 @@
#include "FluFramelessHelper.h" #include "FluFramelessHelper.h"
#include <QGuiApplication> #include <QGuiApplication>
#include "FluTools.h"
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#pragma comment (lib,"user32.lib") #pragma comment (lib,"user32.lib")
#pragma comment (lib,"dwmapi.lib") #pragma comment (lib,"dwmapi.lib")
@ -14,6 +13,28 @@ static inline QByteArray qtNativeEventType()
return result; return result;
} }
static inline bool isWindows11OrGreater() {
DWORD dwVersion = 0;
DWORD dwBuild = 0;
#pragma warning(push)
#pragma warning(disable : 4996)
dwVersion = GetVersion();
if (dwVersion < 0x80000000)
dwBuild = (DWORD)(HIWORD(dwVersion));
#pragma warning(pop)
return dwBuild < 22000;
}
static inline bool isTaskbarAutoHide() {
APPBARDATA appBarData;
memset(&appBarData, 0, sizeof(appBarData));
appBarData.cbSize = sizeof(appBarData);
appBarData.hWnd = FindWindowW(L"Shell_TrayWnd", NULL);
LPARAM lParam = SHAppBarMessage(ABM_GETSTATE, &appBarData);
return lParam & ABS_AUTOHIDE;
}
static inline bool isCompositionEnabled(){ static inline bool isCompositionEnabled(){
typedef HRESULT (WINAPI* DwmIsCompositionEnabledPtr)(BOOL *pfEnabled); typedef HRESULT (WINAPI* DwmIsCompositionEnabledPtr)(BOOL *pfEnabled);
HMODULE module = LoadLibraryW(L"dwmapi.dll"); HMODULE module = LoadLibraryW(L"dwmapi.dll");
@ -89,6 +110,20 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void *
} }
return false; return false;
}else if(uMsg == WM_NCCALCSIZE){ }else if(uMsg == WM_NCCALCSIZE){
NCCALCSIZE_PARAMS* sz = reinterpret_cast<NCCALCSIZE_PARAMS*>(lParam);
if(IsZoomed(hwnd)){
sz->rgrc[0].left += 8;
sz->rgrc[0].top += 8;
sz->rgrc[0].right -= 8;
sz->rgrc[0].bottom -= isTaskbarAutoHide() ? 9 : 8;
}else{
sz->rgrc[0].top += isWindows11OrGreater() ? 0 : 1;
if(isCompositionEnabled()){
sz->rgrc[0].right -= 8;
sz->rgrc[0].bottom -= 8;
sz->rgrc[0].left -= -8;
}
}
*result = WVR_REDRAW; *result = WVR_REDRAW;
return true; return true;
}else if(uMsg == WM_NCPAINT){ }else if(uMsg == WM_NCPAINT){
@ -104,7 +139,7 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void *
} }
return false; return false;
}else if(uMsg == WM_NCHITTEST){ }else if(uMsg == WM_NCHITTEST){
if(FluTools::getInstance()->isWindows11OrGreater() && _helper->hoverMaxBtn() && _helper->resizeable()){ if(isWindows11OrGreater() && _helper->hoverMaxBtn() && _helper->resizeable()){
if (*result == HTNOWHERE) { if (*result == HTNOWHERE) {
*result = HTZOOM; *result = HTZOOM;
} }
@ -112,14 +147,14 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void *
} }
return false; return false;
}else if(uMsg == WM_NCLBUTTONDBLCLK || uMsg == WM_NCLBUTTONDOWN){ }else if(uMsg == WM_NCLBUTTONDBLCLK || uMsg == WM_NCLBUTTONDOWN){
if(FluTools::getInstance()->isWindows11OrGreater() && _helper->hoverMaxBtn() && _helper->resizeable()){ if(isWindows11OrGreater() && _helper->hoverMaxBtn() && _helper->resizeable()){
QMouseEvent event = QMouseEvent(QEvent::MouseButtonPress, QPoint(), QPoint(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QMouseEvent event = QMouseEvent(QEvent::MouseButtonPress, QPoint(), QPoint(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QGuiApplication::sendEvent(_helper->maximizeButton(),&event); QGuiApplication::sendEvent(_helper->maximizeButton(),&event);
return true; return true;
} }
return false; return false;
}else if(uMsg == WM_NCLBUTTONUP || uMsg == WM_NCRBUTTONUP){ }else if(uMsg == WM_NCLBUTTONUP || uMsg == WM_NCRBUTTONUP){
if(FluTools::getInstance()->isWindows11OrGreater() && _helper->hoverMaxBtn() && _helper->resizeable()){ if(isWindows11OrGreater() && _helper->hoverMaxBtn() && _helper->resizeable()){
QMouseEvent event = QMouseEvent(QEvent::MouseButtonRelease, QPoint(), QPoint(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier); QMouseEvent event = QMouseEvent(QEvent::MouseButtonRelease, QPoint(), QPoint(), Qt::LeftButton, Qt::LeftButton, Qt::NoModifier);
QGuiApplication::sendEvent(_helper->maximizeButton(),&event); QGuiApplication::sendEvent(_helper->maximizeButton(),&event);
} }
@ -234,7 +269,9 @@ void FluFramelessHelper::componentComplete(){
} }
if(!window.isNull()){ if(!window.isNull()){
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
window->setFlags(window->flags() | Qt::FramelessWindowHint | Qt::WindowMinimizeButtonHint); if(!isCompositionEnabled()){
window->setFlag(Qt::FramelessWindowHint,true);
}
_nativeEvent =new FramelessEventFilter(this); _nativeEvent =new FramelessEventFilter(this);
qApp->installNativeEventFilter(_nativeEvent); qApp->installNativeEventFilter(_nativeEvent);
HWND hwnd = reinterpret_cast<HWND>(window->winId()); HWND hwnd = reinterpret_cast<HWND>(window->winId());
@ -244,7 +281,6 @@ void FluFramelessHelper::componentComplete(){
}else{ }else{
SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME | WS_CAPTION); SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_THICKFRAME | WS_CAPTION);
} }
showShadow(hwnd);
SetWindowPos(hwnd,nullptr,0,0,0,0,SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED); SetWindowPos(hwnd,nullptr,0,0,0,0,SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
#else #else
window->setFlags((window->flags() & (~Qt::WindowMinMaxButtonsHint) & (~Qt::Dialog)) | Qt::FramelessWindowHint | Qt::Window); window->setFlags((window->flags() & (~Qt::WindowMinMaxButtonsHint) & (~Qt::Dialog)) | Qt::FramelessWindowHint | Qt::Window);

View File

@ -200,14 +200,15 @@ bool FluTools::isWindows11OrGreater(){
static QVariant var; static QVariant var;
if(var.isNull()){ if(var.isNull()){
#if defined(Q_OS_WIN) #if defined(Q_OS_WIN)
QSettings regKey {QString::fromUtf8("HKEY_LOCAL_MACHINE\\SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion"), QSettings::NativeFormat}; DWORD dwVersion = 0;
if (regKey.contains(QString::fromUtf8("CurrentBuildNumber"))) { DWORD dwBuild = 0;
auto buildNumber = regKey.value(QString::fromUtf8("CurrentBuildNumber")).toInt(); #pragma warning(push)
if(buildNumber>=22000){ #pragma warning(disable : 4996)
var = QVariant::fromValue(true); dwVersion = GetVersion();
return true; if (dwVersion < 0x80000000)
} dwBuild = (DWORD)(HIWORD(dwVersion));
} #pragma warning(pop)
return dwBuild < 22000;
#endif #endif
var = QVariant::fromValue(false); var = QVariant::fromValue(false);
return false; return false;

View File

@ -53,7 +53,6 @@ Window {
signal showSystemMenu signal showSystemMenu
signal initArgument(var argument) signal initArgument(var argument)
signal firstVisible() signal firstVisible()
property point _offsetXY : Qt.point(0,0)
id:window id:window
color:"transparent" color:"transparent"
Component.onCompleted: { Component.onCompleted: {
@ -73,19 +72,6 @@ Window {
Component.onDestruction: { Component.onDestruction: {
lifecycle.onDestruction() lifecycle.onDestruction()
} }
onVisibilityChanged: {
if(visibility === Window.Maximized || visibility === Window.FullScreen){
var dx = window.x-Screen.virtualX
var dy = window.y-Screen.virtualY
if(dx<0 && dy<0){
_offsetXY = Qt.point(Math.abs(dx+1),Math.abs(dy+1))
}else{
_offsetXY = Qt.point(0,0)
}
}else{
_offsetXY = Qt.point(0,0)
}
}
onShowSystemMenu: { onShowSystemMenu: {
if(loader_frameless_helper.item){ if(loader_frameless_helper.item){
loader_frameless_helper.item.showSystemMenu() loader_frameless_helper.item.showSystemMenu()
@ -191,10 +177,6 @@ Window {
id:layout_container id:layout_container
anchors{ anchors{
fill:parent fill:parent
leftMargin: _offsetXY.x
rightMargin: _offsetXY.x
topMargin: _offsetXY.y
bottomMargin: _offsetXY.y
} }
onWidthChanged: { onWidthChanged: {
window.appBar.width = width window.appBar.width = width
@ -247,7 +229,7 @@ Window {
border.width: window.resizeBorderWidth border.width: window.resizeBorderWidth
border.color: window.resizeBorderColor border.color: window.resizeBorderColor
visible: { visible: {
if(window.useSystemAppBar){ if(window.useSystemAppBar || FluTools.isWin()){
return false return false
} }
if(window.visibility == Window.Maximized || window.visibility == Window.FullScreen){ if(window.visibility == Window.Maximized || window.visibility == Window.FullScreen){

View File

@ -52,7 +52,6 @@ Window {
signal showSystemMenu signal showSystemMenu
signal initArgument(var argument) signal initArgument(var argument)
signal firstVisible() signal firstVisible()
property point _offsetXY : Qt.point(0,0)
id:window id:window
color:"transparent" color:"transparent"
Component.onCompleted: { Component.onCompleted: {
@ -72,19 +71,6 @@ Window {
Component.onDestruction: { Component.onDestruction: {
lifecycle.onDestruction() lifecycle.onDestruction()
} }
onVisibilityChanged: {
if(visibility === Window.Maximized || visibility === Window.FullScreen){
var dx = window.x-Screen.virtualX
var dy = window.y-Screen.virtualY
if(dx<0 && dy<0){
_offsetXY = Qt.point(Math.abs(dx+1),Math.abs(dy+1))
}else{
_offsetXY = Qt.point(0,0)
}
}else{
_offsetXY = Qt.point(0,0)
}
}
onShowSystemMenu: { onShowSystemMenu: {
if(loader_frameless_helper.item){ if(loader_frameless_helper.item){
loader_frameless_helper.item.showSystemMenu() loader_frameless_helper.item.showSystemMenu()
@ -190,10 +176,6 @@ Window {
id:layout_container id:layout_container
anchors{ anchors{
fill:parent fill:parent
leftMargin: _offsetXY.x
rightMargin: _offsetXY.x
topMargin: _offsetXY.y
bottomMargin: _offsetXY.y
} }
onWidthChanged: { onWidthChanged: {
window.appBar.width = width window.appBar.width = width
@ -246,7 +228,7 @@ Window {
border.width: window.resizeBorderWidth border.width: window.resizeBorderWidth
border.color: window.resizeBorderColor border.color: window.resizeBorderColor
visible: { visible: {
if(window.useSystemAppBar){ if(window.useSystemAppBar || FluTools.isWin()){
return false return false
} }
if(window.visibility == Window.Maximized || window.visibility == Window.FullScreen){ if(window.visibility == Window.Maximized || window.visibility == Window.FullScreen){