天天看点

无边框窗口鼠标拉伸

无边框窗口鼠标拉伸

 窗口区域划分

无边框窗口鼠标拉伸
#include "SignalWin.h"
#include "Mycanvas.h"
#include <QDebug>
#include <QMouseEvent>
SignalWin::SignalWin(QWidget *parent)
	: QWidget(parent),
	m_bPressed(false),
	m_bSizeChanging(false)
{
	ui.setupUi(this);

}

SignalWin::~SignalWin()
{

}

void SignalWin::setCurWinInfo(SignalWinInfo info)
{
	m_info = info;
	setMouseTracking(true);   //开启鼠标位置追踪
	ui.WinFrame->setMouseTracking(true);   //开启鼠标位置追踪
	ui.SignalWinFrame->setMouseTracking(true);
}

void SignalWin::mousePressEvent(QMouseEvent *event)
{
	m_startPos = event->pos();
	m_startWPos = pos();
	pLast = event->globalPos();
	m_bPressed = true;
	m_wid = geometry();
	raise();
}
int poss = 0;
void SignalWin::mouseMoveEvent(QMouseEvent *event)
{
	if (this->isMaximized()) 
		return;
	if (!m_bSizeChanging)
	{
		poss = countFlag(event->pos(), countRow(event->pos()));//计算出来鼠标在哪个区域
		if (!event->buttons())
			setCursorType(poss);//设置鼠标形状
	}
	

	if ((event->buttons() == Qt::LeftButton) && m_bPressed)
	{
		m_bSizeChanging = true;
		QPoint ptemp;
		QPoint Framepos = event->globalPos();
		ptemp = m_startWPos + Framepos - pLast;  //鼠标移动的偏移量
		if (poss == 22)    //区域(2,2)表示移动窗口
		{
			move(ptemp);

		}
		else
		{
			QRect wid = geometry();

			int minWidth = this->minimumWidth();
			int minHeight = this->minimumHeight();

			switch (poss)//改变窗口的大小
			{
			case 11:
			{
				QPoint pos = m_wid.topLeft();
				pos.rx() = pos.rx() + Framepos.x() - pLast.x();
				pos.ry() = pos.ry() + Framepos.y() - pLast.y();
				wid.setTopLeft(pos);
				break;//左上角
			}
			case 12:
			{
				int topY = m_wid.top();
				topY = topY + Framepos.y() - pLast.y();
				wid.setTop(topY);
				break;//中上角

			}
			case 13:
			{
				QPoint pos = m_wid.topRight();
				pos.rx() = pos.rx() + Framepos.x() - pLast.x();
				pos.ry() = pos.ry() + Framepos.y() - pLast.y();
				wid.setTopRight(pos);
				break;//右上角
			}
			case 21:
			{
				int leftX= m_wid.left();
				leftX = leftX + Framepos.x() - pLast.x();
				wid.setLeft(leftX);
				break;//中左角
			}
			case 23:
			{
				int rightX = m_wid.right();
				rightX = rightX + Framepos.x() - pLast.x();
				wid.setRight(rightX);
				break;//中右角
			}
			case 31:
			{
				QPoint pos = m_wid.bottomLeft();
				pos.rx() = pos.rx() + Framepos.x() - pLast.x();
				pos.ry() = pos.ry() + Framepos.y() - pLast.y();
				wid.setBottomLeft(pos);

				break;//左下角
			}
			case 32:
			{
				int bottomY = m_wid.bottom();
				bottomY = bottomY + Framepos.y() - pLast.y();
				wid.setBottom(bottomY);
				break;//中下角
			}
			case 33:
			{
				QPoint pos = m_wid.bottomRight();
				pos.rx() = pos.rx() + Framepos.x() - pLast.x();
				pos.ry() = pos.ry() + Framepos.y() - pLast.y();
				wid.setBottomRight(pos);

				break;//左下角
			}
			}

			setGeometry(wid);   //设置窗口的位置
		}
	}

}

void SignalWin::mouseReleaseEvent(QMouseEvent *event)
{
	
	m_bPressed = false;
	m_bSizeChanging = false;

}

int SignalWin::countFlag(QPoint p, int row)//计算鼠标在哪一列和哪一行
{
	if (p.y() < MARGIN)
		return 10 + row;
	else if (p.y() > this->height() - MARGIN)
		return 30 + row;
	else
		return 20 + row;
}
void SignalWin::setCursorType(int flag)//根据鼠标所在位置改变鼠标指针形状
{
	Qt::CursorShape cursor;
	switch (flag)
	{
	case 11:
	case 33:
		cursor = Qt::SizeFDiagCursor; break;
	case 13:
	case 31:
		cursor = Qt::SizeBDiagCursor; break;
	case 21:
	case 23:
		cursor = Qt::SizeHorCursor; break;
	case 12:
	case 32:
		cursor = Qt::SizeVerCursor; break;
	case 22:
		cursor = Qt::ArrowCursor; break;
	default:
		QApplication::restoreOverrideCursor();//恢复鼠标指针性状
		break;

	}
	setCursor(cursor);
}

int SignalWin::countRow(QPoint p)
{
	int row = 0;
	if (p.x() < MARGIN)
		return row + 1;
	else if (p.x() > this->width() - MARGIN)
		return row + 3;
	else
		return 2 + row;
}
           

继续阅读