//确定点的位置方向 MainFrame::enum_Direction MainFrame::PointValid(int nXRelative,int nYRelative) { int nTop = qAbs(nYRelative)<VALUE_DIS ?eTop:eNone; int nRight = qAbs(nXRelative-rect().right())<VALUE_DIS ? eRight:eNone; int nBottom = qAbs(nYRelative-rect().bottom())<VALUE_DIS ? eBottom:eNone; int nLeft = qAbs(nXRelative)<VALUE_DIS ? eLeft:eNone; return enum_Direction(nTop + nRight + nBottom + nLeft); }
/** * 鼠标移动事件 * @event 事件 */ void AppUi::mouseMoveEvent(QMouseEvent *event) { if(!this->bIsMaxAble) { // 左键按下&&非最大化&&正在移动 if(this->bLeftPressed && this->bIsMoving) { // 移动窗体 this->move(this->pos() + event->globalPos() - this->pPressPoint); // 更新按下的点 this->pPressPoint = event->globalPos(); // 关闭调整大小,防止串扰 this->bIsResizing = false; // 设置光标样式为普通 setCursorStyle(enum_Direction(eNone)); return; } // 左键没有按下 && 没有在调节大小 if(!this->bLeftPressed && this->resizeValid(event->x(), event->y())) { // 设置光标样式 setCursorStyle(this->eDirection); return; } // 左键按下&&正在调节大小&&没有超出限制,则调节大小 if(this->bLeftPressed && this->bIsResizing) { setDrayMove(event->globalX(), event->globalY(), this->eDirection); this->pPressPoint = event->globalPos(); return; } // 设置光标样式为普通 setCursorStyle(enum_Direction(eNone)); } }
/** * 设置光标方向 * @xXRelative X方向相对坐标 * @nYRelative Y方向相对坐标 */ enum_Direction AppUi::resizeValid(int nXRelative, int nYRelative) { int nTop = qAbs(nYRelative) < APP_WINDOW_ZOOM_PADDING ? eTop:eNone; int nRight = qAbs(nXRelative-rect().right())< APP_WINDOW_ZOOM_PADDING + 2 ? eRight:eNone; int nBottom = qAbs(nYRelative-rect().bottom())< APP_WINDOW_ZOOM_PADDING + 2 ? eBottom:eNone; int nLeft = qAbs(nXRelative) < APP_WINDOW_ZOOM_PADDING + 2 ? eLeft:eNone; this->eDirection = enum_Direction(nTop + nRight + nBottom + nLeft); return this->eDirection; }
/** * 鼠标释放事件 * @event 事件 */ void AppUi::mouseReleaseEvent(QMouseEvent *event) { // 设置左键释放标志 if(event->button() == Qt::LeftButton) { // 左键按下标志释放 this->bLeftPressed = false; // 正在移动标志置否 this->bIsMoving = false; // 缩放方向标志置0 this->eDirection = enum_Direction(eNone); this->setCursorStyle(this->eDirection); } }
/** * 添加组件 */ void AppUi::initWindowState() { // 设置鼠标跟踪 this->setMouseTracking(true); // 初始状态栏不可调整大小 AppUi::statusBar->setSizeGripEnabled(false); // 设置无边框|任务栏系统菜单 this->setWindowFlags( Qt::FramelessWindowHint | Qt::WindowSystemMenuHint); // 设置鼠标跟踪 this->setMouseTracking(true); // 设置不继承父窗口背景 this->setAutoFillBackground(true); // 初始化时为不允许关闭窗口 this->allowClose = false; // 初始化为最大化窗口 this->bIsMaxAble = true; // 最大化窗体 this->setGeometry(qApp->desktop()->availableGeometry()); // 左键状态 bLeftPressed = false; // 是否正在移动 bIsMoving = false; // 是否正在调整大小 bIsResizing = false; // 调节大小的光标方向 eDirection = enum_Direction(eNone); // 保存非最大化时窗体尺寸 rRestoreWindow = QRect(APP_WINDOW_INIT_TOP_LEFT_X, APP_WINDOW_INIT_TOP_LEFT_Y, APP_WINDOW_INIT_WIDTH, APP_WINDOW_INIT_HEIGHT); }
/** * 调节窗口大小 * @nXGlobale X全局坐标 * @nYGlobale Y全局坐标 * @direction 方向枚举 */ void AppUi::setDrayMove(int nXGlobal, int nYGlobal, enum_Direction direction) { //计算偏差 int ndX = nXGlobal - this->pPressPoint.x(); int ndY = nYGlobal - this->pPressPoint.y(); //获得主窗口位置信息 QRect rectWindow = geometry(); //判别方向 if(direction & eTop) { rectWindow.setTop(rectWindow.top()+ndY); } if(direction & eRight) { rectWindow.setRight(rectWindow.right()+ndX); } if(direction & eBottom) { rectWindow.setBottom(rectWindow.bottom()+ndY); } if(direction & eLeft) { rectWindow.setLeft(rectWindow.left()+ndX); } // 小于最小尺寸,或大于最大尺寸,直接返回 if(rectWindow.width() < APP_WINDOW_INIT_WIDTH || rectWindow.height() < APP_WINDOW_INIT_HEIGHT || rectWindow.width() > qApp->desktop()->availableGeometry().width() || rectWindow.height() > qApp->desktop()->availableGeometry().height()) { this->bIsResizing = false; this->setCursorStyle(enum_Direction(eNone)); return; } // 重新设置窗口位置为新位置信息 setGeometry(rectWindow); }