無邊框視窗滑鼠拉伸 視窗區域劃分
無邊框視窗滑鼠拉伸 #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;
}