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

news2024/10/1 19:40:36

说明

我们知道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_OBJECT

public:
    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 programtically
    void setResizeable(bool resizeable=true);
    bool isResizeable(){return m_bResizeable;}

    //设置可调整大小区域的宽度,在此区域内,可以使用鼠标调整窗口大小
    //set border width, inside this aera, window can be resized by mouse
    void setResizeableAreaWidth(int width = 5);

protected:
    //设置一个标题栏widget,此widget会被当做标题栏对待
    //set a widget which will be treat as SYSTEM titlebar
    void 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")
#endif

AeroMainWindow::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() later
        HWND 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_NCCALCSIZE
    case WM_NCHITTEST:
    {
        *result = 0;
        const LONG border_width = m_borderWidth; //in pixels
        RECT 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 border
                if (x >= winrect.left && x < winrect.left + border_width)
                {
                    *result = HTLEFT;
                }
                //right border
                if (x < winrect.right && x >= winrect.right - border_width)
                {
                    *result = HTRIGHT;
                }
            }
            if(resizeHeight)
            {
                //bottom border
                if (y < winrect.bottom && y >= winrect.bottom - border_width)
                {
                    *result = HTBOTTOM;
                }
                //top border
                if (y >= winrect.top && y < winrect.top + border_width)
                {
                    *result = HTTOP;
                }
            }
            if(resizeWidth && resizeHeight)
            {
                //bottom left corner
                if (x >= winrect.left && x < winrect.left + border_width &&
                        y < winrect.bottom && y >= winrect.bottom - border_width)
                {
                    *result = HTBOTTOMLEFT;
                }
                //bottom right corner
                if (x < winrect.right && x >= winrect.right - border_width &&
                        y < winrect.bottom && y >= winrect.bottom - border_width)
                {
                    *result = HTBOTTOMRIGHT;
                }
                //top left corner
                if (x >= winrect.left && x < winrect.left + border_width &&
                        y >= winrect.top && y < winrect.top + border_width)
                {
                    *result = HTTOPLEFT;
                }
                //top right corner
                if (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 area
        if (!m_titleBar) return false;

        //support highdpi
        double 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_NCHITTEST
    default:
        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;
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/836613.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

光环云入选“算力服务方阵”成员单位,共筑算力新生态,赋能数字经济发展

7月26日&#xff0c;由中国信息通信研究院、中国通信标准化协会联合主办的“2023第十届可信云大会”在北京举办。会上&#xff0c;光环云正式入选国内首个算力服务研究组织——算力服务方阵&#xff0c;携手方阵成员共筑算力新生态&#xff0c;赋能数字经济发展。 算力服务方阵…

日撸java_day56-57

文章目录 day56-57kMeans 聚类代码运行截图 day56-57kMeans 聚类 1.kMeans 聚类需要中心点收敛时结束. 2.数据集为 iris, 所以最后一个属性没使用. 如果对于没有决策属性的数据集, 需要进行相应修改. 3.数据没有归一化. 4.getRandomIndices() 和 knn 的完全相同, 拷贝过来. 本…

markdown语法速记

markdown语法速记 这里只记录兼容性强的语法格式&#xff1a; [markdown官方文档]https://markdown.com.cn 标题 #号后跟一个空格如&#xff1a;“# title_level_1” 代表一级标题&#xff0c;两个#号代表二级标题&#xff0c;以此类推&#xff1b;最小为6级标题换行 直接…

两数相加 II

给你两个 非空 链表来代表两个非负整数。数字最高位位于链表开始位置。它们的每个节点只存储一位数字。将这两数相加会返回一个新的链表。 你可以假设除了数字 0 之外&#xff0c;这两个数字都不会以零开头。 示例1&#xff1a; 输入&#xff1a;l1 [7,2,4,3], l2 [5,6,4] 输…

【ASP.NET MVC】使用动软(五)(13)

一、问题 前文完成的用户登录后的首页如下&#xff1a; 后续账单管理、人员管理等功能页面都有相同的头部&#xff0c;左边和下边&#xff0c;唯一不同的右边内容部分&#xff0c;所以要解决重复设计的问题。 二、解决方法——使用布局页 在Views上右键添加新建项&#xff…

鸿蒙4.0发布会说了啥?关注个性与效率,小艺智能程度令人惊艳

鸿蒙4.0系统的发布会已经结束&#xff0c;整个发布会看下来&#xff0c;给我最深刻的印象就是——鸿蒙4.0是一个让手机更接近个人终端的系统。但选择系统难免掺杂个人喜好和偏见&#xff0c;因此本文我只会从鸿蒙4.0那些让我感到惊喜的功能入手介绍&#xff0c;不对系统进行评价…

【深度学习Week4】MobileNet_ShuffleNet

报错&#xff1a;unsafe legacy renegotiation disabled 解决方案&#xff1a; 尝试了更换cryptography36.0.2版本&#xff0c;以及更换下载链接的方法&#xff0c;都不行&#xff0c;最后采用了手动下载mat文件并上传到colab的方法 高光谱图像分类数据集简介Indian Pines&…

免疫疗法勘察兵——DC细胞

DC细胞又叫树状细胞或者树突细胞&#xff0c;1869年由保罗兰格尔翰斯发现&#xff0c;一开始被误以为是神经细胞的一种&#xff0c;直到1973年皮肤科医师Inga Silberberg发现了他的免疫功能&#xff0c;同年&#xff0c;被拉尔夫斯坦曼和赞威尔A科恩两人正式命名为“dendritic…

《凤凰架构》第一章——演进中的部分

前言 刚开始决定弄懂文中所提到的所有东西&#xff0c;就像我写ByteByteGo呢几篇文章一样&#xff0c;把每一句话都弄懂。但是对于《凤凰架构》来说&#xff0c;这有点太费时间了&#xff0c;并且没有必要&#xff0c;有些东西可能永远都不会用到&#xff0c;但文章为了全面的…

【基础类】—CSS盒模型的全面认识

一、基本概念&#xff1a;标准IE模型 盒模型&#xff1a;margin border padding content 标准模型&#xff1a;将元素的宽度和高度仅计算为内容区域的尺寸&#xff08;content-box&#xff0c;默认&#xff09; 当CSS盒模型为 标准盒模型 &#xff08;box-sizing: conten…

【安全测试】安全测试威胁建模设计方法STRIDE

目录 背景 TM(ThreatModeling) 实践 具体流程 资料获取方法 背景 目前安全测试一般都存在如下问题&#xff1a; 安全测试人员不懂业务&#xff0c;业务测试人员不懂安全&#xff0c;安全测试设计出现遗漏是无法避免的安全测试点繁多复杂&#xff0c;单点分析会导致风险暴…

商城-学习整理-基础-商品服务API-品牌管理(六)

目录 一、使用逆向工程生成前后端代码1、新增品牌管理菜单2、使用生成的前端代码 二、优化逆向生成的品牌管理页面1、显示状态开关优化2、品牌上传优化&#xff08;使用阿里云存储&#xff09;1&#xff09;阿里云对象存储-普通上传方式2&#xff09;阿里云对象存储-服务端签名…

paddlenlp:社交网络中多模态虚假媒体内容核查(代码篇)

初赛之baseline解读篇 一、模型框架图1、框架解读2、评价指标解读 二、代码功能1、数据集加载2、模型定义3、模型训练4、模型预测 三、写在最后 一、模型框架图 1、框架解读 第一列是输入&#xff0c;一部分是文本&#xff08;需核查文本、文本证据材料&#xff09;&#xff…

ExtJs 7.7.0 下载方法与去除trial水印

背景 最近发现Sencha ExtJs发布了ExtJs7.7.0版本&#xff0c;立刻下载了SDK包&#xff0c;许多朋友不知如何下载&#xff0c;如何去除右上角的trial水印。本文讲下相关下载技巧与方法。 下载SDK 首先需要申请试用&#xff0c;申请地址如下&#xff0c;需要注意可能需要梯子&…

好烦!快让ChatGPT停止道歉!SD创作宣传图的超细教程;教你在PH冷启动薅流量;CSDN举办AI应用创新大赛 | ShowMeAI日报

&#x1f440;日报&周刊合集 | &#x1f3a1;生产力工具与行业应用大全 | &#x1f9e1; 点赞关注评论拜托啦&#xff01; &#x1f916; Stable Diffusion 图生图入门&#xff0c;一份详细的思维导图 &#x1f916; DeeCamp X CSDN 举办AI应用创新大赛&#xff0c;万元奖金…

【C++】继承的基本特性(定义,赋值转换,友元,静态成员,虚拟继承,默认成员函数,作用域)

文章目录 一、继承的定义1.定义格式2.继承基类成员访问方式的变化 二、基类和派生类对象赋值转换三、继承的作用域1. 在继承体系中基类和派生类都有独立的作用域。2.子类和父类中有同名成员3.成员函数的隐藏4.注意在实际中在继承体系里面最好不要定义同名的成员。 四、派生类的…

【C语言学习】整数类型表达数的范围

一、整数类型表达数的范围 1.char类型 char 是1个字节 &#xff0c;即00000000 ~ 11111111&#xff0c;一般情况默认是有符号char(signed char) ,此时char所能表达的数就是 -128 ~ 127&#xff0c;即 -2 ^ n-1 ~ (2 ^ n-1)-1 ,其中n是位数或比特位&#xff08;1字节8位8比特&…

OPENCV C++(四)形态学操作+连通域统计

形态学操作 先得到一个卷积核 Mat kernel getStructuringElement(MORPH_RECT,Size(5,5)); 第一个是形状 第二个是卷积核大小 依次为腐蚀 膨胀 开运算 闭运算 Mat erodemat,dilatemat,openmat,closemat;morphologyEx(result1, erodemat, MORPH_ERODE, kernel);morphologyEx…

万界星空科技/免费开源MES系统/免费仓库管理

仓库管理&#xff08;仓储管理&#xff09;&#xff0c;指对仓库及仓库内部的物资进行收发、结存等有效控制和管理&#xff0c;确保仓储货物的完好无损&#xff0c;保证生产经营活动的正常进行&#xff0c;在此基础上对货物进行分类记录&#xff0c;通过报表分析展示仓库状态、…

道本科技受邀参加建筑产业互联网推动建筑产业现代化体系构建座谈会,以数字化产品为建筑行业注入新动能!

2023年7月底&#xff0c;道本科技作为中国建筑业协会合作伙伴&#xff0c;受邀参加了建筑产业互联网推动建筑产业现代化体系构建座谈会。在这次座谈会上&#xff0c;道本科技旗下产品“合规数”“合同智能审查”和“智合同范本库”被中国建筑&#xff08;中小企业&#xff09;产…