PYQT5 实现 无frame窗口的拖动和放缩

# -*- coding:utf-8 -*-
'''
Created on 2016年12月14日

@author: DXL

Copyright (C) 2004-2019 Shandong Zhaoyuan Software Development Co.,Ltd

'''
import cgitb
cgitb.enable( format='text')
from PyQt5.QtWidgets import QWidget, QApplication
from PyQt5.QtCore import Qt 
import sip
from ctypes.wintypes import *

print (help(MSG))
PADDING = 2
UP,DOWN,LEFT,RIGHT,LEFTTOP,LEFTBOTTOM,RIGHTTOP,RIGHTBOTTOM,UNDIRECT = range(9)
HTLEFT = 10
HTRIGHT = 11
HTTOP = 12
HTTOPLEFT = 13
HTTOPRIGHT = 14
HTBOTTOM = 15
HTBOTTOMLEFT = 16
HTBOTTOMRIGHT = 17
HTCAPTION = 2      

class CustomWidget(QWidget):
    def __init__(self,parent=None):
        QWidget.__init__(self,parent)
        self.setWindowFlags(Qt.FramelessWindowHint)

    def isInTitle(self, xPos, yPos):
        return yPos < 30

    def GET_X_LPARAM(self, param):
        return param & 0xffff

    def GET_Y_LPARAM(self, param):
        return param >> 16

    def nativeEvent(self,eventType,message):
        result = 0
        msg2 = ctypes.wintypes.MSG.from_address(message.__int__())
        minV,maxV = 18,22
        if msg2.message == 0x0084:
            print(msg2)
            xPos = self.GET_X_LPARAM(msg2.lParam) - self.frameGeometry().x()
            yPos = self.GET_Y_LPARAM(msg2.lParam) - self.frameGeometry().y()
#             if self.childAt(xPos,yPos) == 0:
#                 result = HTCAPTION
#             else:
#                 return (False,result)
            if(xPos > minV and xPos < maxV):
                result = HTLEFT
            elif(xPos > (self.width() - maxV) and xPos < (self.width() - minV)):
                result = HTRIGHT
            elif(yPos > minV and yPos < maxV):
                result = HTTOP
            elif(yPos > (self.height() - maxV) and yPos < (self.height() - minV)):
                result = HTBOTTOM
            elif(xPos > minV and xPos < maxV and yPos > minV and yPos < maxV):
                result = HTTOPLEFT
            elif(xPos > (self.width() - maxV) and xPos < (self.width() - minV) and yPos > minV and yPos < maxV):
                result = HTTOPRIGHT
            elif(xPos > minV and xPos < maxV and yPos > (self.height() - maxV) and yPos < (self.height() - minV)):
                result = HTBOTTOMLEFT
            elif(xPos > (self.width() - maxV) and xPos < (self.width() - minV) and yPos > (self.height() - maxV) and yPos < (self.height() - minV)):
                result = HTBOTTOMRIGHT
            else:
                result = HTCAPTION
            return (True,result)
        ret= QWidget.nativeEvent(self,eventType,message)
        return ret





if __name__ == '__main__':
    app = QApplication([])
    try:
        w = CustomWidget ()
        w.show()
    except:
        import traceback
        traceback.print_exc()

    app.exec_()
  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现导入图片鼠标滑轮放缩和左键按住移动,可以使用QGraphicsView和QGraphicsScene来实现,具体步骤如下: 1. 创建一个QGraphicsView对象和QGraphicsScene对象,并将QGraphicsScene设置为QGraphicsView的场景。 2. 使用QGraphicsPixmapItem将图片添加到QGraphicsScene中。 3. 重写QGraphicsView的wheelEvent()方法,实现鼠标滑轮放缩图片的功能。 4. 重写QGraphicsView的mousePressEvent()、mouseMoveEvent()和mouseReleaseEvent()方法,实现鼠标左键按住移动图片的功能。 下面是一个简单的代码示例: ```python from PyQt5.QtWidgets import QGraphicsView, QGraphicsScene, QGraphicsPixmapItem from PyQt5.QtGui import QPixmap from PyQt5.QtCore import Qt class ImageViewer(QGraphicsView): def __init__(self, parent=None): super().__init__(parent) # 创建QGraphicsScene对象 self.scene = QGraphicsScene(self) self.setScene(self.scene) # 添加图片 self.image = QGraphicsPixmapItem() self.scene.addItem(self.image) # 设置滚动条策略 self.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff) self.setRenderHint(QPainter.Antialiasing, True) # 设置鼠标追踪 self.setMouseTracking(True) # 设置缩放比例 self.scaleFactor = 1.0 def loadImage(self, filename): # 加载图片 pixmap = QPixmap(filename) self.image.setPixmap(pixmap) self.setSceneRect(pixmap.rect()) def wheelEvent(self, event): # 放缩图片 if event.angleDelta().y() > 0: self.scaleFactor *= 1.1 else: self.scaleFactor /= 1.1 self.scale(self.scaleFactor, self.scaleFactor) def mousePressEvent(self, event): # 记录鼠标按下的位置 if event.button() == Qt.LeftButton: self.lastPos = event.pos() def mouseMoveEvent(self, event): # 移动图片 if event.buttons() & Qt.LeftButton: delta = event.pos() - self.lastPos self.lastPos = event.pos() self.translate(delta.x(), delta.y()) def mouseReleaseEvent(self, event): pass ``` 在上面的代码中,loadImage()方法用于加载图片,wheelEvent()方法用于放缩图片,mousePressEvent()、mouseMoveEvent()和mouseReleaseEvent()方法用于移动图片。注意,在mousePressEvent()方法中要记录鼠标按下的位置,在mouseMoveEvent()方法中要计算鼠标移动的距离,并将场景平移相应的距离。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值