Qt边框隐藏后 双击标题栏窗口最大化 标题栏左击移动窗口 横向拉动窗口 竖向拉动窗口

#pragma once
#include<QMainWindow>
class CMainWindow : public QMainWindow
{
    Q_OBJECT


public:
    CMainWindow(QMainWindow *parent = 0);
    ~CMainWindow();


private:
    void initMainWindow();
    void initWidget();
    void assembleWidget();


    enum tagCursorCtrlStyle
    {
        eCursorNormal = 0,    // 普通鼠标  
        eCursorHor,           // 水平拉伸  
        eCursorVer,           // 垂直拉伸  
        eCursorHorVer         // 水平和垂直拉伸  
    };


    // custom title bar  
    void mousePressEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseReleaseEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseMoveEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void mouseDoubleClickEvent(QMouseEvent *event) Q_DECL_OVERRIDE;
    void maxRestoreSlot();
    int setCursorStyle(const QPoint& curPoint);    // 当前位置设置鼠标样式  


    QPoint m_MousePressPos;    // 鼠标点击位置  
    QPoint m_MouseMovePos;     // 鼠标移动位置  
    bool   m_bMousePressed;    // 鼠标是否按下  
    int    m_nMouseResize;     // 鼠标设置大小  
    bool   m_bMouseResizePressed;  // 设置大小的按下  
    QPoint m_ResizePressPos;   // 设置大小鼠标按下的点
    QRect *rect;  //鼠标点击的有效区域
    QRect clientRect;   //用户可用窗口大小
    bool isMaxWindow = {false};


                               // title  
    QWidget*         m_pTitleWidget;    // 标题栏  

};

#include "CMainWindow.h"  
#include<QMouseEvent>
#include<QApplication>
#include<QDesktopWidget>


CMainWindow::CMainWindow(QMainWindow *parent)
    : QMainWindow(parent)
    , m_bMousePressed(false)
    , m_nMouseResize(eCursorNormal)
    , m_bMouseResizePressed(false)
    , m_pTitleWidget(NULL)
{
    clientRect = QApplication::desktop()->availableGeometry(); //用户可用窗口大小
    rect = new QRect(0, 0, clientRect.width(), 100); // 鼠标点击的有效区域
    initMainWindow();
    initWidget();
    assembleWidget();
    QWidget* my_Widget = new QWidget();
    my_Widget->setStyleSheet("background-color:blue");
   // setCentralWidget(my_Widget);
    my_Widget->move(5, -40);
   
}


CMainWindow::~CMainWindow()
{
}


void CMainWindow::initMainWindow()
{
    setWindowFlags(Qt::FramelessWindowHint);    // 设置窗口标志  
    setMinimumSize(600, 400);    // 设置最小尺寸  
    setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);    // 设置尺寸属性  
    setMouseTracking(true);    // 界面拉伸需要这个属性  
}


void CMainWindow::initWidget()
{
    m_pTitleWidget = new QWidget(this);
    //m_pTitleWidget->setStyleSheet("background-color:red");
}


void CMainWindow::assembleWidget()
{
    m_pTitleWidget->setFixedHeight(40);
    m_pTitleWidget->setMinimumWidth(600);
    m_pTitleWidget->setStyleSheet("background-color:red");
    m_pTitleWidget->move(0, 0);
}


void CMainWindow::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton &&
        m_pTitleWidget->rect().contains(event->globalPos() - this->frameGeometry().topLeft()))
    {
        m_MousePressPos = event->globalPos();
        m_bMousePressed = true;
    }


    if (eCursorNormal != m_nMouseResize)
    {
        m_bMouseResizePressed = true;
        m_ResizePressPos = event->pos();
    }
    event->ignore();    //表示继续向下传递事件,其他的控件还可以去获取  
}


void CMainWindow::mouseReleaseEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
    {
        m_bMousePressed = false;
        m_bMouseResizePressed = false;
        m_ResizePressPos.setX(0);
        m_ResizePressPos.setY(0);
        m_nMouseResize = setCursorStyle(event->pos());
    }
    event->ignore();
}


void CMainWindow::mouseMoveEvent(QMouseEvent *event)
{
    if (m_bMousePressed)
    {
        m_MouseMovePos = event->globalPos();
        this->move(this->pos() + m_MouseMovePos - m_MousePressPos);
        m_MousePressPos = m_MouseMovePos;
        return;
    }


    QPoint curPoint = event->pos();


    if (!m_bMouseResizePressed)
    {
        m_nMouseResize = setCursorStyle(curPoint);
    }


    if (m_bMouseResizePressed && !m_ResizePressPos.isNull())
    {
        switch (m_nMouseResize)
        {
        case eCursorHor:
            this->resize(curPoint.x(), this->height());
            break;
        case eCursorVer:
            this->resize(this->width(), curPoint.y());
            break;
        case eCursorHorVer:
            this->resize(curPoint.x(), curPoint.y());
            break;
        default:
            break;
        }
    }


    event->ignore();
}


void CMainWindow::mouseDoubleClickEvent(QMouseEvent *event)
{
    if (rect->contains(this->mapFromGlobal(QCursor::pos())) == true) // 指定区域内
        maxRestoreSlot();
}


void CMainWindow::maxRestoreSlot()


{
    if (isMaxWindow) {
        resize(600, 400);
        move((clientRect.width() - this->width()) / 2, (clientRect.height() - this->height()) / 2); // 居中
        isMaxWindow = false;
    }
    else
    {
        resize(clientRect.width(), clientRect.height());
        move(0, 0);
        isMaxWindow = true;
    }
}


int CMainWindow::setCursorStyle(const QPoint& curPoint)
{
    int nCurWidth = this->width();
    int nCurHeight = this->height();
    int nRes = eCursorNormal;


    if ((nCurWidth - curPoint.x() <= 3) && (nCurHeight - curPoint.y() <= 3))
    {
        setCursor(Qt::SizeFDiagCursor);
        nRes = eCursorHorVer;
    }
    else if (nCurWidth - curPoint.x() <= 3)
    {
        setCursor(Qt::SizeHorCursor);
        nRes = eCursorHor;
    }
    else if (nCurHeight - curPoint.y() <= 3)
    {
        setCursor(Qt::SizeVerCursor);
        nRes = eCursorVer;
    }
    else
    {
        setCursor(Qt::ArrowCursor);
        nRes = eCursorNormal;
    }


    return nRes;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值