|
1 | 1 | # 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 |
3 | 3 | from ctypes.wintypes import DWORD, HWND, LPARAM, RECT, UINT |
4 | 4 | from platform import platform |
5 | 5 | import sys |
|
9 | 9 | import win32con |
10 | 10 | import win32gui |
11 | 11 | import win32print |
12 | | -from PyQt5.QtCore import QOperatingSystemVersion |
| 12 | +from PyQt5.QtCore import QOperatingSystemVersion, QObject, QEvent |
13 | 13 | from PyQt5.QtGui import QGuiApplication, QColor |
| 14 | +from PyQt5.QtWidgets import QWidget |
14 | 15 | from win32comext.shell import shellcon |
15 | 16 |
|
16 | 17 |
|
@@ -351,3 +352,31 @@ def starSystemResize(cls, window, globalPos, edges): |
351 | 352 | window edges |
352 | 353 | """ |
353 | 354 | 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) |
0 commit comments