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

这篇具有很好参考价值的文章主要介绍了[Qt]FrameLessWindow实现调整大小、移动弹窗并具有Aero效果。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

说明

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

代码

大部分参考这个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);
}
测试代码

生成一个类,继承上面的类。然后实现下面的内容。很简单:文章来源地址https://www.toymoban.com/news/detail-632495.html

#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效果的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【WPF】弹出一个弹窗并显示一个列表

    要在WPF中弹出一个弹窗并显示一个列表,你可以使用 Popup 元素和一个包含列表的控件,如 ListBox 或 ListView 。下面是一个示例: 在这个示例中,我们在 Grid 中放置了一个 Button 和一个 Popup 。 Popup 的 PlacementTarget 属性被设置为 Button ,这将使得弹窗相对于按钮进行定位。 Popup 的

    2024年02月04日
    浏览(22)
  • 【AHK】鼠标调整窗口大小/移动窗口位置/窗口置顶/透明度

    通过AHK,设置窗体大小或位置,首先是两种鼠标调节窗体方案,后面是快捷键,注意修改快捷键的位置有两个,仿照Ubuntu调节窗体的快捷键 Alt+F7、Alt+F8,个人感觉比要按着ALT调节的小工具要好用一点。 如果换成单个F7、F8触发,只需要将多出的GetKeyState删除即可,具体函数意

    2024年02月12日
    浏览(31)
  • QT学习笔记:调整控件大小和位置

    前面的文章,我讲了怎么用layout去布局。但布局做完后,发现界面有点怪。比如,最低下的“清除”按钮这么大,“消息体”这个label没有位于中间等。下面,我就来讲下怎么把界面继续优化。 1、调整“清除”按钮大小和位置 (1)在“sizePolicy”中,选择Fixed。 (2)把左边

    2024年02月12日
    浏览(45)
  • [Qt] 怎么将Widget调整为自适应大小?

    怎么将Widget调整为自适应大小? 要将Qt中的Widget调整为自适应大小,可以通过以下步骤实现: 为Widget设置自适应大小属性:将QWidget的sizePolicy设置为QSizePolicy::Expanding,可以使Widget在布局中自适应大小,如下所示:

    2024年02月01日
    浏览(32)
  • 【Qt】根据界面所在显示器自适应调整ui大小

    使用QDesktopWidget、QApplication::screens()等获取屏幕宽高、DPI等信息,详见上一篇概述。 我们需要将窗口、布局和控件的大小类型(size type)设置为相对单位,如: 设置窗口的尺寸策略为Qt::SizePolicy::Expanding 使用setBaseSize() + setSizeIncrement() 使窗口大小可根据屏幕比例增长 使用 percentages 而

    2023年04月26日
    浏览(31)
  • VS+Qt设置窗口尺寸(二):窗体控件自适应窗口布局,自动调整大小

    VS版本:VS2019 QT版本:Qt5.12.3(msvc2017_64) 为了适配不同尺寸的显示屏,软件窗口需要调整大小,窗口内的控件尺寸也要适配窗口的大小。 本例重点讲述如何设置可调整尺寸的窗口及控件,实现窗口最大化和尺寸调节。 本例使用相对简单的按键和文本框来做示例,其他控件均可

    2023年04月23日
    浏览(65)
  • ‘xxx‘ “将对您的电脑造成伤害。 您应该将它移到废纸篓。”mac一直弹窗并关不掉的解决方式

    10.15.1之后的系统: 在终端输入:csrutil status 如果返回值为: System Integrity Protection status: enabled 说明MAC系统SIP保护开启了,关闭SIP保护即可 重新启动MAC电脑,重启的时候按command+r,进入恢复环境,在恢复环境打开终端 输入csrutil disable 重新启动系统后,再次输入csrutil status,应

    2024年02月13日
    浏览(36)
  • java实现音乐播放器(调整显示音量大小、调整进度、图片切换)

    上学期老师布置了一个音乐播放器的作业,自己独立写的界面感觉还行就传上来了。 然后底下是主函数 关于进度条进度的问题,可以利用计时器统计当前已经播放的时间。然后利用函数计算音乐播放的总时间。两者相除就能得出当前的进度了。

    2024年02月12日
    浏览(29)
  • echarts系列-带图教你调整左右位置x轴样式网格虚线刻度居中双轴Y轴滚动上下移动文字旋转改分割线颜色部分字体改色折注混合,X轴的颜色,X轴字体颜色,调整柱子颜色,调整小图标图例的大小和位置,鼠标

    本文已参与「新人创作礼」活动,一起开启掘金创作之路。 看图 欢迎大家指出文章需要改正之处~ 学无止境,合作共赢 欢迎路过的小哥哥小姐姐们提出更好的意见哇~~

    2024年02月10日
    浏览(30)
  • C# wpf 附加属性实现任意控件拖动调整大小

    第一节 Grid内控件拖动调整大小 第二节 Canvas内控件拖动调整大小 第三节 窗口拖动调整大小 第四节 附加属性实现拖动调整大小(本章) 第五章 拓展更多调整大小功能 前面几节讲了控件拖动改变大小的几种方法,根据不同的布局可以有不同的实现方式。本节主要讲的是利用

    2024年02月11日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包