把鼠标限制在屏幕上的一个矩形区域内

函数原型:BOOL ClipCursor(CONST RECT * lpRect);

  参数:IpRect:指向RECT结构的指针,该结构包含限制矩形区域左上角和右下角的屏幕坐标,如果该指针为NULL(空),则鼠标可以在屏幕的任何区域移动。

void TitleBar::mouseMoveEvent(QMouseEvent *event) {
    if ((event->buttons() & Qt::LeftButton) && this->moveable && this->isMoveable)  {  
        RECT rect;
        QRect qRect = QApplication::desktop()->availableGeometry();
        rect.left = qRect.left();
        rect.top = qRect.top();
        rect.right = qRect.right();
        rect.bottom = qRect.bottom();

        ClipCursor(&rect);

        parentWidget()->move(event->globalPos() - this->dragPosition);  
        event->accept();  
    }  
}

void TitleBar::mouseReleaseEvent(QMouseEvent *event) {  

    if (this->moveable) this->moveable = false;  
    ClipCursor(NULL);
}

 

posted @ 2014-10-18 15:10  纠纠结结  阅读(737)  评论(0编辑  收藏  举报