Skip to content

Commit

Permalink
Replace ctypes by existing pywin32 functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Dec 14, 2023
1 parent c81ab65 commit 6064f29
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
4 changes: 2 additions & 2 deletions src/AutoSplit.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/python3
import ctypes
import os
import signal
import sys
Expand All @@ -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
Expand Down Expand Up @@ -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):
Expand Down
3 changes: 1 addition & 2 deletions src/capture_method/BitBltCaptureMethod.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/capture_method/DesktopDuplicationCaptureMethod.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/capture_method/WindowsGraphicsCaptureMethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 7 additions & 11 deletions src/region_selection.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -29,9 +28,6 @@
is_valid_image,
)

user32 = ctypes.windll.user32


if TYPE_CHECKING:
from AutoSplit import AutoSplit

Expand Down Expand Up @@ -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__)
Expand Down
2 changes: 1 addition & 1 deletion src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 6064f29

Please sign in to comment.