def _cp_boundary(dest_dc, dest_x0, dest_y0, src_dc, src_x0, src_y0, w, h): win32gui.BitBlt(dest_dc, dest_x0 + 0, dest_y0 + 0, w, 1, src_dc, src_x0, src_y0, win32con.SRCCOPY) win32gui.BitBlt(dest_dc, dest_x0 + 0, dest_y0 + h - 1, w, 1, src_dc, src_x0, src_y0 + h - 1, win32con.SRCCOPY) win32gui.BitBlt(dest_dc, dest_x0 + 0, dest_y0 + 1, 1, h - 2, src_dc, src_x0, src_y0, win32con.SRCCOPY) win32gui.BitBlt(dest_dc, dest_x0 + w - 1, dest_y0 + 1, 1, h - 2, src_dc, src_x0 + w - 1, src_y0, win32con.SRCCOPY)
def get_screen_shot(self, x: int = None, y: int = None, width: int = None, height: int = None) -> Image: x = x or 0 y = y or 0 hwnd = self._window left, top, right, bottom = win32gui.GetWindowRect(hwnd) if not width: width = right - left if not height: height = bottom - top hwnd_dc = win32gui.GetWindowDC(hwnd) mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc) save_dc = mfc_dc.CreateCompatibleDC() save_dc.SetWindowOrg((0, 0)) # 13, 151)) save_bit_map = win32ui.CreateBitmap() save_bit_map.CreateCompatibleBitmap(mfc_dc, width, height) # 287, 76) save_dc.SelectObject(save_bit_map) # result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0) win32gui.BitBlt(save_dc.GetSafeHdc(), 0, 0, width, height, hwnd_dc, x, y, win32con.SRCCOPY) bmp_info = save_bit_map.GetInfo() bmp_str = save_bit_map.GetBitmapBits(True) im = PIL.Image.frombuffer( 'RGB', (bmp_info['bmWidth'], bmp_info['bmHeight']), bmp_str, 'raw', 'BGRX', 0, 1) win32gui.DeleteObject(save_bit_map.GetHandle()) save_dc.DeleteDC() mfc_dc.DeleteDC() win32gui.ReleaseDC(hwnd, hwnd_dc) return im
def capture_window(cls, handle): assert env.is_windows() left, top, right, bottom = win32gui.GetWindowRect(handle) width, height = right - left, bottom - top hwnd_dc = win32gui.GetWindowDC(handle) mfc_dc = win32ui.CreateDCFromHandle(hwnd_dc) save_dc = mfc_dc.CreateCompatibleDC() save_dc.SetWindowOrg((0, 0)) # 13, 151)) save_bit_map = win32ui.CreateBitmap() save_bit_map.CreateCompatibleBitmap(mfc_dc, width, height) # 287, 76) save_dc.SelectObject(save_bit_map) # result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0) win32gui.BitBlt(save_dc.GetSafeHdc(), 0, 0, width, height, hwnd_dc, 0, 0, win32con.SRCCOPY) bmp_info = save_bit_map.GetInfo() bmp_str = save_bit_map.GetBitmapBits(True) im = Image.frombuffer( 'RGB', (bmp_info['bmWidth'], bmp_info['bmHeight']), bmp_str, 'raw', 'BGRX', 0, 1) win32gui.DeleteObject(save_bit_map.GetHandle()) save_dc.DeleteDC() mfc_dc.DeleteDC() win32gui.ReleaseDC(handle, hwnd_dc) return im
def screen(self): """PIL Image of current window screen. (the window must be on the top) reference: https://msdn.microsoft.com/en-us/library/dd183402(v=vs.85).aspx""" # opengl windows cannot get from it's hwnd, so we use the screen hwnd = win32gui.GetDesktopWindow() # get window size and offset left, top, right, bottom = self.rect width, height = right - left, bottom - top # the device context of the window hdcwin = win32gui.GetWindowDC(hwnd) # make a temporary dc hdcmem = win32gui.CreateCompatibleDC(hdcwin) # make a temporary bitmap in memory, this is a PyHANDLE object hbmp = win32gui.CreateCompatibleBitmap(hdcwin, width, height) # select bitmap for temporary dc win32gui.SelectObject(hdcmem, hbmp) # copy bits to temporary dc win32gui.BitBlt(hdcmem, 0, 0, width, height, hdcwin, left, top, win32con.SRCCOPY) # check the bitmap object infomation bmp = win32gui.GetObject(hbmp) bi = BITMAPINFOHEADER() bi.biSize = ctypes.sizeof(BITMAPINFOHEADER) bi.biWidth = bmp.bmWidth bi.biHeight = bmp.bmHeight bi.biPlanes = bmp.bmPlanes bi.biBitCount = bmp.bmBitsPixel bi.biCompression = 0 # BI_RGB bi.biSizeImage = 0 bi.biXPelsPerMeter = 0 bi.biYPelsPerMeter = 0 bi.biClrUsed = 0 bi.biClrImportant = 0 # calculate total size for bits pixel = bmp.bmBitsPixel size = ((bmp.bmWidth * pixel + pixel - 1) / pixel) * 4 * bmp.bmHeight buf = (ctypes.c_char * size)() # read bits into buffer windll.gdi32.GetDIBits(hdcmem, hbmp.handle, 0, bmp.bmHeight, buf, ctypes.byref(bi), win32con.DIB_RGB_COLORS) # make a PIL Image img = Image.frombuffer('RGB', (bmp.bmWidth, bmp.bmHeight), buf, 'raw', 'BGRX', 0, 1) img = img.transpose(Image.FLIP_TOP_BOTTOM) # cleanup win32gui.DeleteObject(hbmp) win32gui.DeleteObject(hdcmem) win32gui.ReleaseDC(hwnd, hdcwin) return img
def display(): chat_region = (40, 753, 400, 200) foreground = win32gui.GetForegroundWindow() dc = win32gui.GetWindowDC(foreground) pirate_win = win32gui.FindWindow(None, "The Legend of Pirates Online [BETA]") pirate_dc = win32gui.GetWindowDC(pirate_win) win32gui.BitBlt(dc, chat_region[0], chat_region[1], chat_region[2], chat_region[3], pirate_dc, chat_region[0], chat_region[1], win32con.SRCCOPY) win32gui.ReleaseDC(foreground, dc) win32gui.ReleaseDC(pirate_win, pirate_dc)
def screen_cv2(self): """cv2 Image of current window screen""" hwnd = win32gui.GetDesktopWindow() left, top, right, bottom = self.rect width, height = right-left, bottom-top # copy bits to temporary dc win32gui.BitBlt(self._hdcmem, 0, 0, width, height, self._hdcwin, left, top, win32con.SRCCOPY) # read bits into buffer windll.gdi32.GetDIBits(self._hdcmem, self._hbmp.handle, 0, height, self._buf, ctypes.byref(self._bi), win32con.DIB_RGB_COLORS) # make a cv2 Image arr = np.fromstring(self._buf, dtype=np.uint8) img = arr.reshape(height, width, 4) img = img[::-1,:, 0:3] return img
def screen(self): """PIL Image of current window screen. reference: https://msdn.microsoft.com/en-us/library/dd183402(v=vs.85).aspx""" hwnd = win32gui.GetDesktopWindow() left, top, right, bottom = self.rect width, height = right-left, bottom-top # copy bits to temporary dc win32gui.BitBlt(self._hdcmem, 0, 0, width, height, self._hdcwin, left, top, win32con.SRCCOPY) # read bits into buffer windll.gdi32.GetDIBits(self._hdcmem, self._hbmp.handle, 0, height, self._buf, ctypes.byref(self._bi), win32con.DIB_RGB_COLORS) # make a PIL Image img = Image.frombuffer('RGB', (width, height), self._buf, 'raw', 'BGRX', 0, 1) img = img.transpose(Image.FLIP_TOP_BOTTOM) return img
def _take_screenshot_with_native_api(self, x, y, w, h, hwnd): if hwnd is None: scr_hdc = win32gui.CreateDC('DISPLAY', None, None) else: scr_hdc = win32gui.GetDC(hwnd) mem_hdc = win32gui.CreateCompatibleDC(scr_hdc) new_bitmap_h = win32gui.CreateCompatibleBitmap(scr_hdc, w, h) win32gui.SelectObject( mem_hdc, new_bitmap_h ) # Returns 'old_bitmap_h'. It will be deleted automatically. win32gui.BitBlt(mem_hdc, 0, 0, w, h, scr_hdc, x, y, win32con.SRCCOPY) bmp = win32ui.CreateBitmapFromHandle(new_bitmap_h) bmp_info = bmp.GetInfo() if bmp_info['bmHeight'] != h or bmp_info['bmWidth'] != w: raise FailExit('bmp_info = {bmp}, but (w, h) = ({w}, {h})'.format( bmp=bmp_info, width=w, height=h)) if bmp_info['bmType'] != 0 or bmp_info['bmPlanes'] != 1: raise FailExit( 'bmp_info = {bmp}: bmType !=0 or bmPlanes != 1'.format( bmp=str(bmp_info))) if bmp_info['bmBitsPixel'] % 8 != 0: raise FailExit( 'bmp_info = {bmp}: bmBitsPixel mod. 8 is not zero'.format( bmp=str(bmp_info))) bmp_arr = list(bmp.GetBitmapBits()) if len(bmp_arr) == w * h * 4: del bmp_arr[3:: 4] # Delete alpha channel. TODO: Is it fast enough??? elif len(bmp_arr) != w * h * 3: raise FailExit('An error occurred while read bitmap bits') result = np.array(bmp_arr, dtype=np.uint8).reshape((h, w, 3)) win32gui.DeleteDC(mem_hdc) win32gui.DeleteObject(new_bitmap_h) if not hwnd: win32gui.DeleteDC(scr_hdc) else: win32gui.ReleaseDC(hwnd, scr_hdc) return result
def get_color(pos): hdc_screen = win32gui.CreateDC("DISPLAY", "", None) hmem_dc = win32gui.CreateCompatibleDC(hdc_screen) h_bitmap = win32gui.CreateCompatibleBitmap(hdc_screen, 1, 1) h_old_bitmap = win32gui.SelectObject(hmem_dc, h_bitmap) win32gui.BitBlt(hmem_dc, 0, 0, 1, 1, hdc_screen, pos[0], pos[1], win32con.SRCCOPY) win32gui.DeleteDC(hdc_screen) win32gui.DeleteDC(hmem_dc) x = win32ui.CreateBitmapFromHandle(h_bitmap) bits = x.GetBitmapBits(True) return struct.unpack('I', bits)[0]
def WndProc(self, hWnd, message, wParam, lParam): if message == win32con.WM_PAINT: rect = win32gui.GetClientRect(hWnd) # tuple(L,T,R,B) hDC, paintStruct = win32gui.BeginPaint(hWnd) win32gui.SetTextColor(hDC, win32api.RGB(255, 0, 0)) win32gui.SetBkMode(hDC, 1) rect2 = (rect[0] + 10, rect[1] + 10, rect[2], rect[3]) rect3 = (rect[0] + 10, rect[1] + 30, rect[2], rect[3]) win32gui.DrawText(hDC, 'Overlay Test', -1, rect2, win32con.DT_SINGLELINE | win32con.DT_NOCLIP) win32gui.DrawText(hDC, 'Test message2', -1, rect3, win32con.DT_SINGLELINE | win32con.DT_NOCLIP) ### memDC = win32gui.CreateCompatibleDC(hDC) image1 = win32gui.LoadImage(None, "1111.bmp", win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE) OldBitmap = win32gui.SelectObject(memDC, image1) if image1 == 0: print("image load error") exit(1) win32gui.BitBlt(hDC, 10, 50, 100, 100, memDC, 0, 0, win32con.SRCCOPY) win32gui.SelectObject(memDC, OldBitmap) win32gui.DeleteObject(image1) win32gui.DeleteObject(memDC) win32gui.EndPaint(hWnd, paintStruct) elif message == win32con.WM_DESTROY: print("Being destroyed") win32gui.PostQuitMessage(0) else: return win32gui.DefWindowProc(hWnd, message, wParam, lParam)
import win32con import win32ui #hinstance = win32api.GetModuleHandle(None) hWnd = win32gui.GetDesktopWindow() # Retrieves a handle to the desktop window. The desktop window covers the entire screen. # The desktop window is the area on top of which other windows are painted. hdc = win32gui.GetDC(hWnd) #win32gui.GetDC(None) # A handle to the window whose DC is to be retrieved. If this value is NULL, GetDC # retrieves the DC for the entire screen. hMemDC = win32gui.CreateCompatibleDC(hdc) hImage = win32gui.LoadImage( None, "./test.png", win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE | win32con.LR_CREATEDIBSECTION) # Loads an icon, cursor, animated cursor, or bitmap hOldBitmap = win32gui.SelectObject(hMemDC, hImage) win32gui.BitBlt(hdc, 50, 50, 50 + 400, 50 + 272, hMemDC, 0, 0, win32con.SRCCOPY) # Image(400, 272) at (50, 50) # The BitBlt function performs a bit-block transfer of the color data corresponding to a rectangle of pixels # from the specified source device context into a destination device context. win32gui.SelectObject(hMemDC, hOldBitmap) win32gui.DeleteObject(hImage) win32gui.DeleteDC(hMemDC) win32gui.ReleaseDC(hWnd, hdc)
def inversecolor(): win32gui.BitBlt(t, 0, 0, w, h, t, 0, 0, NOTSRCCOPY)