From 6064f291f203e9fa66b455530dfb0b17695a7be3 Mon Sep 17 00:00:00 2001 From: Avasam Date: Thu, 14 Dec 2023 00:46:57 -0500 Subject: [PATCH] Replace ctypes by existing pywin32 functions --- pyproject.toml | 2 +- src/AutoSplit.py | 4 ++-- src/capture_method/BitBltCaptureMethod.py | 3 +-- .../DesktopDuplicationCaptureMethod.py | 6 +++--- .../WindowsGraphicsCaptureMethod.py | 2 +- src/region_selection.py | 18 +++++++----------- src/utils.py | 2 +- 7 files changed, 16 insertions(+), 21 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 2f2bcfe4..06e68387 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -177,5 +177,5 @@ reportFunctionMemberAccess = "none" reportUnnecessaryComparison = "warning" # Using Flake8/Ruff instead reportUnusedImport = "none" -# numpy has way too many complex types that triggers this +# pywin32 has way too many Unknown parameters left reportUnknownMemberType = "none" diff --git a/src/AutoSplit.py b/src/AutoSplit.py index f1297f67..3fd27f1b 100644 --- a/src/AutoSplit.py +++ b/src/AutoSplit.py @@ -1,5 +1,4 @@ #!/usr/bin/python3 -import ctypes import os import signal import sys @@ -17,6 +16,7 @@ from PySide6.QtTest import QTest from PySide6.QtWidgets import QApplication, QFileDialog, QLabel, QMainWindow, QMessageBox from typing_extensions import override +from win32comext.shell import shell as shell32 import error_messages import user_profile @@ -55,7 +55,7 @@ # Needed when compiled, along with the custom hook-requests PyInstaller hook os.environ["REQUESTS_CA_BUNDLE"] = certifi.where() myappid = f"Toufool.AutoSplit.v{AUTOSPLIT_VERSION}" -ctypes.windll.shell32.SetCurrentProcessExplicitAppUserModelID(myappid) +shell32.SetCurrentProcessExplicitAppUserModelID(myappid) class AutoSplit(QMainWindow, design.Ui_MainWindow): diff --git a/src/capture_method/BitBltCaptureMethod.py b/src/capture_method/BitBltCaptureMethod.py index 079a010a..29c3ddb7 100644 --- a/src/capture_method/BitBltCaptureMethod.py +++ b/src/capture_method/BitBltCaptureMethod.py @@ -1,13 +1,12 @@ import ctypes -import ctypes.wintypes import numpy as np import pywintypes import win32con +import win32gui import win32ui from cv2.typing import MatLike from typing_extensions import override -from win32 import win32gui from capture_method.CaptureMethodBase import CaptureMethodBase from utils import BGRA_CHANNEL_COUNT, get_window_bounds, is_valid_hwnd, try_delete_dc diff --git a/src/capture_method/DesktopDuplicationCaptureMethod.py b/src/capture_method/DesktopDuplicationCaptureMethod.py index 0551ff20..65ce19ec 100644 --- a/src/capture_method/DesktopDuplicationCaptureMethod.py +++ b/src/capture_method/DesktopDuplicationCaptureMethod.py @@ -1,12 +1,12 @@ -import ctypes from typing import TYPE_CHECKING, cast import cv2 import d3dshot +import win32api import win32con +import win32gui from cv2.typing import MatLike from typing_extensions import override -from win32 import win32gui from capture_method.BitBltCaptureMethod import BitBltCaptureMethod from utils import GITHUB_REPOSITORY, get_window_bounds @@ -37,7 +37,7 @@ def __init__(self, autosplit: "AutoSplit"): def get_frame(self): selection = self._autosplit_ref.settings_dict["capture_region"] hwnd = self._autosplit_ref.hwnd - hmonitor = ctypes.windll.user32.MonitorFromWindow(hwnd, win32con.MONITOR_DEFAULTTONEAREST) + hmonitor = win32api.MonitorFromWindow(hwnd, win32con.MONITOR_DEFAULTTONEAREST) if not hmonitor or not self.check_selected_region_exists(): return None diff --git a/src/capture_method/WindowsGraphicsCaptureMethod.py b/src/capture_method/WindowsGraphicsCaptureMethod.py index 465a4349..6c387a58 100644 --- a/src/capture_method/WindowsGraphicsCaptureMethod.py +++ b/src/capture_method/WindowsGraphicsCaptureMethod.py @@ -2,9 +2,9 @@ from typing import TYPE_CHECKING, cast import numpy as np +import win32gui from cv2.typing import MatLike from typing_extensions import override -from win32 import win32gui from winsdk.windows.graphics import SizeInt32 from winsdk.windows.graphics.capture import Direct3D11CaptureFramePool, GraphicsCaptureSession from winsdk.windows.graphics.capture.interop import create_for_window diff --git a/src/region_selection.py b/src/region_selection.py index 764cd2f4..34a61b22 100644 --- a/src/region_selection.py +++ b/src/region_selection.py @@ -1,17 +1,16 @@ -import ctypes -import ctypes.wintypes import os from math import ceil -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, cast import cv2 import numpy as np +import win32api +import win32gui from cv2.typing import MatLike from PySide6 import QtCore, QtGui, QtWidgets from PySide6.QtTest import QTest from pywinctl import getTopWindowAt from typing_extensions import override -from win32 import win32gui from win32con import SM_CXVIRTUALSCREEN, SM_CYVIRTUALSCREEN, SM_XVIRTUALSCREEN, SM_YVIRTUALSCREEN from winsdk._winrt import initialize_with_window from winsdk.windows.foundation import AsyncStatus, IAsyncOperation @@ -29,9 +28,6 @@ is_valid_image, ) -user32 = ctypes.windll.user32 - - if TYPE_CHECKING: from AutoSplit import AutoSplit @@ -298,10 +294,10 @@ def __init__(self): super().__init__() # We need to pull the monitor information to correctly draw the geometry covering all portions # of the user's screen. These parameters create the bounding box with left, top, width, and height - x = user32.GetSystemMetrics(SM_XVIRTUALSCREEN) - y = user32.GetSystemMetrics(SM_YVIRTUALSCREEN) - width = user32.GetSystemMetrics(SM_CXVIRTUALSCREEN) - height = user32.GetSystemMetrics(SM_CYVIRTUALSCREEN) + x = cast(int, win32api.GetSystemMetrics(SM_XVIRTUALSCREEN)) + y = cast(int, win32api.GetSystemMetrics(SM_YVIRTUALSCREEN)) + width = cast(int, win32api.GetSystemMetrics(SM_CXVIRTUALSCREEN)) + height = cast(int, win32api.GetSystemMetrics(SM_CYVIRTUALSCREEN)) self.setGeometry(x, y, width, height) self.setFixedSize(width, height) # Prevent move/resizing on Linux self.setWindowTitle(type(self).__name__) diff --git a/src/utils.py b/src/utils.py index 3c086c18..d62f855b 100644 --- a/src/utils.py +++ b/src/utils.py @@ -10,9 +10,9 @@ from threading import Thread from typing import TYPE_CHECKING, Any, TypeGuard, TypeVar +import win32gui import win32ui from cv2.typing import MatLike -from win32 import win32gui from winsdk.windows.ai.machinelearning import LearningModelDevice, LearningModelDeviceKind from winsdk.windows.media.capture import MediaCapture