Skip to content

Commit 1bba7e9

Browse files
committed
添加禁用窗口截图的功能
1 parent 3fc266a commit 1bba7e9

File tree

8 files changed

+119
-8
lines changed

8 files changed

+119
-8
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* Win11 mica blur
3636
* Win7 Aero blur
3737
* MacOS blur
38+
* Disable screen capture
3839

3940
## Install
4041
To install use pip:

examples/screen_capture_filter.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# coding:utf-8
2+
import sys
3+
4+
from PyQt5.QtCore import Qt
5+
from PyQt5.QtWidgets import QApplication
6+
7+
from qframelesswindow import FramelessWindow
8+
from qframelesswindow.utils import ScreenCaptureFilter
9+
10+
11+
12+
class Window(FramelessWindow):
13+
14+
def __init__(self, parent=None):
15+
super().__init__(parent=parent)
16+
self.setWindowTitle("PyQt-Frameless-Window")
17+
18+
# disable screen capture
19+
self.installEventFilter(ScreenCaptureFilter(self))
20+
21+
22+
23+
if __name__ == "__main__":
24+
# enable dpi scale
25+
QApplication.setHighDpiScaleFactorRoundingPolicy(
26+
Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)
27+
QApplication.setAttribute(Qt.AA_EnableHighDpiScaling)
28+
QApplication.setAttribute(Qt.AA_UseHighDpiPixmaps)
29+
30+
# run app
31+
app = QApplication(sys.argv)
32+
demo = Window()
33+
demo.show()
34+
sys.exit(app.exec_())

qframelesswindow/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
:license: GPLv3, see LICENSE for more details.
1313
"""
1414

15-
__version__ = "0.5.1"
15+
__version__ = "0.6.0"
1616
__author__ = "zhiyiYo"
1717

1818
import sys

qframelesswindow/utils/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44
if sys.platform == "win32":
55
from .win32_utils import WindowsMoveResize as MoveResize
66
from .win32_utils import getSystemAccentColor
7+
from .win32_utils import WindowsScreenCaptureFilter as ScreenCaptureFilter
78
elif sys.platform == "darwin":
89
from .mac_utils import MacMoveResize as MoveResize
910
from .mac_utils import getSystemAccentColor
11+
from .mac_utils import MacScreenCaptureFilter as ScreenCaptureFilter
1012
else:
1113
from .linux_utils import LinuxMoveResize as MoveResize
1214
from .linux_utils import getSystemAccentColor
15+
from .linux_utils import LinuxScreenCaptureFilter as ScreenCaptureFilter
1316

1417

1518
def startSystemMove(window, globalPos):

qframelesswindow/utils/linux_utils.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import xcffib as xcb
55
from PyQt5 import sip
6-
from PyQt5.QtCore import QPointF, Qt, QEvent, QPoint
6+
from PyQt5.QtCore import QPointF, Qt, QEvent, QPoint, QObject
77
from PyQt5.QtGui import QMouseEvent, QColor
88
from PyQt5.QtWidgets import QWidget, QApplication
99
from PyQt5.QtX11Extras import QX11Info
@@ -176,4 +176,23 @@ def getSystemAccentColor():
176176
color: QColor
177177
accent color
178178
"""
179-
return QColor()
179+
return QColor()
180+
181+
182+
class LinuxScreenCaptureFilter(QObject):
183+
""" Filter for screen capture """
184+
185+
def __init__(self, parent: QWidget):
186+
super().__init__(parent)
187+
self.setScreenCaptureEnabled(False)
188+
189+
def eventFilter(self, watched, event):
190+
if watched == self.parent():
191+
if event.type() == QEvent.Type.WinIdChange:
192+
self.setScreenCaptureEnabled(self.isScreenCaptureEnabled)
193+
194+
return super().eventFilter(watched, event)
195+
196+
def setScreenCaptureEnabled(self, enabled: bool):
197+
""" Set screen capture enabled """
198+
self.isScreenCaptureEnabled = enabled

