def clean_dc(self): dc = self.dc wnd = self.wnd if dc and wnd: self.dc = None self.wnd = None ReleaseDC(wnd, dc) memdc = self.memdc if memdc: self.memdc = None DeleteDC(memdc)
def cleanup(self): if self.disabled_dwm_composition: set_dwm_composition(DWM_EC_ENABLECOMPOSITION) dc = self.dc if dc: self.dc = None ReleaseDC(dc) memdc = self.memdc if memdc: self.memdc = None DeleteDC(memdc)
def clean(self): if self.disabled_dwm_composition: set_dwm_composition(DWM_EC_ENABLECOMPOSITION) dc = self.dc wnd = self.wnd if dc and wnd: self.dc = None self.wnd = None ReleaseDC(wnd, dc) memdc = self.memdc if memdc: self.memdc = None DeleteDC(memdc)
def __exit__(self, exc_type, exc_val, exc_tb): log("GDIPrintContext(%s).exit%s hdc=%s, info=%s, handle=%s", self.printer_name, (exc_type, exc_val, exc_tb), self.hdc, (self.info1, self.info2, self.info8, self.info9), self.handle) if self.hdc: DeleteDC(self.hdc) self.hdc = None if self.info1: msvcrt.free(self.info1) self.info1 = None if self.info2: msvcrt.free(self.info2) self.info2 = None if self.info8: msvcrt.free(self.info8) self.info8 = None if self.info9: msvcrt.free(self.info9) self.info9 = None if self.handle: ClosePrinter(self.handle) self.handle = None
def get_cursor_data(hCursor): #w, h = get_fixed_cursor_size() if not hCursor: return None x, y = 0, 0 dc = None memdc = None bitmap = None old_handle = None pixels = None try: ii = ICONINFO() ii.cbSize = sizeof(ICONINFO) if not GetIconInfo(hCursor, byref(ii)): raise WindowsError() #@UndefinedVariable x = ii.xHotspot y = ii.yHotspot cursorlog( "get_cursor_data(%#x) hotspot at %ix%i, hbmColor=%#x, hbmMask=%#x", hCursor, x, y, ii.hbmColor or 0, ii.hbmMask or 0) if not ii.hbmColor: #FIXME: we don't handle black and white cursors return None iie = ICONINFOEXW() iie.cbSize = sizeof(ICONINFOEXW) if not GetIconInfoExW(hCursor, byref(iie)): raise WindowsError() #@UndefinedVariable name = iie.szResName[:MAX_PATH] cursorlog("wResID=%#x, sxModName=%s, szResName=%s", iie.wResID, iie.sxModName[:MAX_PATH], name) bm = Bitmap() if not GetObjectA(ii.hbmColor, sizeof(Bitmap), byref(bm)): raise WindowsError() #@UndefinedVariable cursorlog( "cursor bitmap: type=%i, width=%i, height=%i, width bytes=%i, planes=%i, bits pixel=%i, bits=%#x", bm.bmType, bm.bmWidth, bm.bmHeight, bm.bmWidthBytes, bm.bmPlanes, bm.bmBitsPixel, bm.bmBits or 0) w = bm.bmWidth h = bm.bmHeight dc = GetDC(None) assert dc, "failed to get a drawing context" memdc = CreateCompatibleDC(dc) assert memdc, "failed to get a compatible drawing context from %s" % dc bitmap = CreateCompatibleBitmap(dc, w, h) assert bitmap, "failed to get a compatible bitmap from %s" % dc old_handle = SelectObject(memdc, bitmap) #check if icon is animated: UINT_MAX = 2**32 - 1 if not DrawIconEx(memdc, 0, 0, hCursor, w, h, UINT_MAX, 0, 0): cursorlog("cursor is animated!") #if not DrawIcon(memdc, 0, 0, hCursor): if not DrawIconEx(memdc, 0, 0, hCursor, w, h, 0, 0, win32con.DI_NORMAL): raise WindowsError() #@UndefinedVariable buf_size = bm.bmWidthBytes * h buf = create_string_buffer(b"", buf_size) r = GetBitmapBits(bitmap, buf_size, byref(buf)) cursorlog("get_cursor_data(%#x) GetBitmapBits(%#x, %#x, %#x)=%i", hCursor, bitmap, buf_size, addressof(buf), r) if not r: cursorlog.error("Error: failed to copy screen bitmap data") return None elif r != buf_size: cursorlog.warn( "Warning: invalid cursor buffer size, got %i bytes but expected %i", r, buf_size) return None else: #32-bit data: pixels = bytearray(strtobytes(buf.raw)) has_alpha = False has_pixels = False for i in range(len(pixels) // 4): has_pixels = has_pixels or pixels[i * 4] != 0 or pixels[ i * 4 + 1] != 0 or pixels[i * 4 + 2] != 0 has_alpha = has_alpha or pixels[i * 4 + 3] != 0 if has_pixels and has_alpha: break if has_pixels and not has_alpha: #generate missing alpha - don't ask me why for i in range(len(pixels) // 4): if pixels[i * 4] != 0 or pixels[i * 4 + 1] != 0 or pixels[i * 4 + 2] != 0: pixels[i * 4 + 3] = 0xff return [0, 0, w, h, x, y, hCursor, bytes(pixels), strtobytes(name)] except Exception as e: cursorlog("get_cursor_data(%#x)", hCursor, exc_info=True) cursorlog.error("Error: failed to grab cursor:") cursorlog.error(" %s", str(e) or type(e)) return None finally: if old_handle: SelectObject(memdc, old_handle) if bitmap: DeleteObject(bitmap) if memdc: DeleteDC(memdc) if dc: ReleaseDC(None, dc)