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 _save_selection(self): """Save selection region from the original screenshot.""" assert self.filename is not None assert self._selection is not None left, top, right, bottom = self._selection width = right - left height = bottom - top src_dch = win32gui.CreateCompatibleDC(None) src_dc = win32ui.CreateDCFromHandle(src_dch) src_default = src_dc.SelectObject(self._screenshot) assert src_default is not None dst_dch = win32gui.CreateCompatibleDC(None) dst_dc = win32ui.CreateDCFromHandle(dst_dch) dst_bitmap = win32ui.CreateBitmap() dst_bitmap.CreateCompatibleBitmap(src_dc, width, height) dst_default = dst_dc.SelectObject(dst_bitmap) assert dst_default is not None dst_dc.BitBlt((0, 0), (width, height), src_dc, (left, top), win32con.SRCCOPY) dst_bitmap.SaveBitmapFile(dst_dc, self.filename) self.logger.info("Saved selected region as '%s'", self.filename) dst_dc.SelectObject(dst_default) src_dc.SelectObject(src_default) win32gui.DeleteObject(dst_bitmap.GetHandle()) dst_dc.DeleteDC() src_dc.DeleteDC()
def OnPaint(self, hwnd, msg, wp, lp): dc, ps = win32gui.BeginPaint(hwnd) wndrect = win32gui.GetClientRect(hwnd) wndwidth = wndrect[2] - wndrect[0] wndheight = wndrect[3] - wndrect[1] win32clipboard.OpenClipboard() try: try: hbitmap = win32clipboard.GetClipboardData( win32clipboard.CF_BITMAP) except TypeError: font = win32gui.LOGFONT() font.lfHeight = 15 # int(wndheight/20) font.lfWidth = 15 # font.lfHeight # font.lfWeight=150 hf = win32gui.CreateFontIndirect(font) win32gui.SelectObject(dc, hf) win32gui.SetBkMode(dc, win32con.TRANSPARENT) win32gui.SetTextColor(dc, win32api.RGB(0, 0, 0)) win32gui.DrawText( dc, "No bitmaps are in the clipboard\n(try pressing the PrtScn button)", -1, (0, 0, wndwidth, wndheight), win32con.DT_CENTER, ) else: bminfo = win32gui.GetObject(hbitmap) dcDC = win32gui.CreateCompatibleDC(None) win32gui.SelectObject(dcDC, hbitmap) win32gui.StretchBlt( dc, 0, 0, wndwidth, wndheight, dcDC, 0, 0, bminfo.bmWidth, bminfo.bmHeight, win32con.SRCCOPY, ) win32gui.DeleteDC(dcDC) win32gui.EndPaint(hwnd, ps) finally: win32clipboard.CloseClipboard() return 0
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 OnPaint(self, hWnd, msg, wparam, lparam): dc, ps = win32gui.BeginPaint(hWnd) wndrect = win32gui.GetClientRect(hWnd) wndwidth = wndrect[2] - wndrect[0] wndheight = wndrect[3] - wndrect[1] if os.path.isfile(FILENAME): hBitmap = win32gui.LoadImage(0, FILENAME, win32con.IMAGE_BITMAP, 0, 0, win32con.LR_LOADFROMFILE) hdcBitmap = win32gui.CreateCompatibleDC(dc) hOldBitmap = win32gui.SelectObject(hdcBitmap, hBitmap) bminfo = win32gui.GetObject(hBitmap) win32gui.SetStretchBltMode(dc, win32con.COLORONCOLOR) win32gui.StretchBlt(dc, 0, 0, wndwidth, wndheight, hdcBitmap, 0, 0, bminfo.bmWidth, bminfo.bmHeight, win32con.SRCCOPY) win32gui.SelectObject(hdcBitmap, hOldBitmap) win32gui.EndPaint(hWnd, ps) return 0
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 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 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 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(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 getExtents(self, text=None, style=None): if text == None: text = str(self.text) if style == None: style = self.style txtext = {} dc = win32gui.CreateCompatibleDC(0) win32gui.SetMapMode(dc, win32con.MM_TEXT) font = self.selectFont(dc, style, 64) if style.spacing: for char in text: cx, cy = win32gui.GetTextExtentPoint32(dc, char) txtext['width'] += cx + (style.spacing * 64) txtext['height'] = cy else: cx, cy = win32gui.GetTextExtentPoint32(dc, text) txtext['width'] = cx txtext['height'] = cy tm = win32gui.GetTextMetrics(dc) txtext['ascent'] = tm['Ascent'] txtext['descent'] = tm['Descent'] txtext['internal_lead'] = tm['InternalLeading'] txtext['external_lead'] = tm['ExternalLeading'] txtext['width'] = txtext['width'] * (style.scale_x / float(100)) / float(64) txtext['height'] = txtext['height'] * (style.scale_y / float(100)) / float(64) txtext['ascent'] = txtext['ascent'] * (style.scale_y / float(100)) / float(64) txtext['descent'] = txtext['descent'] * (style.scale_y / float(100)) / float(64) txtext['internal_lead'] = txtext['internal_lead'] * ( style.scale_y / float(100)) / float(64) txtext['external_lead'] = txtext['external_lead'] * ( style.scale_y / float(100)) / float(64) return txtext
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 update(self, src_hdl, fps): """Met à jour les paramètres du stream.""" if sys.platform == 'win32': if isinstance(src_hdl, str): if not src_hdl == "" and not src_hdl == entry_hint: self._src_hdl = int(src_hdl, 16) else: self._src_hdl = 0x00010010 elif isinstance(src_hdl, int): pass else: log.add( "Entrée incorrecte, l'entrée utilisateur devrait être un int, mais est de type : " + str(type(src_hdl))) self.src_window_dc = win32gui.GetDC(self._src_hdl) self.dest_window_dc = win32gui.GetDC(self._dest_hdl) # Crée un DC compatible pour la sauvegarde self.mem_dc = win32gui.CreateCompatibleDC(self.dest_window_dc) if self.mem_dc is None: log.add("Échec de la création du compatibleDC.") return self.dest_rect_dc = win32gui.GetClientRect(self._dest_hdl) self.src_rect_dc = win32gui.GetClientRect(self._src_hdl) win32gui.SetStretchBltMode(self.dest_window_dc, win.HALFTONE) self.timer.cancel() self.timer = InfiniteTimer(1 / float(fps), self.run) if self._hasToRun: self.timer.start() log.add("Paramètres stream rafraîchis.") elif sys.platform == 'linux': """TODO : . . . ...""" pass
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
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)
def window_capture(filename): hwnd = win32gui.GetDesktopWindow() dc = win32gui.GetDC(hwnd) win32gui.CreateCompatibleDC()
import win32api import win32gui 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)
dm.Orientation = win32con.DMORIENT_LANDSCAPE 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)
def setup(self, alpha=0xff): global hWnd, hDC, hRC, hbitmap hwnd = pygame.display.get_wm_info()["window"] if SOFTWARE_RENDER: hWnd = pygame.display.get_wm_info()["window"] hdcwnd = win32gui.GetDC(hWnd) hDC = win32gui.CreateCompatibleDC(hdcwnd) win32gui.SetWindowLong( hWnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hWnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED) bmi = BITMAPINFO() bmi.bmiHeader.biSize = ctypes.sizeof(BITMAPINFOHEADER) bmi.bmiHeader.biWidth = self.WIDTH bmi.bmiHeader.biHeight = self.HEIGHT bmi.bmiHeader.biPlanes = 1 bmi.bmiHeader.biBitCount = 32 bmi.bmiHeader.biCompression = win32con.BI_RGB bmi.bmiHeader.biSizeImage = self.WIDTH * self.HEIGHT * 4 hbitmap = ctypes.windll.gdi32.CreateDIBSection( hDC, ctypes.byref(bmi), win32con.DIB_RGB_COLORS, ctypes.byref(ctypes.c_void_p()), None, 0) win32gui.SelectObject(hDC, hbitmap) PFD_TYPE_RGBA = 0 PFD_MAIN_PLANE = 0 PFD_DRAW_TO_BITMAP = 0x00000008 PFD_SUPPORT_GDI = 0x00000010 PFD_SUPPORT_OPENGL = 0x00000020 dwFlags = PFD_SUPPORT_OPENGL | PFD_DRAW_TO_BITMAP | PFD_SUPPORT_GDI pfd = PIXELFORMATDESCRIPTOR() pfd.nSize = ctypes.sizeof(PIXELFORMATDESCRIPTOR) pfd.nVersion = 1 pfd.dwFlags = dwFlags pfd.iPixelType = PFD_TYPE_RGBA pfd.cColorBits = 32 pfd.cDepthBits = 32 pfd.cStencilBits = 8 pfd.iLayerType = PFD_MAIN_PLANE # hdc = win32gui.GetDC(hWnd) pixelformat = ChoosePixelFormat(hDC, pfd) SetPixelFormat(hDC, pixelformat, pfd) hRC = wglCreateContext(hDC) # win32gui.ReleaseDC(hWnd, hdc) wglMakeCurrent(hDC, hRC) win32gui.ShowWindow(hwnd, win32con.SW_SHOWNOACTIVATE) win32gui.SetWindowPos( hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOSIZE | win32con.SWP_NOACTIVATE) cur_style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, cur_style | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_LAYERED) else: win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) | win32con.WS_EX_LAYERED | win32con.WS_EX_TOOLWINDOW) win32gui.SetParent(hwnd, win32con.NULL) win32gui.SetLayeredWindowAttributes(hwnd, win32api.RGB(*COLORKEY), 0, win32con.LWA_COLORKEY) win32gui.ShowWindow(hwnd, win32con.SW_SHOWNOACTIVATE) win32gui.SetWindowPos( hwnd, win32con.HWND_TOPMOST, 0, 0, 0, 0, win32con.SWP_NOSIZE | win32con.SWP_NOACTIVATE) cur_style = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE) win32gui.SetWindowLong( hwnd, win32con.GWL_EXSTYLE, cur_style | win32con.WS_EX_TRANSPARENT | win32con.WS_EX_LAYERED)
def __init__(self, style: Style): self.family = style.fontname self.bold = style.bold self.italic = style.italic self.underline = style.underline self.strikeout = style.strikeout self.size = style.fontsize self.xscale = style.scale_x / 100 self.yscale = style.scale_y / 100 self.hspace = style.spacing self.upscale = FONT_PRECISION self.downscale = 1 / FONT_PRECISION if sys.platform == "win32": # Create device context self.dc = win32gui.CreateCompatibleDC(None) # Set context coordinates mapping mode win32gui.SetMapMode(self.dc, win32con.MM_TEXT) # Set context backgrounds to transparent win32gui.SetBkMode(self.dc, win32con.TRANSPARENT) # Create font handle font_spec = { "height": int(self.size * self.upscale), "width": 0, "escapement": 0, "orientation": 0, "weight": win32con.FW_BOLD if self.bold else win32con.FW_NORMAL, "italic": int(self.italic), "underline": int(self.underline), "strike out": int(self.strikeout), "charset": win32con.DEFAULT_CHARSET, "out precision": win32con.OUT_TT_PRECIS, "clip precision": win32con.CLIP_DEFAULT_PRECIS, "quality": win32con.ANTIALIASED_QUALITY, "pitch and family": win32con.DEFAULT_PITCH + win32con.FF_DONTCARE, "name": self.family, } self.pycfont = win32ui.CreateFont(font_spec) win32gui.SelectObject(self.dc, self.pycfont.GetSafeHandle()) # Calculate metrics self.metrics = win32gui.GetTextMetrics(self.dc) elif sys.platform == "linux" or sys.platform == "darwin": surface = cairo.ImageSurface(cairo.Format.A8, 1, 1) self.context = cairo.Context(surface) self.layout = PangoCairo.create_layout(self.context) font_description = Pango.FontDescription() font_description.set_family(self.family) font_description.set_absolute_size(self.size * self.upscale * PANGO_SCALE) font_description.set_weight( Pango.Weight.BOLD if self.bold else Pango.Weight.NORMAL) font_description.set_style( Pango.Style.ITALIC if self.italic else Pango.Style.NORMAL) self.layout.set_font_description(font_description) self.metrics = Pango.Context.get_metrics( self.layout.get_context(), self.layout.get_font_description()) if LIBASS_FONTHACK: self.fonthack_scale = self.size / ( (self.metrics.get_ascent() + self.metrics.get_descent()) / PANGO_SCALE * self.downscale) else: self.fonthack_scale = 1 else: raise NotImplementedError