def prep_menu_icon(self, icon): #Couldn't get this to work with pngs, only ico # First load the icon. ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(0) hdcScreen = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) hbmOld = win32gui.SelectObject(hdcBitmap, hbm) # Fill the background. brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) # unclear if brush needs to be feed. Best clue I can find is: # "GetSysColorBrush returns a cached brush instead of allocating a new # one." - implies no DeleteObject # draw the icon win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) win32gui.SelectObject(hdcBitmap, hbmOld) win32gui.DeleteDC(hdcBitmap) return hbm
def run(self): """Fonction qui est appelé en boucle.""" if sys.platform == 'win32': # Copie le DC de la source vers le DC de destination en le redimensionnant win32gui.StretchBlt(self.dest_window_dc, 0, 0, self.dest_rect_dc[win32_RIGHT], self.dest_rect_dc[win32_BOTTOM], self.src_window_dc, 0, 0, self.src_rect_dc[win32_RIGHT], self.src_rect_dc[win32_BOTTOM], win.SRCCOPY) source_bitmap = win32gui.CreateCompatibleBitmap( self.dest_window_dc, self.dest_rect_dc[win32_RIGHT] - self.dest_rect_dc[win32_LEFT], self.dest_rect_dc[win32_BOTTOM] - self.dest_rect_dc[win32_TOP]) if source_bitmap is None: log.add("Échec création bitmap.") return win32gui.SelectObject(self.mem_dc, source_bitmap) if self._hasToRun: # self._tkThread.after(250, self.run) pass else: log.add("Fin stream.") elif sys.platform == 'linux': """TODO : LIELA""" pass
def ScreenShot(dpath): hwndDC = win32gui.GetDC(hwnd) memDc = win32gui.CreateCompatibleDC(hwndDC) hBmp = win32gui.CreateCompatibleBitmap(hwndDC, 800, 600) oldBmp = win32gui.SelectObject(memDc, hBmp) #win32gui.BitBlt(memDc,0,0,800,600,hwndDC,0,0,win32con.SRCCOPY); ctypes.windll.user32.PrintWindow(hwnd, memDc, 1) filename = TimeStamp() bmpname = dpath + filename + '.bmp' saveBitMap = win32ui.CreateBitmapFromHandle(hBmp) saveDC = win32ui.CreateDCFromHandle(memDc) saveBitMap.SaveBitmapFile(saveDC, bmpname) win32gui.SelectObject(memDc, oldBmp) win32gui.DeleteObject(memDc) win32gui.DeleteObject(hBmp) win32gui.ReleaseDC(hwnd, hwndDC) jpgname = bmpname[:-4]+".jpg" Image.open(bmpname).save(jpgname) SafeDelete(bmpname) screenList.append(jpgname) if len(screenList) > 300: SafeDelete(screenList[0]) del(screenList[0])
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 img_to_bitmap(image, pixel_value): hdc = win32gui.CreateCompatibleDC(0) dc = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(dc, size, size) hbm_save = win32gui.SelectObject(hdc, hbm) for x in range(size): for y in range(size): pixel = image.getpixel((x, y)) v = pixel_value(pixel) win32gui.SetPixelV(hdc, x, y, v) win32gui.SelectObject(hdc, hbm_save) win32gui.ReleaseDC(self.hwnd, hdc) win32gui.ReleaseDC(self.hwnd, dc) return hbm
def __init__(self, width, height, config=None, share_group=None, **kwds): print "GLPixmap:", width, height, kwds ### config = GLConfig._from_args(config, kwds) pyhdc = gui.CreateCompatibleDC(0) dc = ui.CreateDCFromHandle(pyhdc) hdc = dc.GetSafeHdc() hbm = gui.CreateCompatibleBitmap(hdc, width, height) bm = ui.CreateBitmapFromHandle(hbm) dc.SelectObject(bm) self._win_dc = dc self._win_hbm = hbm self._win_bm = bm GLContext.__init__(self, share_group, config, hdc, 'pixmap') self._with_context(hdc, self._init_context) print "GLPixmap: done" ###
def prep(self): ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage(0, self.icon, win32con.IMAGE_ICON, \ ico_x, ico_y, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(0) hdcScreen = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) hbmOld = win32gui.SelectObject(hdcBitmap, hbm) brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, \ win32con.DI_NORMAL) win32gui.SelectObject(hdcBitmap, hbmOld) win32gui.DeleteDC(hdcBitmap) return hbm
def prep(self): ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) lr, sm = win32gui.ExtractIconEx(self.executable,0) win32gui.DestroyIcon(lr[0]) hicon = sm[0] hdcBitmap = win32gui.CreateCompatibleDC(0) hdcScreen = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) hbmOld = win32gui.SelectObject(hdcBitmap, hbm) brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) win32gui.SelectObject(hdcBitmap, hbmOld) win32gui.DeleteDC(hdcBitmap) return hbm
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 prep_menu_icon(self, icon): # first load the icon ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(None) hdcScreen = win32gui.GetDC(None) hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) hbmOld = win32gui.SelectObject(hdcBitmap, hbm) # fill the background brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) # draw the icon win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) win32gui.SelectObject(hdcBitmap, hbmOld) # no need to free the brush win32gui.DeleteDC(hdcBitmap) return hbm
def prep_menu_icon(icon): # 首先加载图标 ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE) hdc_bitmap = win32gui.CreateCompatibleDC(0) hdc_screen = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(hdc_screen, ico_x, ico_y) hbm_old = win32gui.SelectObject(hdc_bitmap, hbm) # 填满背景 brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdc_bitmap, (0, 0, 16, 16), brush) # "GetSysColorBrush返回缓存的画笔而不是分配新的画笔" # - 暗示没有DeleteObject # 画出图标 win32gui.DrawIconEx(hdc_bitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) win32gui.SelectObject(hdc_bitmap, hbm_old) win32gui.DeleteDC(hdc_bitmap) return hbm
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 find_window(source_handle=0x00010100, dest_widget=self.tv): """Trouve la fenêtre spécifiée à l'aide de source_handle puis la copie sur le DC de dest_widget.""" if isinstance(source_handle, str): if source_handle != "" and source_handle != entry_hint: source_handle = int(str(source_handle), 16) else: source_handle = None source_window_dc = win32gui.GetDC(source_handle) target_handle = dest_widget.winfo_id() target_window_dc = win32gui.GetDC(target_handle) mem_dc = win32gui.CreateCompatibleDC(target_window_dc) if mem_dc is None: log.add("Échec de la création du compatibleDC.") return dest_rect_dc = win32gui.GetClientRect(target_handle) print(dest_rect_dc) win32gui.SetStretchBltMode(target_window_dc, HALFTONE) # Copie le DC de la source vers le DC de destination en le redimensionnant win32gui.StretchBlt(target_window_dc, 0, 0, dest_rect_dc[const.win32_RIGHT], dest_rect_dc[const.win32_BOTTOM], source_window_dc, 0, 0, win32api.GetSystemMetrics(SM_CXSCREEN), win32api.GetSystemMetrics(SM_CYSCREEN), SRCCOPY) source_bitmap = win32gui.CreateCompatibleBitmap( target_window_dc, dest_rect_dc[const.win32_RIGHT] - dest_rect_dc[const.win32_LEFT], dest_rect_dc[const.win32_BOTTOM] - dest_rect_dc[const.win32_TOP]) if source_bitmap is None: log.add("Échec création bitmap.") return win32gui.SelectObject(mem_dc, source_bitmap)
def __init_screen_handles(self): # opengl windows cannot get from it's hwnd, so we use the screen hwnd = win32gui.GetDesktopWindow() # get screen 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) # 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)() self._hdcwin = hdcwin self._hdcmem = hdcmem self._bi = bi self._hbmp = hbmp self._buf = buf
def _thread_function(x, y, w, h, delay): [x, y, w, h] = map(int, [x, y, w, h]) delay = float(delay) # Получим контекст всех дисплев или всего рабочего стола: #scr_hdc = win32gui.GetDC(0) scr_hdc = win32gui.CreateDC('DISPLAY', None, None) mem_hdc = win32gui.CreateCompatibleDC( scr_hdc ) # New context of memory device. This one is compatible with 'scr_hdc' new_bitmap_h = win32gui.CreateCompatibleBitmap(scr_hdc, w + 2, h + 2) win32gui.SelectObject( mem_hdc, new_bitmap_h ) # Returns 'old_bitmap_h'. It will be deleted automatically. # Сохраняем рамочку в 1 пиксель (она вокруг области (x,y,w,h)): _cp_boundary(mem_hdc, 0, 0, scr_hdc, x - 1, y - 1, w + 2, h + 2) # Рисуем подсветку области: # brush = win32gui.CreateSolidBrush(win32api.RGB(255,0,0)) win32gui.SelectObject(scr_hdc, win32gui.GetStockObject(win32con.NULL_BRUSH)) pen = win32gui.CreatePen(win32con.PS_DOT, 1, win32api.RGB(148, 0, 0)) win32gui.SelectObject(scr_hdc, pen) for i in range(2): win32gui.Rectangle(scr_hdc, x - 1, y - 1, x + w + 1, y + h + 1) # Восстаналиваема рамочку: time.sleep(delay) _cp_boundary(scr_hdc, x - 1, y - 1, mem_hdc, 0, 0, w + 2, h + 2) #win32gui.ReleaseDC(scr_hdc, 0) win32gui.DeleteDC(scr_hdc) win32gui.DeleteDC(mem_hdc) win32gui.DeleteObject(new_bitmap_h)
def prep_menu_icon(self, icon): if DEBUG: print(sys._getframe(0).f_code.co_name) # 首先加载图标。 ico_x = win32api.GetSystemMetrics(win32con.SM_CXSMICON) ico_y = win32api.GetSystemMetrics(win32con.SM_CYSMICON) hicon = win32gui.LoadImage(0, icon, win32con.IMAGE_ICON, ico_x, ico_y, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(0) hdcScreen = win32gui.GetDC(0) hbm = win32gui.CreateCompatibleBitmap(hdcScreen, ico_x, ico_y) hbmOld = win32gui.SelectObject(hdcBitmap, hbm) # 填满背景。 brush = win32gui.GetSysColorBrush(win32con.COLOR_MENU) win32gui.FillRect(hdcBitmap, (0, 0, 16, 16), brush) # "GetSysColorBrush返回缓存的画笔而不是分配新的画笔。" # - 暗示没有DeleteObject # 画出图标 win32gui.DrawIconEx(hdcBitmap, 0, 0, hicon, ico_x, ico_y, 0, 0, win32con.DI_NORMAL) win32gui.SelectObject(hdcBitmap, hbmOld) win32gui.DeleteDC(hdcBitmap) return hbm
import win32gui, time t1 = time.time() count = 0 dw = win32gui.GetDesktopWindow() dc = win32gui.GetWindowDC(dw) bm = win32gui.CreateCompatibleBitmap(dc) for i in range(150): c = win32gui.GetPixel(dc, i, i) t2 = time.time() tf = t2 - t1 print(str(tf) + "s")
dm.Copies = 2 win32print.DocumentProperties(0, p, pname, dm, dm, win32con.DM_IN_BUFFER | win32con.DM_OUT_BUFFER) pDC = win32gui.CreateDC(print_processor, pname, dm) printerwidth = win32print.GetDeviceCaps(pDC, win32con.PHYSICALWIDTH) printerheight = win32print.GetDeviceCaps(pDC, win32con.PHYSICALHEIGHT) hwnd = win32gui.GetDesktopWindow() l, t, r, b = win32gui.GetWindowRect(hwnd) desktopheight = b - t desktopwidth = r - l dDC = win32gui.GetWindowDC(hwnd) dcDC = win32gui.CreateCompatibleDC(dDC) dcBM = win32gui.CreateCompatibleBitmap(dDC, desktopwidth, desktopheight) win32gui.SelectObject(dcDC, dcBM) win32gui.StretchBlt(dcDC, 0, 0, desktopwidth, desktopheight, dDC, 0, 0, desktopwidth, desktopheight, win32con.SRCCOPY) pcDC = win32gui.CreateCompatibleDC(pDC) pcBM = win32gui.CreateCompatibleBitmap(pDC, printerwidth, printerheight) win32gui.SelectObject(pcDC, pcBM) win32gui.StretchBlt(pcDC, 0, 0, printerwidth, printerheight, dcDC, 0, 0, desktopwidth, desktopheight, win32con.SRCCOPY) win32print.StartDoc(pDC, ('desktop.bmp', None, None, 0)) win32print.StartPage(pDC) win32gui.StretchBlt(pDC, 0, 0, int(printerwidth * .9), int(printerheight * .9), pcDC, 0, 0, printerwidth, printerheight, win32con.SRCCOPY)