• 《QT实用小工具·四十一》无边框窗口


    1、概述
    源码放在文章末尾

    该项目实现了无边框窗口效果,项目demo如下所示:
    在这里插入图片描述

    项目代码如下所示:

    #include "framelesswindow.h"
    #include 
    #include 
    
    #ifdef Q_OS_WIN
    #include 
    #endif
    
    FramelessWindow::FramelessWindow(QWindow *parent)
        : QQuickWindow (parent)
    {
        setFlags(flags() | Qt::FramelessWindowHint);
    
    #ifdef Q_OS_WIN
        HWND hwnd = reinterpret_cast<HWND>(winId());
        LONG style = GetWindowLong(hwnd, GWL_STYLE);
        SetWindowLongPtr(hwnd, GWL_STYLE, style | WS_MAXIMIZEBOX | WS_THICKFRAME | WS_CAPTION);
    #endif
        //在这里改变默认移动区域
        //只有鼠标在移动区域内,才能移动窗口
        connect(this, &QQuickWindow::widthChanged, this, [this](int arg){
            m_moveArea.setWidth(arg - 16);
        });
    }
    
    bool FramelessWindow::resizable() const
    {
        return m_resizable;
    }
    
    void FramelessWindow::setResizable(bool resizable)
    {
        if (m_resizable != resizable) {
            m_resizable = resizable;
            emit resizableChanged();
        }
    }
    
    bool FramelessWindow::nativeEvent(const QByteArray &eventType, void *message, long *result)
    {
    #ifdef Q_OS_WIN
        MSG* msg = reinterpret_cast<MSG*>(message);
        switch(msg->message) {
        case WM_NCCALCSIZE:
        {
            *result = 0;
            return true;
        }
        case WM_NCHITTEST:
        {
            auto x = GET_X_LPARAM(msg->lParam) - this->x();
            auto y = GET_Y_LPARAM(msg->lParam) - this->y();
            auto w = width();
            auto h = height();
    
            if (m_resizable) {
                if (x >= 0 && x <= 8 && y >= 0 && y <= 8) {
                    *result = HTTOPLEFT;
                    return true;
                } else if (x > 8 && x < (w - 8) && y >= 0 && y <= 8) {
                    *result = HTTOP;
                    return true;
                } else if (x >=(w - 8) && x <= w && y >= 0 && y <= 8) {
                    *result = HTTOPRIGHT;
                    return true;
                } else if (x >= 0 && x <= 8 && y > 8 && y < (h - 8)) {
                    *result = HTLEFT;
                    return true;
                } else if (x >=(w - 8) && x <= w && y > 8 && y < (h - 8)) {
                    *result = HTRIGHT;
                    return true;
                } else if (x >= 0 && x <= 8 && y >= (h - 8) && y <= h) {
                    *result = HTBOTTOMLEFT;
                    return true;
                } else if (x > 8 && x < (w - 8) && y >= (h - 8) && y <= h) {
                    *result = HTBOTTOM;
                    return true;
                } else if (x >=(w - 8) && x <= w && y >= (h - 8) && y <= h) {
                    *result = HTBOTTOMRIGHT;
                    return true;
                }
            }
    
            if (m_moveArea.contains(x, y)){
                *result = HTCAPTION;
                return true;
            } else {
                *result = HTCLIENT;
                return true;
            }
        }
        default:
            break;
        }
    #endif
    
        return QQuickWindow::nativeEvent(eventType, message, result);
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    源码下载

  • 相关阅读:
    世界互联网大会|美创新品发布—流动安全管控平台
    Kotlin进阶指南 - Parcelable序列化
    【coding加油站】人事管理系统---毕设
    GO语言的由来与发展历程
    GD32 单片机 硬件I2C死锁解决方法
    python作业
    9. 成功解决:Driver class ‘org.gjt.mm.mysql.Driver‘ could not be found
    程序人生:从小公司到一线大厂,薪资翻倍,我做到了...
    文本编辑器去除PDF水印
    VBM计算操作过程记录
  • 原文地址:https://blog.csdn.net/cs1395293598/article/details/138198545