![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/517df4c2cb77b1f73c17221dd1b3fd78.gif#pic_center)
QPoint dragPosition;
int edgeMargin;
enum {nodir,
top = 0x01,
bottom = 0x02,
left = 0x04,
right = 0x08,
topLeft = 0x01 | 0x04,
topRight = 0x01 | 0x08,
bottomLeft = 0x02 | 0x04,
bottomRight = 0x02 | 0x08} resizeDir;
void testEdge();
protected:
void mousePressEvent(QMouseEvent*event);
void mouseMoveEvent(QMouseEvent*event);
void mouseReleaseEvent(QMouseEvent*event);
edgeMargin = 5;
resizeDir = nodir;
setWindowFlags(Qt::FramelessWindowHint);
setMouseTracking(true);
setMinimumSize(600,510);
QBitmap bmp(this->size());
bmp.fill();
QPainter p(&bmp);
p.setPen(Qt::NoPen);
p.setBrush(Qt::black);
p.drawRoundedRect(bmp.rect(), 15, 15);
setMask(bmp);
void MainWindow::mousePressEvent(QMouseEvent *event)
{
qDebug() << "mousePressEvent" <<endl;
event->ignore();
if (event->button() == Qt::LeftButton)
{
dragPosition = event->globalPos() - frameGeometry().topLeft();
}
}
void MainWindow::testEdge()
{
qDebug() << "testEdge" <<endl;
int diffLeft = abs(cursor().pos().x() - frameGeometry().left());
int diffRight = abs(cursor().pos().x() - frameGeometry().right());
int diffTop = abs(cursor().pos().y() - frameGeometry().top());
int diffBottom = abs(cursor().pos().y() - frameGeometry().bottom());
qDebug()<< "左:" << diffLeft << "右:" << diffRight << "上:"<< diffTop << "下:"<< diffBottom;
QCursor tempCursor;
tempCursor = cursor();
if(diffTop < edgeMargin){
if(diffLeft < edgeMargin){
resizeDir = topLeft;
tempCursor.setShape(Qt::SizeFDiagCursor);
}
else if(diffRight < edgeMargin){
resizeDir = topRight;
tempCursor.setShape(Qt::SizeBDiagCursor);
}
else{
resizeDir = top;
tempCursor.setShape(Qt::SizeVerCursor);
}
}
else if(diffBottom < edgeMargin){
if(diffLeft < edgeMargin){
resizeDir = bottomLeft;
tempCursor.setShape(Qt::SizeBDiagCursor);
}
else if(diffRight < edgeMargin){
resizeDir = bottomRight;
tempCursor.setShape(Qt::SizeFDiagCursor);
}
else{
resizeDir = bottom;
tempCursor.setShape(Qt::SizeVerCursor);
}
}
else if(diffLeft < edgeMargin){
resizeDir = left;
tempCursor.setShape(Qt::SizeHorCursor);
}
else if(diffRight < edgeMargin){
resizeDir = right;
tempCursor.setShape(Qt::SizeHorCursor);
}
else{
resizeDir = nodir;
tempCursor.setShape(Qt::ArrowCursor);
}
setCursor(tempCursor);
}
void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
qDebug() << "mouseMoveEvent" <<endl;
event->ignore();
if (event->buttons() & Qt::LeftButton)
{
if(resizeDir == nodir){
move(event->globalPos() - dragPosition);
}
else{
int ptop,pbottom,pleft,pright;
ptop = frameGeometry().top();
pbottom = frameGeometry().bottom();
pleft = frameGeometry().left();
pright = frameGeometry().right();
if(resizeDir & top){
if(height() == minimumHeight()){
ptop = min(event->globalY(),ptop);
}
else if(height() == maximumHeight()){
ptop = max(event->globalY(),ptop);
}
else{
ptop = event->globalY();
}
}
else if(resizeDir & bottom){
if(height() == minimumHeight()){
pbottom = max(event->globalY(),ptop);
}
else if(height() == maximumHeight()){
pbottom = min(event->globalY(),ptop);
}
else{
pbottom = event->globalY();
}
}
if(resizeDir & left){
if(width() == minimumWidth()){
pleft = min(event->globalX(),pleft);
}
else if(width() == maximumWidth()){
pleft = max(event->globalX(),pleft);
}
else{
pleft = event->globalX();
}
}
else if(resizeDir & right){
if(width() == minimumWidth()){
pright = max(event->globalX(),pright);
}
else if(width() == maximumWidth()){
pright = min(event->globalX(),pright);
}
else{
pright = event->globalX();
}
}
setGeometry(QRect(QPoint(pleft,ptop),QPoint(pright,pbottom)));
}
}
else
{
testEdge();
}
}
void MainWindow::mouseReleaseEvent(QMouseEvent *event)
{
qDebug() << "mouseReleaseEvent" <<endl;
event->ignore();
if(resizeDir != nodir){
testEdge();
}
}