#include "testdialog.h"
#include "ui_testdialog.h"
TestDialog::TestDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::TestDialog)
{
ui->setupUi(this);
this->setWindowFlags(Qt::FramelessWindowHint);
setWindowFlags(Qt::FramelessWindowHint | Qt::Tool | Qt::WindowStaysOnTopHint);
setWindowOpacity(0.9);
setMouseTracking(true);
desktop = QApplication::desktop();
}
TestDialog::~TestDialog()
{
delete ui;
}
void TestDialog::mousePressEvent(QMouseEvent *event)
{
if (event->button() == Qt::LeftButton) {
this->m_drag = true;
this->dragPos = event->pos();
this->resizeDownPos = event->globalPos();
this->mouseDownRect = this->rect();
}
}
void TestDialog::mouseMoveEvent(QMouseEvent * event)
{
if (resizeRegion != Default)
{
handleResize();
return;
}
if(m_move) {
move(event->globalPos() - dragPos);
return;
}
QPoint clientCursorPos = event->pos();
QRect r = this->rect();
QRect resizeInnerRect(resizeBorderWidth, resizeBorderWidth, r.width() - 2*resizeBorderWidth, r.height() - 2*resizeBorderWidth);
if(r.contains(clientCursorPos) && !resizeInnerRect.contains(clientCursorPos)) { //调整窗体大小
ResizeRegion resizeReg = getResizeRegion(clientCursorPos);
setResizeCursor(resizeReg);
if (m_drag && (event->buttons() & Qt::LeftButton)) {
resizeRegion = resizeReg;
handleResize();
}
}
else { //移动窗体
setCursor(Qt::ArrowCursor);
if (m_drag && (event->buttons() & Qt::LeftButton)) {
m_move = true;
move(event->globalPos() - dragPos);
}
}
}
void TestDialog::mouseReleaseEvent(QMouseEvent *event)
{
m_drag = false;
if(m_move) {
m_move = false;
handleMove(event->globalPos()); //鼠标放开后若超出屏幕区域自动吸附于屏幕顶部/左侧/右侧
}
resizeRegion = Default;
setCursor(Qt::ArrowCursor);
}
void TestDialog::setResizeCursor(ResizeRegion region)
{
switch (region)
{
case North:
case South:
setCursor(Qt::SizeVerCursor);
break;
case East:
case West:
setCursor(Qt::SizeHorCursor);
break;
case NorthWest:
case SouthEast:
setCursor(Qt::SizeFDiagCursor);
break;
default:
setCursor(Qt::SizeBDiagCursor);
break;
}
}
ResizeRegion TestDialog::getResizeRegion(QPoint clientPos)
{
if (clientPos.y() <= resizeBorderWidth) {
if (clientPos.x() <= resizeBorderWidth)
return NorthWest;
else if (clientPos.x() >= this->width() - resizeBorderWidth)
return NorthEast;
else
return North;
}
else if (clientPos.y() >= this->height() - resizeBorderWidth) {
if (clientPos.x() <= resizeBorderWidth)
return SouthWest;
else if (clientPos.x() >= this->width() - resizeBorderWidth)
return SouthEast;
else
return South;
}
else {
if (clientPos.x() <= resizeBorderWidth)
return West;
else
return East;
}
}
void TestDialog::handleMove(QPoint pt)
{
QPoint currentPos = pt - dragPos;
if(currentPos.x()<desktop->x()) { //吸附于屏幕左侧
currentPos.setX(desktop->x());
}
else if (currentPos.x()+this->width()>desktop->width()) { //吸附于屏幕右侧
currentPos.setX(desktop->width()-this->width());
}
if(currentPos.y()<desktop->y()) { //吸附于屏幕顶部
currentPos.setY(desktop->y());
}
move(currentPos);
}
void TestDialog::handleResize()
{
int xdiff = QCursor::pos().x() - resizeDownPos.x();
int ydiff = QCursor::pos().y() - resizeDownPos.y();
switch (resizeRegion)
{
case East:
{
resize(mouseDownRect.width()+xdiff, this->height());
break;
}
case West:
{
resize(mouseDownRect.width()-xdiff, this->height());
move(resizeDownPos.x()+xdiff, this->y());
break;
}
case South:
{
resize(this->width(),mouseDownRect.height()+ydiff);
break;
}
case North:
{
resize(this->width(),mouseDownRect.height()-ydiff);
move(this->x(), resizeDownPos.y()+ydiff);
break;
}
case SouthEast:
{
resize(mouseDownRect.width() + xdiff, mouseDownRect.height() + ydiff);
break;
}
case NorthEast:
{
resize(mouseDownRect.width()+xdiff, mouseDownRect.height()-ydiff);
move(this->x(), resizeDownPos.y()+ydiff);
break;
}
case NorthWest:
{
resize(mouseDownRect.width()-xdiff, mouseDownRect.height()-ydiff);
move(resizeDownPos.x()+xdiff, resizeDownPos.y()+ydiff);
break;
}
case SouthWest:
{
resize(mouseDownRect.width()-xdiff, mouseDownRect.height()+ydiff);
move(resizeDownPos.x()+xdiff, this->y());
break;
}
}
}
Qt 窗口 无边框化,移动,大小调整
版权申诉
57 浏览量
2023-02-13
13:47:31
上传
评论
收藏 8KB ZIP 举报
恋恋西风
- 粉丝: 8847
- 资源: 122
最新资源
- 论文(最终)_20240430235101.pdf
- 基于python编写的Keras深度学习框架开发,利用卷积神经网络CNN,快速识别图片并进行分类
- 最全空间计量实证方法(空间杜宾模型和检验以及结果解释文档).txt
- 5uonly.apk
- 蓝桥杯Python组的历年真题
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 2023-04-06-项目笔记 - 第一百十九阶段 - 4.4.2.117全局变量的作用域-117 -2024.04.30
- 前端开发技术实验报告:内含4四实验&实验报告
- Highlight Plus v20.0.1
- 林周瑜-论文.docx
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