1 Star 0 Fork 0

程序员马工 / WindowFramLess

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
fram_less_window.py 3.45 KB
一键复制 编辑 原始数据 按行查看 历史
程序员马工 提交于 2023-05-15 17:14 . first commit
import ctypes
import ctypes.wintypes
import sys
from PySide2.QtWidgets import QApplication, QMainWindow
from PySide2.QtUiTools import QUiLoader
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2 import QtCore
from PySide2.QtGui import *
from PySide2.QtSvg import *
from PySide2 import QtGui
from window_ui import Ui_MainWindow
# Windows constants
WM_NCHITTEST = 0x0084
HTCAPTION = 2
HTLEFT = 10
HTRIGHT = 11
HTTOP = 12
HTTOPLEFT = 13
HTTOPRIGHT = 14
HTBOTTOM = 15
HTBOTTOMLEFT = 16
HTBOTTOMRIGHT = 17
class FrameLessWindow(Ui_MainWindow, QMainWindow):
def __init__(self) -> None:
super().__init__()
self._drag = False
self._corner_drag = False
self._bottom_drag = False
self._right_drag = False
self._padding = 5
self.setupUi(self)
self.setWindowFlags(Qt.FramelessWindowHint) # 隐藏边框
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
if event.pos().x() > self.width() - self._padding and event.pos().y() > self.height() - self._padding:
self._corner_drag = True
elif event.pos().x() > self.width() - self._padding:
self._right_drag = True
elif event.pos().y() > self.height() - self._padding:
self._bottom_drag = True
else:
self._drag = True
self.move_DragPosition = event.globalPos() - self.pos()
event.accept()
def mouseMoveEvent(self, event):
if Qt.LeftButton and self._drag:
self.move(event.globalPos() - self.move_DragPosition)
event.accept()
elif Qt.LeftButton and self._right_drag:
self.resize(event.pos().x(),self.height())
event.accept()
elif Qt.LeftButton and self._bottom_drag:
self.resize(self.width(),event.pos().y())
event.accept()
elif Qt.LeftButton and self._corner_drag:
self.resize(event.pos().x(),event.pos().y())
event.accept()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton:
self._drag = False
self._corner_drag = False
self._bottom_drag = False
self._right_drag = False
def nativeEvent(self, eventType, message):
if eventType == b"windows_generic_MSG":
msg = ctypes.wintypes.MSG.from_address(message.__int__())
if msg.message == WM_NCHITTEST:
x = msg.lParam & 0xffff
y = (msg.lParam >> 16) & 0xffff
pos = self.mapFromGlobal(QtCore.QPoint(x, y))
if pos.x() < 5:
if pos.y() < 5:
return True, HTTOPLEFT
elif pos.y() > self.height() - 5:
return True, HTBOTTOMLEFT
else:
return True, HTLEFT
elif pos.x() > self.width() - 5:
if pos.y() < 5:
return True, HTTOPRIGHT
elif pos.y() > self.height() - 5:
return True, HTBOTTOMRIGHT
else:
return True, HTRIGHT
elif pos.y() < 5:
return True, HTTOP
elif pos.y() > self.height() - 5:
return True, HTBOTTOM
return False
Python
1
https://gitee.com/cxy-magong/window-fram-less.git
git@gitee.com:cxy-magong/window-fram-less.git
cxy-magong
window-fram-less
WindowFramLess
master

搜索帮助