qframelesswindow/utils/mac_utils.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import Cocoa
55
import objc
6-
from PyQt5.QtCore import QT_VERSION_STR
6+
from PyQt5.QtCore import QT_VERSION_STR, QEvent, QObject
77
from PyQt5.QtGui import QColor
88
from PyQt5.QtWidgets import QWidget
99
from Quartz.CoreGraphics import (CGEventCreateMouseEvent,
@@ -86,4 +86,29 @@ def getSystemAccentColor():
8686
r = int(color.redComponent() * 255)
8787
g = int(color.greenComponent() * 255)
8888
b = int(color.blueComponent() * 255)
89-
return QColor(r, g, b)
89+
return QColor(r, g, b)
90+
91+
92+
class MacScreenCaptureFilter(QObject):
93+
""" Filter for screen capture """
94+
95+
def __init__(self, parent: QWidget):
96+
super().__init__(parent)
97+
self.setScreenCaptureEnabled(False)
98+
99+
def eventFilter(self, watched, event):
100+
if watched == self.parent():
101+
if event.type() == QEvent.Type.WinIdChange:
102+
self.setScreenCaptureEnabled(self.isScreenCaptureEnabled)
103+
104+
return super().eventFilter(watched, event)
105+
106+
def setScreenCaptureEnabled(self, enabled: bool):
107+
""" Set screen capture enabled """
108+
self.isScreenCaptureEnabled = enabled
109+
110+
nsWindow = getNSWindow(self.parent().winId())
111+
if nsWindow:
112+
NSWindowSharingNone = 0
113+
NSWindowSharingReadOnly = 1
114+
nsWindow.setSharingType_(NSWindowSharingReadOnly if enabled else NSWindowSharingNone)

qframelesswindow/utils/win32_utils.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# coding:utf-8
2-
from ctypes import Structure, byref, sizeof, windll, c_int, c_ulong, c_bool, POINTER
2+
from ctypes import Structure, byref, sizeof, windll, c_int, c_ulong, c_bool, POINTER, WinDLL, wintypes
33
from ctypes.wintypes import DWORD, HWND, LPARAM, RECT, UINT
44
from platform import platform
55
import sys
@@ -9,8 +9,9 @@
99
import win32con
1010
import win32gui
1111
import win32print
12-
from PyQt5.QtCore import QOperatingSystemVersion
12+
from PyQt5.QtCore import QOperatingSystemVersion, QObject, QEvent
1313
from PyQt5.QtGui import QGuiApplication, QColor
14+
from PyQt5.QtWidgets import QWidget
1415
from win32comext.shell import shellcon
1516

1617

@@ -351,3 +352,31 @@ def starSystemResize(cls, window, globalPos, edges):
351352
window edges
352353
"""
353354
pass
355+
356+
357+
class WindowsScreenCaptureFilter(QObject):
358+
""" Filter for screen capture """
359+
360+
def __init__(self, parent: QWidget):
361+
super().__init__(parent)
362+
self.setScreenCaptureEnabled(False)
363+
364+
def eventFilter(self, watched, event):
365+
if watched == self.parent():
366+
if event.type() == QEvent.Type.WinIdChange:
367+
self.setScreenCaptureEnabled(self.isScreenCaptureEnabled)
368+
369+
return super().eventFilter(watched, event)
370+
371+
def setScreenCaptureEnabled(self, enabled: bool):
372+
""" Set screen capture enabled """
373+
self.isScreenCaptureEnabled = enabled
374+
WDA_NONE = 0x00000000
375+
WDA_EXCLUDEFROMCAPTURE = 0x00000011
376+
377+
user32 = WinDLL('user32', use_last_error=True)
378+
SetWindowDisplayAffinity = user32.SetWindowDisplayAffinity
379+
SetWindowDisplayAffinity.argtypes = (wintypes.HWND, wintypes.DWORD)
380+
SetWindowDisplayAffinity.restype = wintypes.BOOL
381+
382+
SetWindowDisplayAffinity(int(self.parent().winId()), WDA_NONE if enabled else WDA_EXCLUDEFROMCAPTURE)

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
setuptools.setup(
88
name="PyQt5-Frameless-Window",
9-
version="0.5.1",
9+
version="0.6.0",
1010
keywords="pyqt frameless",
1111
author="zhiyiYo",
1212
author_email="[email protected]",

0 commit comments

Comments
 (0)