This commit is contained in:
朱子楚\zhuzi 2023-12-18 21:29:38 +08:00
parent ecd0f29d30
commit 1bf992ed69
3 changed files with 166 additions and 111 deletions

View File

@ -12,6 +12,23 @@ static inline QByteArray qtNativeEventType()
}
#endif
static inline bool isCompositionEnabled(){
typedef HRESULT (WINAPI* DwmIsCompositionEnabledPtr)(BOOL *pfEnabled);
HMODULE module = LoadLibraryW(L"dwmapi.dll");
if (module)
{
BOOL composition_enabled = false;
DwmIsCompositionEnabledPtr dwm_is_composition_enabled;
dwm_is_composition_enabled= reinterpret_cast<DwmIsCompositionEnabledPtr>(GetProcAddress(module, "DwmIsCompositionEnabled"));
if (dwm_is_composition_enabled)
{
dwm_is_composition_enabled(&composition_enabled);
}
return composition_enabled;
}
return false;
}
FramelessEventFilter::FramelessEventFilter(QQuickWindow* window){
_window = window;
_current = window->winId();
@ -23,25 +40,27 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void *
return false;
}
const auto msg = static_cast<const MSG *>(message);
const HWND hWnd = msg->hwnd;
if (!hWnd) {
const HWND hwnd = msg->hwnd;
if (!hwnd) {
return false;
}
const qint64 wid = reinterpret_cast<qint64>(hWnd);
const qint64 wid = reinterpret_cast<qint64>(hwnd);
if(wid != _current){
return false;
}
const UINT uMsg = msg->message;
if (!msg || !msg->hwnd)
const WPARAM wParam = msg->wParam;
const LPARAM lParam = msg->lParam;
if (!msg || !hwnd)
{
return false;
}
if(uMsg == WM_WINDOWPOSCHANGING){
WINDOWPOS* wp = reinterpret_cast<WINDOWPOS*>(msg->lParam);
WINDOWPOS* wp = reinterpret_cast<WINDOWPOS*>(lParam);
if (wp != nullptr && (wp->flags & SWP_NOSIZE) == 0)
{
wp->flags |= SWP_NOCOPYBITS;
*result = DefWindowProc(msg->hwnd, msg->message, msg->wParam, msg->lParam);
*result = DefWindowProc(hwnd, uMsg, wParam, lParam);
return true;
}
return false;
@ -49,8 +68,22 @@ bool FramelessEventFilter::nativeEventFilter(const QByteArray &eventType, void *
*result = WVR_REDRAW;
return true;
}else if(uMsg == WM_NCPAINT){
if(!isCompositionEnabled()){
*result = WVR_REDRAW;
return true;
}
return false;
}else if(uMsg == WM_NCACTIVATE){
if(isCompositionEnabled()){
*result = DefWindowProc(hwnd, uMsg, wParam, -1);
}else{
if (wParam == FALSE) {
*result = TRUE;
} else {
*result = FALSE;
}
}
return true;
}
return false;
#endif
@ -90,7 +123,7 @@ void FluFrameless::updateCursor(int edges){
}
bool FluFrameless::eventFilter(QObject *obj, QEvent *ev){
if (!_window.isNull() && _window->flags()& Qt::FramelessWindowHint) {
if (!_window.isNull()) {
static int edges = 0;
const int margin = 8;
@ -156,25 +189,29 @@ void FluFrameless::componentComplete(){
o = o->parent();
}
if(!_window.isNull()){
_window->setFlag(Qt::FramelessWindowHint,true);
_window->installEventFilter(this);
#ifdef Q_OS_WIN
_nativeEvent =new FramelessEventFilter(_window);
qApp->installNativeEventFilter(_nativeEvent);
HWND hwnd = reinterpret_cast<HWND>(_window->winId());
ULONG_PTR cNewStyle = GetClassLongPtr(hwnd, GCL_STYLE) | CS_DROPSHADOW;
SetClassLongPtr(hwnd, GCL_STYLE, cNewStyle);
DWORD style = GetWindowLongPtr(hwnd,GWL_STYLE);
SetWindowLongPtr(hwnd,GWL_STYLE,style &~ WS_SYSMENU);
SetWindowPos(hwnd,nullptr,0,0,0,0,SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_NOMOVE | SWP_NOSIZE |SWP_FRAMECHANGED);
#else
_window->setFlag(Qt::FramelessWindowHint,true);
#endif
_window->installEventFilter(this);
}
}
FluFrameless::~FluFrameless(){
if (!_window.isNull()) {
_window->setFlag(Qt::FramelessWindowHint,false);
_window->removeEventFilter(this);
#ifdef Q_OS_WIN
qApp->removeNativeEventFilter(_nativeEvent);
#else
_window->setFlag(Qt::FramelessWindowHint,false);
#endif
}
}

View File

@ -197,6 +197,14 @@ Window {
FluLoader{
id:loader_frameless
}
Item{
anchors.fill: parent
anchors.margins: {
if(FluTools.isWin() && !window.useSystemAppBar){
return window.visibility === Window.Maximized ? 8 : 0
}
return 0
}
onWidthChanged: {
window.appBar.width = width
}
@ -253,6 +261,7 @@ Window {
return true
}
}
}
function destoryOnClose(){
lifecycle.onDestoryOnClose()
}

View File

@ -196,6 +196,14 @@ Window {
FluLoader{
id:loader_frameless
}
Item{
anchors.fill: parent
anchors.margins: {
if(FluTools.isWin() && !window.useSystemAppBar){
return window.visibility === Window.Maximized ? 8 : 0
}
return 0
}
onWidthChanged: {
window.appBar.width = width
}
@ -252,6 +260,7 @@ Window {
return true
}
}
}
function destoryOnClose(){
lifecycle.onDestoryOnClose()
}