[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

相关文章

SpringBoot实现微信小程序支付功能

《SpringBoot实现微信小程序支付功能》小程序支付功能已成为众多应用的核心需求之一,本文主要介绍了SpringBoot实现微信小程序支付功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作... 目录一、引言二、准备工作(一)微信支付商户平台配置(二)Spring Boot项目搭建(三)配置文件

基于Python实现高效PPT转图片工具

《基于Python实现高效PPT转图片工具》在日常工作中,PPT是我们常用的演示工具,但有时候我们需要将PPT的内容提取为图片格式以便于展示或保存,所以本文将用Python实现PPT转PNG工具,希望... 目录1. 概述2. 功能使用2.1 安装依赖2.2 使用步骤2.3 代码实现2.4 GUI界面3.效

MySQL更新某个字段拼接固定字符串的实现

《MySQL更新某个字段拼接固定字符串的实现》在MySQL中,我们经常需要对数据库中的某个字段进行更新操作,本文就来介绍一下MySQL更新某个字段拼接固定字符串的实现,感兴趣的可以了解一下... 目录1. 查看字段当前值2. 更新字段拼接固定字符串3. 验证更新结果mysql更新某个字段拼接固定字符串 -

java实现延迟/超时/定时问题

《java实现延迟/超时/定时问题》:本文主要介绍java实现延迟/超时/定时问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java实现延迟/超时/定时java 每间隔5秒执行一次,一共执行5次然后结束scheduleAtFixedRate 和 schedu

Java Optional避免空指针异常的实现

《JavaOptional避免空指针异常的实现》空指针异常一直是困扰开发者的常见问题之一,本文主要介绍了JavaOptional避免空指针异常的实现,帮助开发者编写更健壮、可读性更高的代码,减少因... 目录一、Optional 概述二、Optional 的创建三、Optional 的常用方法四、Optio

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Spring Boot项目中结合MyBatis实现MySQL的自动主从切换功能

《SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能》:本文主要介绍SpringBoot项目中结合MyBatis实现MySQL的自动主从切换功能,本文分步骤给大家介绍的... 目录原理解析1. mysql主从复制(Master-Slave Replication)2. 读写分离3.

Redis实现延迟任务的三种方法详解

《Redis实现延迟任务的三种方法详解》延迟任务(DelayedTask)是指在未来的某个时间点,执行相应的任务,本文为大家整理了三种常见的实现方法,感兴趣的小伙伴可以参考一下... 目录1.前言2.Redis如何实现延迟任务3.代码实现3.1. 过期键通知事件实现3.2. 使用ZSet实现延迟任务3.3

基于Python和MoviePy实现照片管理和视频合成工具

《基于Python和MoviePy实现照片管理和视频合成工具》在这篇博客中,我们将详细剖析一个基于Python的图形界面应用程序,该程序使用wxPython构建用户界面,并结合MoviePy、Pill... 目录引言项目概述代码结构分析1. 导入和依赖2. 主类:PhotoManager初始化方法:__in

springboot filter实现请求响应全链路拦截

《springbootfilter实现请求响应全链路拦截》这篇文章主要为大家详细介绍了SpringBoot如何结合Filter同时拦截请求和响应,从而实现​​日志采集自动化,感兴趣的小伙伴可以跟随小... 目录一、为什么你需要这个过滤器?​​​二、核心实现:一个Filter搞定双向数据流​​​​三、完整代码