[Qt]FrameLessWindow实现调整大小、移动弹窗并具有Aero效果

本文主要是介绍[Qt]FrameLessWindow实现调整大小、移动弹窗并具有Aero效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

说明

我们知道QWidget等设置了this->setWindowFlags(Qt::FramelessWindowHint);后无法移动和调整大小,但实际项目中是需要窗口能够调整大小的。所以以实现FrameLess弹窗调整大小及移动弹窗需求,并且在Windows 10上有Aero效果。
先看一下效果:
FrameLessWindow实现调整大小、移动弹窗并具有Aero效果

代码

大部分参考这个github。然后自己修改了一下,因为github上面的,在设置了qss后怎么也实现不了窗口圆角以阴影。下面修改版的代码可以实现圆角,但也没有阴影,只能在Widget中自己实现阴影了。
如果不需要圆角,github上面的也是会自带阴影的。不用下面的调整版实现方案。

#ifndef AEROMAINWINDOW_H
#define AEROMAINWINDOW_H#include <QMainWindow>class AeroMainWindow : public QMainWindow
{Q_OBJECTpublic:explicit AeroMainWindow(QWidget *parent = nullptr);~AeroMainWindow();//设置是否可以通过鼠标调整窗口大小//if resizeable is set to false, then the window can not be resized by mouse//but still can be resized programticallyvoid setResizeable(bool resizeable=true);bool isResizeable(){return m_bResizeable;}//设置可调整大小区域的宽度,在此区域内,可以使用鼠标调整窗口大小//set border width, inside this aera, window can be resized by mousevoid setResizeableAreaWidth(int width = 5);protected://设置一个标题栏widget,此widget会被当做标题栏对待//set a widget which will be treat as SYSTEM titlebarvoid setTitleBar(QWidget* titlebar);//在标题栏控件内,也可以有子控件如标签控件“label1”,此label1遮盖了标题栏,导致不能通过label1拖动窗口//要解决此问题,使用addIgnoreWidget(label1)//generally, we can add widget say "label1" on titlebar, and it will cover the titlebar under it//as a result, we can not drag and move the MainWindow with this "label1" again//we can fix this by add "label1" to a ignorelist, just call addIgnoreWidget(label1)void addIgnoreWidget(QWidget* widget);bool nativeEvent(const QByteArray &eventType, void *message, long *result);void resizeEvent(QResizeEvent *event);public slots:private slots:void onTitleBarDestroyed();private:QWidget *m_titleBar;QList<QWidget*> m_whiteList;int m_borderWidth;bool m_bResizeable;
};#endif // AEROMAINWINDOW_H
#include "aeromainwindow.h"#include <QGraphicsDropShadowEffect>
#include <QDesktopServices>
#include <QUrl>
#include <QGridLayout>
#include <QStyle>
#include <QDebug>
#include <QPushButton>#ifdef Q_OS_WIN
#include <windows.h>
#include <WinUser.h>
#include <windowsx.h>
#include <dwmapi.h>
#include <objidl.h> // Fixes error C2504: 'IUnknown' : base class undefined
#include <gdiplus.h>
#include <GdiPlusColor.h>
#pragma comment (lib,"Dwmapi.lib") // Adds missing library, fixes error LNK2019: unresolved external symbol __imp__DwmExtendFrameIntoClientArea
#pragma comment (lib,"user32.lib")
#endifAeroMainWindow::AeroMainWindow(QWidget *parent) :QMainWindow(parent),m_titleBar(Q_NULLPTR),m_borderWidth(5),m_bResizeable(true)
{this->setAttribute(Qt::WA_TranslucentBackground);//设置窗口背景透明this->setWindowFlags(Qt::FramelessWindowHint); //设置无边框窗口this->setResizeable(true);
}AeroMainWindow::~AeroMainWindow()
{
}void AeroMainWindow::setResizeable(bool resizeable)
{bool visible = isVisible();m_bResizeable = resizeable;if (m_bResizeable){setWindowFlags(windowFlags() | Qt::WindowMaximizeButtonHint);//此行代码可以带回Aero效果,同时也带回了标题栏和边框,在nativeEvent()会再次去掉标题栏////this line will get titlebar/thick frame/Aero back, which is exactly what we want//we will get rid of titlebar and thick frame again in nativeEvent() laterHWND hwnd = (HWND)this->winId();DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);::SetWindowLong(hwnd, GWL_STYLE, style | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CAPTION);}else{setWindowFlags(windowFlags() & ~Qt::WindowMaximizeButtonHint);HWND hwnd = (HWND)this->winId();DWORD style = ::GetWindowLong(hwnd, GWL_STYLE);::SetWindowLong(hwnd, GWL_STYLE, style & ~WS_MAXIMIZEBOX & ~WS_CAPTION);}//保留一个像素的边框宽度,否则系统不会绘制边框阴影const MARGINS shadow = { 1, 1, 1, 1 };DwmExtendFrameIntoClientArea(HWND(winId()), &shadow);setVisible(visible);
}void AeroMainWindow::setResizeableAreaWidth(int width)
{if (1 > width) width = 1;m_borderWidth = width;
}void AeroMainWindow::setTitleBar(QWidget* titlebar)
{m_titleBar = titlebar;if (!titlebar) return;connect(titlebar, SIGNAL(destroyed(QObject*)), this, SLOT(onTitleBarDestroyed()));
}void AeroMainWindow::onTitleBarDestroyed()
{if (m_titleBar == QObject::sender()){m_titleBar = Q_NULLPTR;}
}void AeroMainWindow::addIgnoreWidget(QWidget* widget)
{if (!widget) return;if (m_whiteList.contains(widget)) return;m_whiteList.append(widget);
}bool AeroMainWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
{MSG* msg = (MSG *)message;switch (msg->message){case WM_NCCALCSIZE:{//this kills the window frame and title bar we added with//WS_THICKFRAME and WS_CAPTION*result = 0;return true;} // WM_NCCALCSIZEcase WM_NCHITTEST:{*result = 0;const LONG border_width = m_borderWidth; //in pixelsRECT winrect;GetWindowRect(HWND(winId()), &winrect);long x = GET_X_LPARAM(msg->lParam);long y = GET_Y_LPARAM(msg->lParam);if(m_bResizeable){bool resizeWidth = minimumWidth() != maximumWidth();bool resizeHeight = minimumHeight() != maximumHeight();if(resizeWidth){//left borderif (x >= winrect.left && x < winrect.left + border_width){*result = HTLEFT;}//right borderif (x < winrect.right && x >= winrect.right - border_width){*result = HTRIGHT;}}if(resizeHeight){//bottom borderif (y < winrect.bottom && y >= winrect.bottom - border_width){*result = HTBOTTOM;}//top borderif (y >= winrect.top && y < winrect.top + border_width){*result = HTTOP;}}if(resizeWidth && resizeHeight){//bottom left cornerif (x >= winrect.left && x < winrect.left + border_width &&y < winrect.bottom && y >= winrect.bottom - border_width){*result = HTBOTTOMLEFT;}//bottom right cornerif (x < winrect.right && x >= winrect.right - border_width &&y < winrect.bottom && y >= winrect.bottom - border_width){*result = HTBOTTOMRIGHT;}//top left cornerif (x >= winrect.left && x < winrect.left + border_width &&y >= winrect.top && y < winrect.top + border_width){*result = HTTOPLEFT;}//top right cornerif (x < winrect.right && x >= winrect.right - border_width &&y >= winrect.top && y < winrect.top + border_width){*result = HTTOPRIGHT;}}}if (0 != *result) return true;//*result still equals 0, that means the cursor locate OUTSIDE the frame area//but it may locate in titlebar areaif (!m_titleBar) return false;//support highdpidouble dpr = this->devicePixelRatioF();QPoint pos = m_titleBar->mapFromGlobal(QPoint(x/dpr,y/dpr));if (!m_titleBar->rect().contains(pos)) return false;QWidget* child = m_titleBar->childAt(pos);if (!child){*result = HTCAPTION;return true;}else{if (m_whiteList.contains(child)){*result = HTCAPTION;return true;}}return false;} // WM_NCHITTESTdefault:return QMainWindow::nativeEvent(eventType, msg, result);}
}void AeroMainWindow::resizeEvent(QResizeEvent *event)
{if (m_titleBar)m_titleBar->setGeometry(QRect(0, 0, this->rect().width(), m_titleBar->rect().height()));QMainWindow::resizeEvent(event);
}
测试代码

生成一个类,继承上面的类。然后实现下面的内容。很简单:

#include "testmainwindow.h"
#include "ui_testmainwindow.h"TestMainWindow::TestMainWindow(QWidget *parent) :AeroMainWindow(parent),ui(new Ui::TestMainWindow)
{ui->setupUi(this);QWidget *titleBar = new QWidget(this);titleBar->setGeometry(QRect(0, 0, this->rect().width(), 25));this->setTitleBar(titleBar);this->setStyleSheet("background-color: red;\border-radius: 8px;");
}TestMainWindow::~TestMainWindow()
{delete ui;
}

这篇关于[Qt]FrameLessWindow实现调整大小、移动弹窗并具有Aero效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/181936

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

使用Python和OpenCV库实现实时颜色识别系统

《使用Python和OpenCV库实现实时颜色识别系统》:本文主要介绍使用Python和OpenCV库实现的实时颜色识别系统,这个系统能够通过摄像头捕捉视频流,并在视频中指定区域内识别主要颜色(红... 目录一、引言二、系统概述三、代码解析1. 导入库2. 颜色识别函数3. 主程序循环四、HSV色彩空间详解

PostgreSQL中MVCC 机制的实现

《PostgreSQL中MVCC机制的实现》本文主要介绍了PostgreSQL中MVCC机制的实现,通过多版本数据存储、快照隔离和事务ID管理实现高并发读写,具有一定的参考价值,感兴趣的可以了解一下... 目录一 MVCC 基本原理python1.1 MVCC 核心概念1.2 与传统锁机制对比二 Postg

SpringBoot整合Flowable实现工作流的详细流程

《SpringBoot整合Flowable实现工作流的详细流程》Flowable是一个使用Java编写的轻量级业务流程引擎,Flowable流程引擎可用于部署BPMN2.0流程定义,创建这些流程定义的... 目录1、流程引擎介绍2、创建项目3、画流程图4、开发接口4.1 Java 类梳理4.2 查看流程图4