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 getMat(self): hdc = win32gui.GetWindowDC(self.hwnd) dc_obj = win32ui.CreateDCFromHandle(hdc) memorydc = dc_obj.CreateCompatibleDC() data_bitmap = win32ui.CreateBitmap() data_bitmap.CreateCompatibleBitmap(dc_obj, self.width, self.height) memorydc.SelectObject(data_bitmap) memorydc.BitBlt((0, 0), (self.width, self.height), dc_obj, (self.dx, self.dy), win32con.SRCCOPY) bmpheader = struct.pack("LHHHH", struct.calcsize("LHHHH"), self.width, self.height, 1, 24) c_bmpheader = ctypes.create_string_buffer(bmpheader) # padded_length = (string_length + 3) & -3 for 4-byte aligned. c_bits = ctypes.create_string_buffer(" " * (self.width * ((self.height * 3 + 3) & -3))) res = ctypes.windll.gdi32.GetDIBits(memorydc.GetSafeHdc(), data_bitmap.GetHandle(), 0, self.height, c_bits, c_bmpheader, win32con.DIB_RGB_COLORS) win32gui.DeleteDC(hdc) win32gui.ReleaseDC(self.hwnd, hdc) memorydc.DeleteDC() win32gui.DeleteObject(data_bitmap.GetHandle()) cv_im = cv.CreateImageHeader((self.width, self.height), cv.IPL_DEPTH_8U, 3) cv.SetData(cv_im, c_bits.raw) # flip around x-axis cv.Flip(cv_im, None, 0) mat = cv.GetMat(cv_im) return numpy.asarray(mat)
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 _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 draw_focus_rectangle(cls, region: Region) -> None: """Draw a highlight region around given region Args: region (Region): [description] """ dc = win32gui.GetDC(0) pen = win32gui.CreatePen(win32con.PS_SOLID, 2, 0) win32gui.DrawFocusRect(dc, region.to_tuple()) win32gui.SelectObject(dc, pen) win32gui.DeleteDC(dc)
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 pixel(self, x, y=None): if y is None: y = x[1] x = x[0] try: dc = win32gui.GetWindowDC(self.hwnd) colorref = win32gui.GetPixel(dc, x + 8, y + 8) except: raise (Exception, "Could not get pixel") win32gui.DeleteDC(dc) r, g, b = rgbint2rgbtuple(colorref) return Color(r, g, b)
def pixel(x, y=None): if y is None: y = x[1] x = x[0] try: window = win32gui.FindWindow(None, "The Legend of Pirates Online [BETA]") dc = win32gui.GetWindowDC(window) colorref = win32gui.GetPixel(dc, x + 8, y + 8) except: print("woops") return 0, 0, 0 win32gui.DeleteDC(dc) return rgbint2rgbtuple(colorref)
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 findImageEx(self, source, x, y, width, height): hdc = win32gui.GetWindowDC(self.hwnd) dc_obj = win32ui.CreateDCFromHandle(hdc) memorydc = dc_obj.CreateCompatibleDC() data_bitmap = win32ui.CreateBitmap() data_bitmap.CreateCompatibleBitmap(dc_obj, self.width, self.height) memorydc.SelectObject(data_bitmap) memorydc.BitBlt((0, 0), (self.width, self.height), dc_obj, (self.dx, self.dy), win32con.SRCCOPY) bmpheader = struct.pack("LHHHH", struct.calcsize("LHHHH"), self.width, self.height, 1, 24) c_bmpheader = ctypes.create_string_buffer(bmpheader) # padded_length = (string_length + 3) & -3 for 4-byte aligned. c_bits = ctypes.create_string_buffer(" " * (self.width * ((self.height * 3 + 3) & -3))) res = ctypes.windll.gdi32.GetDIBits(memorydc.GetSafeHdc(), data_bitmap.GetHandle(), 0, self.height, c_bits, c_bmpheader, win32con.DIB_RGB_COLORS) win32gui.DeleteDC(hdc) win32gui.ReleaseDC(self.hwnd, hdc) memorydc.DeleteDC() win32gui.DeleteObject(data_bitmap.GetHandle()) cv_im = cv.CreateImageHeader((self.width, self.height), cv.IPL_DEPTH_8U, 3) cv.SetData(cv_im, c_bits.raw) # flip around x-axis cv.Flip(cv_im, None, 0) im_region = cv.GetSubRect(cv_im, (x, y, width, height)) #cv.SaveImage('aaak.bmp', im_region) template_source = cv.LoadImage(source) # From the manual of MatchTemplate result_width = im_region.width - template_source.width + 1 result_height = im_region.height - template_source.height + 1; result = cv.CreateImage((result_width, result_height), 32, 1) cv.MatchTemplate(im_region, template_source, result, cv2.TM_CCOEFF_NORMED) minVal, maxVal, minLoc, maxLoc = cv.MinMaxLoc(result) #print minVal, maxVal, minLoc, maxLoc minLoc2 = minLoc[0] + x, minLoc[1] + y maxLoc2 = maxLoc[0] + x, maxLoc[1] + y return minVal, maxVal, minLoc2, maxLoc2
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 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 do_print_qu(): str1=current_textarea.get("0.0","end") pname=win32print.GetDefaultPrinter() pHandle=win32print.OpenPrinter(pname) printinfo=win32print.GetPrinter(pHandle,2) pDevModeObj=printinfo["pDevMode"] pDevModeObj.Scale=100 DC=win32gui.CreateDC("WINSPOOL",pname,pDevModeObj) hDC= win32ui.CreateDCFromHandle(DC) hDC.StartDoc("Python Editor") hDC.StartPage() hDC.TextOut(20,20,str1) hDC.EndPage() hDC.EndDoc() win32gui.DeleteDC(DC)
def draw_line(cls, points: List[Point2D], color: RGB = None) -> None: """[summary] Args: points (List[Point2D]): [description] color (RGB, optional): [description]. Defaults to None. """ if not color: color = RGB(0, 255, 0) # GetDC(hwnd), jos haluaa nimenomaan tietylle ikkunalle... dc = win32gui.GetDC(0) pen = win32gui.CreatePen(win32con.PS_SOLID, 2, color.to_color_ref()) win32gui.SelectObject(dc, pen) lista = [p.to_tuple() for p in points] win32gui.Polyline(dc, lista) win32gui.DeleteObject(pen) win32gui.DeleteDC(dc)
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 draw_ellipse(cls, region: Region, color: RGB = None) -> None: """Draws a colored ellipse around given region Args: region (Region): [description] color (RGB, optional): [description]. Defaults to None. """ if not color: color = RGB(0, 255, 0) dc = win32gui.GetDC(0) pen = win32gui.CreatePen(win32con.PS_SOLID, 2, color.to_color_ref()) brush = win32gui.CreateBrushIndirect({ 'Style': win32con.BS_NULL, 'Color': -1, 'Hatch': win32con.HS_DIAGCROSS }) win32gui.SelectObject(dc, pen) win32gui.SelectObject(dc, brush) win32gui.Ellipse(dc, *region.to_tuple()) win32gui.DeleteObject(pen) win32gui.DeleteObject(brush) win32gui.DeleteDC(dc)
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 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)
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) font = win32gui.LOGFONT() font.lfHeight = int(printerheight / 20) font.lfWidth = font.lfHeight font.lfWeight = 150 font.lfItalic = 1 font.lfUnderline = 1 hf = win32gui.CreateFontIndirect(font) win32gui.SelectObject(pDC, hf) win32gui.SetBkMode(pDC, win32con.TRANSPARENT) win32gui.SetTextColor(pDC, win32api.RGB(0, 255, 0)) win32gui.DrawText( pDC, 'Printed by Python!', -1, (0, 0, int(printerwidth * .9), int(printerheight * .9)), win32con.DT_RIGHT | win32con.DT_BOTTOM | win32con.DT_SINGLELINE) win32print.EndPage(pDC) win32print.EndDoc(pDC) win32print.ClosePrinter(p) win32gui.DeleteObject(dcBM) win32gui.DeleteObject(pcBM) win32gui.DeleteObject(hf) win32gui.DeleteDC(dDC) win32gui.DeleteDC(dcDC) win32gui.DeleteDC(pDC) win32gui.DeleteDC(pcDC)
def __del__(self): gui.DeleteDC(self.dc)
def __del__(self): if sys.platform == "win32": win32gui.DeleteObject(self.pycfont.GetSafeHandle()) win32gui.DeleteDC(self.dc)