展示:
代码实现:
// 鼠标按下事件(记录拉伸窗口或移动窗口时的起始坐标(左上角))
void framelessWidget::mousePressEvent(QMouseEvent *event)
{
if(event->button() == Qt::LeftButton){
mousePressed = true;
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
lastPos = event->globalPosition().toPoint() - this->frameGeometry().topLeft();
#else
lastPos = event->globalPos() - this->frameGeometry().topLeft();
#endif
}
}
// 鼠标移动事件
void framelessWidget::mouseMoveEvent(QMouseEvent *event)
{
if(event->buttons() == Qt::NoButton)
mousePressed = false;
if(!mousePressed){ // 鼠标没按下移动,更新鼠标位置状态
mouseState = 0;
if(!maximized && abs(event->pos().x() - ui->mainWidget->pos().x()) < 5)
mouseState |= AT_LEFT;
if(!maximized && abs(event->pos().y() - ui->mainWidget->pos().y()) < 5)
mouseState |= AT_TOP;
if(!maximized && abs(event->pos().x() - ui->mainWidget->pos().x() - ui->mainWidget->width()) < 5)
mouseState |= AT_RIGHT;
if(!maximized && abs(event->pos().y() - ui->mainWidget->pos().y() - ui->mainWidget->height()) < 5)
mouseState |= AT_BOTTOM;
if(mouseState == AT_TOP_LEFT || mouseState == AT_BOTTOM_RIGHT)
setCursor(Qt::SizeFDiagCursor);
else if(mouseState == AT_TOP_RIGHT || mouseState == AT_BOTTOM_LEFT)
setCursor(Qt::SizeBDiagCursor);
else if(mouseState & (AT_LEFT | AT_RIGHT))
setCursor(Qt::SizeHorCursor);
else if(mouseState & (AT_TOP | AT_BOTTOM))
setCursor(Qt::SizeVerCursor);
else
unsetCursor();
}
else{ //鼠标按下移动
if(mouseState == 0){
if(maximized){
qreal wRatio = (double)event->pos().x() / (double)ui->mainWidget->width();
controlWindowScale();
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
this->move(QPoint(event->globalPosition().x() - ui->mainWidget->width() * wRatio, -30));
#else
this->move(QPoint(event->globalPos().x() - ui->mainWidget->width() * wRatio, -30));
#endif
lastPos = QPoint(ui->mainWidget->width() * wRatio, event->pos().y());
}
else
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
this->move(event->globalPosition().toPoint() - lastPos);
#else
this->move(event->globalPos() - lastPos);
#endif
}
else{
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
QPoint d = event->globalPosition().toPoint() - frameGeometry().topLeft() - lastPos;
#else
QPoint d = event->globalPos() - frameGeometry().topLeft() - lastPos;
#endif
if(mouseState & AT_LEFT){
this->move(this->frameGeometry().x() + d.x(), this->frameGeometry().y());
this->resize(this->width() - d.x(), this->height());
}
if(mouseState & AT_RIGHT){
this->resize(this->width() + d.x(), this->height());
}
if(mouseState & AT_TOP){
this->move(this->frameGeometry().x(), this->frameGeometry().y() + d.y());
this->resize(this->width(), this->height() - d.y());
}
if(mouseState & AT_BOTTOM){
this->resize(this->width(), this->height() + d.y());
}
}
#if (QT_VERSION >= QT_VERSION_CHECK(6,0,0))
lastPos = event->globalPosition().toPoint() - this->frameGeometry().topLeft();
#else
lastPos = event->globalPos() - this->frameGeometry().topLeft();
#endif
}
}
// 窗口变化事件
void framelessWidget::resizeEvent(QResizeEvent *event)
{
//Resize border
if(border)
border->resize(ui->mainWidget->size() + QSize(2, 2));
//Resize mask
QPainterPath path;
#ifdef Q_OS_WINDOWS
path.addRoundedRect(ui->mainWidget->rect(), cornerRadius - 1, cornerRadius - 1);
#else
path.addRect(ui->mainWidget->rect());
#endif
QRegion mask(path.toFillPolygon().toPolygon());
ui->mainWidget->setMask(mask);
//Resize all pages
// for(int i = 0; i < pageList.size(); i++){
// pageList[i]->resize(ui->mainWidget->width() * 0.4 < pageList[i]->preferWidth ? pageList[i]->preferWidth - 1 : ui->mainWidget->width() * 0.4 - 1, ui->mainWidget->height());
// pageList[i]->resize(pageList[i]->width() + 1, pageList[i]->height());
// }
}
//点击最大化按钮事件
void framelessWidget::controlWindowScale(){
#ifdef Q_OS_WINDOWS
if(!maximized){
lastGeometry = this->frameGeometry();
// windowShadow->setEnabled(false);
ui->verticalLayout->setContentsMargins(0, 0, 0, 0);
border->hide();
QString mainStyle = "QWidget#mainWidget{background-color:rgb(251,251,251) border-radius:0px;}";
ui->mainWidget->setStyleSheet(mainStyle);
this->showMaximized();
maximized = true;
QPainterPath path;
path.addRect(ui->mainWidget->rect());
QRegion mask(path.toFillPolygon().toPolygon());
ui->mainWidget->setMask(mask);
}
else{
ui->verticalLayout->setContentsMargins(30, 30, 30, 30);
this->showNormal();
QString mainStyle = "QWidget#mainWidget{background-color:rgb(251,251,251)" + QString::asprintf(";border-radius:%dpx", cornerRadius) + "}";
ui->mainWidget->setStyleSheet(mainStyle);
QPainterPath path;
path.addRoundedRect(ui->mainWidget->rect(), cornerRadius - 1, cornerRadius - 1);
QRegion mask(path.toFillPolygon().toPolygon());
ui->mainWidget->setMask(mask);
border->show();
// windowShadow->setEnabled(true);
this->resize(lastGeometry.width(), lastGeometry.height());
this->move(lastGeometry.x(), lastGeometry.y());
maximized = false;
}
#endif
}
三连可分享全部源码哦