@@ -93,16 +93,27 @@ def __init__(self):
9393 bmi .bmiHeader .biClrImportant = 0 # See grab.__doc__ [3]
9494 self ._bmi = bmi
9595
96- def __exit__ (self , * args ):
97- # type: (*str ) -> None
98- """ Cleanup . """
96+ def close (self ):
97+ # type: () -> None
98+ """ Close GDI handles and free DCs . """
9999
100- for attribute in {"_bmp" , "_memdc" , "_srcdc" }:
101- attr = getattr (self , attribute , None )
102- if attr :
103- ctypes .windll .gdi32 .DeleteObject (attr )
100+ try :
101+ ctypes .windll .gdi32 .DeleteObject (self ._bmp )
102+ del self ._bmp
103+ except AttributeError :
104+ pass
105+
106+ try :
107+ ctypes .windll .gdi32 .DeleteDC (self ._memdc )
108+ del self ._memdc
109+ except (OSError , AttributeError ):
110+ pass
104111
105- super (MSS , self ).__exit__ (* args )
112+ try :
113+ ctypes .windll .user32 .ReleaseDC (0 , self ._srcdc )
114+ del self ._srcdc
115+ except AttributeError :
116+ pass
106117
107118 @property
108119 def monitors (self ):
@@ -235,6 +246,10 @@ def set_argtypes(callback):
235246 ctypes .wintypes .LPARAM ,
236247 ]
237248 ctypes .windll .user32 .GetWindowDC .argtypes = [ctypes .wintypes .HWND ]
249+ ctypes .windll .user32 .ReleaseDC .argtypes = [
250+ ctypes .wintypes .HWND ,
251+ ctypes .wintypes .HGDIOBJ ,
252+ ]
238253 ctypes .windll .gdi32 .GetDeviceCaps .argtypes = [
239254 ctypes .wintypes .HWND ,
240255 ctypes .wintypes .INT ,
@@ -279,6 +294,7 @@ def set_restypes():
279294 ctypes .windll .user32 .GetSystemMetrics .restype = ctypes .wintypes .INT
280295 ctypes .windll .user32 .EnumDisplayMonitors .restype = ctypes .wintypes .BOOL
281296 ctypes .windll .user32 .GetWindowDC .restype = ctypes .wintypes .HDC
297+ ctypes .windll .user32 .ReleaseDC .restype = ctypes .wintypes .INT
282298 ctypes .windll .gdi32 .GetDeviceCaps .restype = ctypes .wintypes .INT
283299 ctypes .windll .gdi32 .CreateCompatibleDC .restype = ctypes .wintypes .HDC
284300 ctypes .windll .gdi32 .CreateCompatibleBitmap .restype = ctypes .wintypes .HBITMAP
0 commit comments