qt重写事件函数实现无边框窗体移动和缩放

头文件包含
#include <WinSock2.h>
#include <windows.h>
#include <QMainWindow>
#include <QFile>
#include <QtCore>
#include <QtGui>
#include <QMenu>
#include <QDesktopWidget>
#ifdef Q_OS_WIN
#include <qt_windows.h>
#include <Windowsx.h>
#endif

重写事件函数

通过重写mousePressEvent、mouseReleaseEvent、mouseMoveEvent控制鼠标按下、移动和释放,实现对窗口的移动效果。通过重写nativeEvent来监听windows的WM_NCHITTEST消息,实现对无边框窗体的缩放效果。

/*************************
 * 鼠标左键被按下
 * ***********************/
void MyCommonUI::mousePressEvent(QMouseEvent *event)
{
    if(event->button() == Qt::LeftButton)
    {
        m_bMousePress = true;
        m_ptMove = event->pos();
    }
    return QWidget::mousePressEvent(event);
}

/*************************
 * 鼠标左键按住拖动
 * ***********************/
void MyCommonUI::mouseMoveEvent(QMouseEvent *event)
{
    //若鼠标左键被按下
    if(m_bMousePress)
    {
        //鼠标相对于屏幕的位置
        QPoint move_pos = event->globalPos();
        //移动主窗体位置
         this->move(move_pos - m_ptMove);
    }
    event->accept();
    return QWidget::mouseMoveEvent(event);
}

/*************************
 * 释放鼠标
 * ***********************/
void MyCommonUI::mouseReleaseEvent(QMouseEvent *event)
{
    m_bMousePress = false;
    return QWidget::mouseReleaseEvent(event);
}


/*************************
 * nativeEvent获取windows的事件处理,为了让无边框窗体还能正常的缩放
 * ***********************/
bool MyCommonUI::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
    int m_nBorder = 5;
    Q_UNUSED(eventType)
    MSG *param = static_cast<MSG *>(message);

    switch (param->message)
    {
        case WM_NCHITTEST:
        {
            int nX = GET_X_LPARAM(param->lParam) - this->geometry().x();
            int nY = GET_Y_LPARAM(param->lParam) - this->geometry().y();

            *result = HTCAPTION;

            // if mouse in the border of mainframe,we zoom mainframe
            if ((nX > 0) && (nX < m_nBorder))
                *result = HTLEFT;

            if ((nX > this->width() - m_nBorder) && (nX < this->width()))
                *result = HTRIGHT;

            if ((nY > 0) && (nY < m_nBorder))
                *result = HTTOP;

            if ((nY > this->height() - m_nBorder) && (nY < this->height()))
                *result = HTBOTTOM;

            if ((nX > 0) && (nX < m_nBorder) && (nY > 0)
                && (nY < m_nBorder))
                *result = HTTOPLEFT;

            if ((nX > this->width() - m_nBorder) && (nX < this->width())
                && (nY > 0) && (nY < m_nBorder))
                *result = HTTOPRIGHT;

            if ((nX > 0) && (nX < m_nBorder)
                && (nY > this->height() - m_nBorder) && (nY < this->height()))
                *result = HTBOTTOMLEFT;

            if ((nX > this->width() - m_nBorder) && (nX < this->width())
                && (nY > this->height() - m_nBorder) && (nY < this->height()))
                *result = HTBOTTOMRIGHT;

            if (*result == HTCAPTION)
            {
                return false;
            }
            return true;
        }
    }
    return QWidget::nativeEvent(eventType, message, result);
}

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浅笑一斤

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值