无边框窗口的移动和改变大小

.h
QString getImage();
//无边框 可改变大小
bool isLeftPressDown; // 判断左键是否按下
QPoint dragPosition; // 窗口移动拖动时需要记住的点
Direction dir; // 窗口大小改变时,记录改变方向
//无边框的界面鼠标移动事件
QPoint mousePoint;
bool mouse_press=true;
void mouseMoveEvent(QMouseEvent *event);
void mousePressEvent(QMouseEvent *event);
void mouseReleaseEvent(QMouseEvent *event);
void region(const QPoint &cursorGlobalPoint);
初始化

    mouse_press = false;
	isLeftPressDown = false;
	this->dir = NONE;
	// 追踪鼠标
	this->setMouseTracking(true);

函数

void FeedBackWnd::mousePressEvent(QMouseEvent *event)
{
	if ((event->button() == Qt::LeftButton)){
		//可改变大小
		isLeftPressDown = true;
		if (dir != NONE) {
			this->mouseGrabber();
		}
		else {
			//dragPosition = event->globalPos() - this->frameGeometry().topLeft();

			//可移动
			mouse_press = true;
			mousePoint = event->globalPos() - this->pos();
		}

		//        event->accept();
	}
	//else if (event->button() == Qt::RightButton){
	//	//如果是右键
	//	this->close();
	//}
}
void FeedBackWnd::mouseMoveEvent(QMouseEvent *event)
{
		//可改变大小
		QPoint gloPoint = event->globalPos();
		QRect rect = this->rect();
		QPoint tl = mapToGlobal(rect.topLeft());
		QPoint rb = mapToGlobal(rect.bottomRight());

		if (!isLeftPressDown) {
			this->region(gloPoint);
		}
		else {
			if (!mouse_press)
			{
				if (dir != NONE) {
					QRect rMove(tl, rb);

					switch (dir) {
					case LEFT:
						if (rb.x() - gloPoint.x() <= this->minimumWidth())
							rMove.setX(tl.x());
						else
							rMove.setX(gloPoint.x());
						break;
					case RIGHT:
						rMove.setWidth(gloPoint.x() - tl.x());
						break;
					case UP:
						if (rb.y() - gloPoint.y() <= this->minimumHeight())
							rMove.setY(tl.y());
						else
							rMove.setY(gloPoint.y());
						break;
					case DOWN:
						rMove.setHeight(gloPoint.y() - tl.y());
						break;
					case LEFTTOP:
						if (rb.x() - gloPoint.x() <= this->minimumWidth())
							rMove.setX(tl.x());
						else
							rMove.setX(gloPoint.x());
						if (rb.y() - gloPoint.y() <= this->minimumHeight())
							rMove.setY(tl.y());
						else
							rMove.setY(gloPoint.y());
						break;
					case RIGHTTOP:
						rMove.setWidth(gloPoint.x() - tl.x());
						rMove.setY(gloPoint.y());
						break;
					case LEFTBOTTOM:
						rMove.setX(gloPoint.x());
						rMove.setHeight(gloPoint.y() - tl.y());
						break;
					case RIGHTBOTTOM:
						rMove.setWidth(gloPoint.x() - tl.x());
						rMove.setHeight(gloPoint.y() - tl.y());
						break;
					default:
						break;
					}
					this->setGeometry(rMove);
				}
				else {
					move(event->globalPos() - dragPosition);
					event->accept();
				}
			}
		
		}
	
	//可移动
	//    if(event->buttons() == Qt::LeftButton){  //如果这里写这行代码,拖动会有点问题
	if (mouse_press){
		move(event->globalPos() - mousePoint);
		//        event->accept();
	}
}
void FeedBackWnd::mouseReleaseEvent(QMouseEvent *event)
{
	//可移动
	mouse_press = false;
	//可改变大小
	if (event->button() == Qt::LeftButton) {
		isLeftPressDown = false;
		if (dir != NONE) {
			this->releaseMouse();
			this->setCursor(QCursor(Qt::ArrowCursor));
		}
	}
}
void FeedBackWnd::region(const QPoint &cursorGlobalPoint)
{
	// 获取窗体在屏幕上的位置区域,tl为topleft点,rb为rightbottom点
	QRect rect = this->rect();
	QPoint tl = mapToGlobal(rect.topLeft());
	QPoint rb = mapToGlobal(rect.bottomRight());

	int x = cursorGlobalPoint.x();
	int y = cursorGlobalPoint.y();

	if (tl.x() + PADDING >= x && tl.x() <= x && tl.y() + PADDING >= y && tl.y() <= y) {
		// 左上角
		dir = LEFTTOP;
		this->setCursor(QCursor(Qt::SizeFDiagCursor));  // 设置鼠标形状
	}
	else if (x >= rb.x() - PADDING && x <= rb.x() && y >= rb.y() - PADDING && y <= rb.y()) {
		// 右下角
		dir = RIGHTBOTTOM;
		this->setCursor(QCursor(Qt::SizeFDiagCursor));
	}
	else if (x <= tl.x() + PADDING && x >= tl.x() && y >= rb.y() - PADDING && y <= rb.y()) {
		//左下角
		dir = LEFTBOTTOM;
		this->setCursor(QCursor(Qt::SizeBDiagCursor));
	}
	else if (x <= rb.x() && x >= rb.x() - PADDING && y >= tl.y() && y <= tl.y() + PADDING) {
		// 右上角
		dir = RIGHTTOP;
		this->setCursor(QCursor(Qt::SizeBDiagCursor));
	}
	else if (x <= tl.x() + PADDING && x >= tl.x()) {
		// 左边
		dir = LEFT;
		this->setCursor(QCursor(Qt::SizeHorCursor));
	}
	else if (x <= rb.x() && x >= rb.x() - PADDING) {
		// 右边
		dir = RIGHT;
		this->setCursor(QCursor(Qt::SizeHorCursor));
	}
	else if (y >= tl.y() && y <= tl.y() + PADDING){
		// 上边
		dir = UP;
		this->setCursor(QCursor(Qt::SizeVerCursor));
	}
	else if (y <= rb.y() && y >= rb.y() - PADDING) {
		// 下边
		dir = DOWN;
		this->setCursor(QCursor(Qt::SizeVerCursor));
	}
	else {
		// 默认
		dir = NONE;
		this->setCursor(QCursor(Qt::ArrowCursor));
	}
}

你可能感兴趣的:(QT,qt,开发语言)