复核驱动输入与窗口补丁,修复接管互斥和异常清理
This commit is contained in:
+61
-24
@@ -110,41 +110,78 @@ def ensure_server(timeout_s=90):
|
|||||||
|
|
||||||
# ---------------------------------------------------------------- 桌面渲染
|
# ---------------------------------------------------------------- 桌面渲染
|
||||||
|
|
||||||
def _blit_ok(hdc):
|
def _blit_ok(hdc, hwnd=None):
|
||||||
"""从给定的 DC 拷一小块回来, 成功与否。"""
|
"""探测采集 API, 窗口走 AALC 的 PrintWindow(flags=3)。不验证画面内容。"""
|
||||||
from ctypes import windll
|
if not hdc:
|
||||||
g = windll.gdi32
|
return False
|
||||||
mem = g.CreateCompatibleDC(hdc)
|
import ctypes
|
||||||
bmp = g.CreateCompatibleBitmap(hdc, 64, 64)
|
from ctypes import wintypes
|
||||||
g.SelectObject(mem, bmp)
|
import win32gui
|
||||||
r = g.BitBlt(mem, 0, 0, 64, 64, hdc, 0, 0, 0x00CC0020)
|
import win32ui
|
||||||
g.DeleteObject(bmp)
|
|
||||||
g.DeleteDC(mem)
|
source = target = bitmap = old = bitmap_handle = None
|
||||||
return bool(r)
|
try:
|
||||||
|
width, height = 64, 64
|
||||||
|
if hwnd:
|
||||||
|
left, top, right, bottom = win32gui.GetClientRect(hwnd)
|
||||||
|
width, height = right - left, bottom - top
|
||||||
|
if width <= 0 or height <= 0:
|
||||||
|
return False
|
||||||
|
source = win32ui.CreateDCFromHandle(hdc)
|
||||||
|
target = source.CreateCompatibleDC()
|
||||||
|
bitmap = win32ui.CreateBitmap()
|
||||||
|
bitmap.CreateCompatibleBitmap(source, width, height)
|
||||||
|
bitmap_handle = bitmap.GetHandle()
|
||||||
|
old = target.SelectObject(bitmap)
|
||||||
|
if hwnd:
|
||||||
|
capture = ctypes.windll.user32.PrintWindow
|
||||||
|
capture.argtypes = [wintypes.HWND, wintypes.HDC, wintypes.UINT]
|
||||||
|
capture.restype = wintypes.BOOL
|
||||||
|
return bool(capture(hwnd, target.GetSafeHdc(), 3))
|
||||||
|
target.BitBlt((0, 0), (width, height), source, (0, 0), 0x00CC0020)
|
||||||
|
return True
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
if old is not None:
|
||||||
|
target.SelectObject(old)
|
||||||
|
finally:
|
||||||
|
try:
|
||||||
|
if target is not None:
|
||||||
|
target.DeleteDC()
|
||||||
|
finally:
|
||||||
|
if bitmap_handle is not None:
|
||||||
|
win32gui.DeleteObject(bitmap_handle)
|
||||||
|
# source 只包装调用者的 DC, 由调用者 ReleaseDC, 不能 DeleteDC。
|
||||||
|
|
||||||
|
|
||||||
def screen_blit_ok():
|
def screen_blit_ok():
|
||||||
"""抓**整个屏幕**能不能成功(GetDC(0))。"""
|
import win32gui
|
||||||
from ctypes import windll
|
hdc = None
|
||||||
u = windll.user32
|
|
||||||
hdc = u.GetDC(0)
|
|
||||||
try:
|
try:
|
||||||
|
hdc = win32gui.GetDC(0)
|
||||||
return _blit_ok(hdc)
|
return _blit_ok(hdc)
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
finally:
|
finally:
|
||||||
u.ReleaseDC(0, hdc)
|
if hdc:
|
||||||
|
win32gui.ReleaseDC(0, hdc)
|
||||||
|
|
||||||
|
|
||||||
def window_blit_ok(hwnd):
|
def window_blit_ok(hwnd):
|
||||||
"""从**游戏窗口**的 DC 拷一小块回来能不能成功(和 AALC 的采集走同一类路径)。"""
|
import win32gui
|
||||||
from ctypes import windll
|
hdc = None
|
||||||
u = windll.user32
|
|
||||||
hdc = u.GetWindowDC(hwnd)
|
|
||||||
if not hdc:
|
|
||||||
return False
|
|
||||||
try:
|
try:
|
||||||
return _blit_ok(hdc)
|
if win32gui.IsIconic(hwnd):
|
||||||
|
return False
|
||||||
|
hdc = win32gui.GetWindowDC(hwnd)
|
||||||
|
return _blit_ok(hdc, hwnd=hwnd)
|
||||||
|
except Exception:
|
||||||
|
return False
|
||||||
finally:
|
finally:
|
||||||
u.ReleaseDC(hwnd, hdc)
|
if hdc:
|
||||||
|
win32gui.ReleaseDC(hwnd, hdc)
|
||||||
|
|
||||||
|
|
||||||
def render_ok():
|
def render_ok():
|
||||||
|
|||||||
Reference in New Issue
Block a user