#include "loqmainwindow.h" #include "QtMath" LoQMainWindow::LoQMainWindow(QWidget *parent) : QMainWindow(parent), m_winMove(false) { setAttribute(Qt::WA_TranslucentBackground,true); setWindowFlags(Qt::FramelessWindowHint | windowFlags()); setMouseTracking(true); // ui->centralWidget->setMouseTracking(true); } void LoQMainWindow::mousePressEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { QRect gRect = this->geometry(); gRect.setWidth(gRect.width() - 10); gRect.setHeight(gRect.height() - 10); gRect.setX(gRect.x() + 5); gRect.setY(gRect.y() + 5); QPoint pointXiangDui = event->pos(); if((pointXiangDui.y()<100&&pointXiangDui.x()>5)&&(pointXiangDui.y()<100&&pointXiangDui.x()globalPos())) { m_winMove = true; m_winOrgPoint = event->globalPos(); m_winWorldPoint = this->frameGeometry().topLeft(); } else { // Resize the window } } else { m_winMove = false; m_iCalCursorPos = CalCursorPos(event->pos(),CalCursorCol(event->pos())); if (event->button() == Qt::LeftButton /*&& Qt::WindowMaximized != windowState()*/) { if(m_iCalCursorPos != CENTER) { m_bLeftPress = true; } } m_rtPreGeometry = geometry(); m_ptViewMousePos = event->globalPos(); } } } void LoQMainWindow::mouseMoveEvent(QMouseEvent *event) { if(m_winMove) { if (event->buttons() & Qt::LeftButton) { QPoint relativePos = event->globalPos() - m_winOrgPoint; this->move(m_winWorldPoint + relativePos ); } } else { //窗体不是最大的话就改变鼠标的形状 if(Qt::WindowMaximized != windowState()) { setCursorShape(CalCursorPos(event->pos(),CalCursorCol(event->pos()))); } else { return; } //获取当前的点,这个点是全局的 QPoint ptCurrentPos = QCursor::pos(); //计算出移动的位置,当前点 - 鼠标左键按下的点 QPoint ptMoveSize = ptCurrentPos - m_ptViewMousePos; QRect rtTempGeometry = m_rtPreGeometry; if(m_bLeftPress) { switch(m_iCalCursorPos) { case TOPLEFT: rtTempGeometry.setTopLeft(m_rtPreGeometry.topLeft()+ptMoveSize); break; case TOP: rtTempGeometry.setTop(m_rtPreGeometry.top()+ptMoveSize.y()); break; case TOPRIGHT: rtTempGeometry.setTopRight(m_rtPreGeometry.topRight()+ptMoveSize); break; case LEFT: rtTempGeometry.setLeft(m_rtPreGeometry.left()+ptMoveSize.x()); break; case RIGHT: rtTempGeometry.setRight(m_rtPreGeometry.right()+ptMoveSize.x()); break; case BUTTOMLEFT: rtTempGeometry.setBottomLeft(m_rtPreGeometry.bottomLeft()+ptMoveSize); break; case BUTTOM: rtTempGeometry.setBottom(m_rtPreGeometry.bottom()+ptMoveSize.y()); break; case BUTTOMRIGHT: rtTempGeometry.setBottomRight(m_rtPreGeometry.bottomRight()+ptMoveSize); break; default: break; } //移动窗体,如果比最小窗体大,就移动 if(rtTempGeometry.width() >= 200 && rtTempGeometry.height() >= 300) setGeometry(rtTempGeometry); } } } void LoQMainWindow::mouseReleaseEvent(QMouseEvent *event) { if (event->button() == Qt::LeftButton) { m_winMove = false; } m_bLeftPress = false; setCursor(Qt::ArrowCursor); } void LoQMainWindow::paintEvent(QPaintEvent *) { QStyleOption opt; opt.init(this); QPainter p(this); style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this); } int LoQMainWindow::CalCursorCol(QPoint pt) { return (pt.x() < FRAMESHAPE ? 1 : ((pt.x() > this->width() - FRAMESHAPE) ? 3 : 2)); } int LoQMainWindow::CalCursorPos(QPoint pt, int colPos) { return ((pt.y() < FRAMESHAPE ? 10 : ((pt.y() > this->height() - FRAMESHAPE) ? 30 : 20)) + colPos); } void LoQMainWindow::setCursorShape(int CalPos) { Qt::CursorShape cursor; switch(CalPos) { case TOPLEFT: case BUTTOMRIGHT: cursor = Qt::SizeFDiagCursor; break; case TOPRIGHT: case BUTTOMLEFT: cursor = Qt::SizeBDiagCursor; break; case TOP: case BUTTOM: cursor = Qt::SizeVerCursor; break; case LEFT: case RIGHT: cursor = Qt::SizeHorCursor; break; default: cursor = Qt::ArrowCursor; break; } setCursor(cursor); }