def click(self, x, y): if self.click_by_mouse: window_title = self._getWindowTitle() try: hwin = win32gui.FindWindow('LDPlayerMainFrame', window_title) self._subhwin = None def winfun(hwnd, lparam): subtitle = win32gui.GetWindowText(hwnd) if subtitle == 'TheRender': self._subhwin = hwnd win32gui.EnumChildWindows(hwin, winfun, None) ret = win32gui.GetWindowRect(self._subhwin) height = ret[3] - ret[1] width = ret[2] - ret[0] tx = int(x * width / constants.BASE_WIDTH) ty = int(y * height / constants.BASE_HEIGHT) positon = win32api.MAKELONG(tx, ty) win32api.SendMessage(self._subhwin, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, positon) win32api.SendMessage(self._subhwin, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, positon) except Exception as e: print(f"fallback adb click:{e}") super().click(x, y) else: super().click(x, y)
def GetHld(self, post): for hld in self.hWndChildList: left, top, right, bottom = win32gui.GetWindowRect(hld) # get hld post left top right bottom # if (post[0]==left) & (post[1]==top) & (post[2]==right) & (post[3]==right): if (post[0] == left) & (post[1] == top): # get hld by (x,y) left top return hld return 0
def init_emu(self): hwnd = self.device_id left, top, right, bot = win32gui.GetWindowRect(hwnd) self.device_left = left self.device_top = top self.device_width = (right - left) self.device_height = (bot - top)
def screengrab(hwnd): import win32gui import win32ui left, top, right, bottom = win32gui.GetWindowRect(hwnd) width, height = right - left, bottom - top hwndDC = win32gui.GetWindowDC(hwnd) mfcDC = win32ui.CreateDCFromHandle(hwndDC) saveDC = mfcDC.CreateCompatibleDC() saveDC.SetWindowOrg((0, 0)) # 13, 151)) saveBitMap = win32ui.CreateBitmap() saveBitMap.CreateCompatibleBitmap(mfcDC, width, height) # 287, 76) saveDC.SelectObject(saveBitMap) # result = windll.user32.PrintWindow(hwnd, saveDC.GetSafeHdc(), 0) import win32con win32gui.BitBlt(saveDC.GetSafeHdc(), 0, 0, width, height, hwndDC, 0, 0, win32con.SRCCOPY) bmpinfo = saveBitMap.GetInfo() bmpstr = saveBitMap.GetBitmapBits(True) im = Image.frombuffer('RGB', (bmpinfo['bmWidth'], bmpinfo['bmHeight']), bmpstr, 'raw', 'BGRX', 0, 1) win32gui.DeleteObject(saveBitMap.GetHandle()) saveDC.DeleteDC() mfcDC.DeleteDC() win32gui.ReleaseDC(hwnd, hwndDC) return im
def nativeEvent(self, eventType, message): """ 处理windows消息 """ msg = MSG.from_address(message.__int__()) if msg.message == win32con.WM_NCHITTEST: # 解决多屏下会出现鼠标一直为拖动状态的问题 xPos = (win32api.LOWORD(msg.lParam) - self.frameGeometry().x()) % 65536 yPos = win32api.HIWORD(msg.lParam) - self.frameGeometry().y() w, h = self.width(), self.height() lx = xPos < self.BORDER_WIDTH rx = xPos + 9 > w - self.BORDER_WIDTH ty = yPos < self.BORDER_WIDTH by = yPos > h - self.BORDER_WIDTH if lx and ty: return True, win32con.HTTOPLEFT elif rx and by: return True, win32con.HTBOTTOMRIGHT elif rx and ty: return True, win32con.HTTOPRIGHT elif lx and by: return True, win32con.HTBOTTOMLEFT elif ty: return True, win32con.HTTOP elif by: return True, win32con.HTBOTTOM elif lx: return True, win32con.HTLEFT elif rx: return True, win32con.HTRIGHT elif msg.message == win32con.WM_NCCALCSIZE: if self.isWindowMaximized(msg.hWnd): self.monitorNCCALCSIZE(msg) return True, 0 elif msg.message == win32con.WM_GETMINMAXINFO: if self.isWindowMaximized(msg.hWnd): window_rect = win32gui.GetWindowRect(msg.hWnd) if not window_rect: return False, 0 # 获取显示器句柄 monitor = win32api.MonitorFromRect(window_rect) if not monitor: return False, 0 # 获取显示器信息 monitor_info = win32api.GetMonitorInfo(monitor) monitor_rect = monitor_info['Monitor'] work_area = monitor_info['Work'] # 将lParam转换为MINMAXINFO指针 info = cast(msg.lParam, POINTER(MINMAXINFO)).contents # 调整窗口大小 info.ptMaxSize.x = work_area[2] - work_area[0] info.ptMaxSize.y = work_area[3] - work_area[1] info.ptMaxTrackSize.x = info.ptMaxSize.x info.ptMaxTrackSize.y = info.ptMaxSize.y # 修改左上角坐标 info.ptMaxPosition.x = abs(window_rect[0] - monitor_rect[0]) info.ptMaxPosition.y = abs(window_rect[1] - monitor_rect[1]) return True, 1 return QWidget.nativeEvent(self, eventType, message)
def classify_handwriting(self): HWND = self.canvas.winfo_id() # get the handle of the canvas rect = win32gui.GetWindowRect(HWND) # get the coordinate of the canvas a,b,c,d = rect rect=(a+4,b+4,c-4,d-4) im = ImageGrab.grab(rect) digit, acc = predict_digit(im) self.label.configure(text= str(digit)+', '+ str(int(acc*100))+'%')
def getFrame(): hwnd = win32gui.FindWindow(None, r'Grand Theft Auto V') win32gui.SetForegroundWindow(hwnd) dimensions = win32gui.GetWindowRect(hwnd) pressKey(DIK_E) pressKey(DIK_PAGE_UP) sleep(1) image = ImageGrab.grab(dimensions) return image
def window_rect(hwnd: int) -> 'autowinpy.type.Rect': """핸들로부터 윈도우 사각영역 좌표를 가져옴 Args: hwnd: 윈도우 핸들 Returns: :class:`Rect <autowinpy.type.Rect>` 사각영역 객체 """ return Rect(*win32gui.GetWindowRect(hwnd))
def show_window_attr(self, hWnd): if not hWnd: return title = win32gui.GetWindowText(hWnd) clsname = win32gui.GetClassName(hWnd) left, top, right, bottom = win32gui.GetWindowRect(hWnd) # attr = {'hWnd': hWnd, 'title': title, 'clsname': clsname, 'post': [left, top, right, bottom]} return attr
def ypp_window_callback(hwnd, _extras): rect = win32gui.GetWindowRect(hwnd) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y window_title = win32gui.GetWindowText(hwnd) if 'Merciless Client' in window_title: print('Window found! location=(%d, %d), size=(%d, %d)' % (x, y, w, h)) pixelDetector(hwnd, w, h)
def ypp_window_callback(hwnd, _extras): rect = win32gui.GetWindowRect(hwnd) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y window_title = win32gui.GetWindowText(hwnd) if 'Puzzle Pirates - ' in window_title: print('Window found! location=(%d, %d), size=(%d, %d)' % (x, y, w, h)) pw = PokerWatcher(hwnd, x, y, w, h) pw.start()
def screenshot(self): if not self.dimensions: hwnd = win32gui.FindWindow(None, r'Euro Truck Simulator 2') win32gui.SetForegroundWindow(hwnd) dimensions = win32gui.GetWindowRect(hwnd) dimensions = list(dimensions) dimensions[0] += 10 dimensions[1] += 30 dimensions[2] -= 10 dimensions[3] -= 10 self.dimensions = (dimensions) return ImageGrab.grab(bbox=self.dimensions)
def GetHWND(self, HWND): ''' Gets handle of window to view HWND: Handle of window to be captured Return: True on success; False on failure ''' if win32gui.IsWindowVisible(HWND): self.hwnd = HWND self.winLeft, self.winTop, self.winRight, self.winBottom = win32gui.GetWindowRect( self.hwnd) return True return False
def getActiveWindowBBox(self): # https://github.com/asweigart/PyGetWindow/blob/master/random_notes.txt # print("system:", platform.system()) if (platform.system() == "Windows"): # import win32gui from win32 import win32gui from ctypes import windll # Make program aware of DPI scaling user32 = windll.user32 user32.SetProcessDPIAware() # w = win32gui.GetForegroundWindow() hwnd = win32gui.GetForegroundWindow() rect = win32gui.GetWindowRect(hwnd) print("rect: ", rect) x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y x2 = rect[2] y2 = rect[3] print("Window %s:" % win32gui.GetWindowText(hwnd)) print("\tLocation: (%d, %d)" % (x, y)) print("\tLocation: (%d, %d)" % (x2, y2)) print("\t Size: (%d, %d)" % (w, h)) bbox = (x, y, x2, y2) return bbox elif (platform.system() == "Darwin"): # https://stackoverflow.com/questions/373020/finding-the-current-active-window-in-mac-os-x-using-python from AppKit import NSWorkspace # activeApp = NSWorkspace.sharedWorkspace().activeApplication() # depricated activeApp = NSWorkspace.sharedWorkspace().frontmostApplication() print(activeApp) activeAppID = NSWorkspace.sharedWorkspace().activeApplication( )['NSApplicationProcessIdentifier'] print(activeAppID) # activeAppName = NSWorkspace.sharedWorkspace().frontmostApplication()['NSApplicationName'] # print(activeAppName) activeAppName = NSWorkspace.sharedWorkspace().activeApplication( )['NSApplicationName'] print(activeAppName) bbox = (10, 10, 510, 510) return bbox else: # try linux? bbox = (10, 10, 510, 510) return bbox bbox = (10, 10, 510, 510) return bbox
def getFrameWin(self): # get corner coordinates of capture window position = win32gui.GetWindowRect(self.hwnd) # save pixels into array frame = getRectAsImage(position) frame = np.array(frame) frame = frame[self.crop[1]:self.crop[3], self.crop[0]:self.crop[2]] frameGs = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) frameGsRr = cv2.resize(frameGs, self.outDim) return frameGsRr
def winEnumHandler( hwnd, ctx): global WindowRects; if win32gui.IsWindowVisible( hwnd ): #print(hex(hwnd), win32gui.GetWindowText( hwnd )) if (win32gui.GetWindowText(hwnd) == "RWBY Deckbuilding Game"): print(hex(hwnd), win32gui.GetWindowText( hwnd )) rect = win32gui.GetWindowRect(hwnd); winRect = [0,0,0,0] winRect[0] = rect[0] winRect[1] = rect[1] winRect[2] = rect[2] winRect[3] = rect[3] WindowRects.append(winRect);
def callback(hwnd, extra): rect = win32gui.GetWindowRect(hwnd) x = int(rect[0] * 1.25) y = int(rect[1] * 1.25) w = int(rect[2] * 1.25) - x h = int(rect[3] * 1.25) - y text = win32gui.GetWindowText(hwnd) if text == "Old School RuneScape": print("Window %s:" % text) print("\tLocation: (%d, %d)" % (x, y)) print("\t Size: (%d, %d)" % (w, h)) size = [int(w), int(h)] position = [x, y] monitor = {'top': y, 'left': x, 'width': size[0], 'height': size[1]} screenshot_and_draw(monitor)
def getLiveSof(): while True: try: rect = win32gui.GetWindowRect(hwnd) break except Exception as e: if e == KeyboardInterrupt: raise hwnd = searchForSoFWindow() x = rect[0] y = rect[1] w = rect[2] - x h = rect[3] - y retRes[0] = w retRes[1] = h return retRes
def get_game_region(title=None): if title: gamewin = win32gui.FindWindow(None, title) if not gamewin: raise Exception('window title not found') #get the bounding box of the window x1, y1, x2, y2 = win32gui.GetWindowRect(gamewin) y1 += 30 # get rid of window bar h_padding = (y2 - y1) * 0.1 w_padding = (x2 - x1) * 0.1 y1 += h_padding y2 -= h_padding x1 += w_padding x2 -= w_padding return (x1, y1, (x2 - x1 + 1), (y2 - y1 + 1)) else: raise Exception("no window title was given.")
def track_window_position(monitor_dict): hwnd = monitor_dict["hwnd"] rw = win32gui.GetWindowRect(hwnd) rc = win32gui.GetClientRect(hwnd) window_width = rw[2] - rw[0] border_width = window_width - rc[ 2] # the client's right side is before the end of the window border_width -= 9 # ??? window_height = rw[3] - rw[1] border_height = window_height - rc[3] border_height -= 8 # ? # border_height = monitor_dict["left"] = rw[0] + border_width monitor_dict["top"] = rw[1] + border_height + SM_HUD_HEIGHT # monitor_dict["width"] = rc[2] + 1 monitor_dict["width"] = SM_SIZE[0] # monitor_dict["height"] = rc[3] - SM_HUD_HEIGHT monitor_dict["height"] = SM_SIZE[1] - SM_HUD_HEIGHT - CROP_BOTTOM
def fit_window_callback(window, arg): """Checks if the given window handle matches the program we want, and saves window data to the global monitor variable""" reg = re.compile('.*Hidden Star in Four Seasons.*') if reg.match(wgui.GetWindowText(window)): global monitor rect = wgui.GetWindowRect(window) monitor['top'] = int(rect[1] * SCALE) + MONITOR_WINDOW_MARGIN[0] monitor['left'] = int(rect[0] * SCALE) + MONITOR_WINDOW_MARGIN[1] monitor['width'] = int( (rect[2] - rect[0]) * SCALE) - MONITOR_WINDOW_MARGIN[2] - MONITOR_WINDOW_MARGIN[1] monitor['height'] = int( (rect[3] - rect[1]) * SCALE) - MONITOR_WINDOW_MARGIN[3] - MONITOR_WINDOW_MARGIN[0] monitor['top'] += int(monitor['height'] * MONITOR_GAME_AREA_RATIO[0]) monitor['left'] += int(monitor['width'] * MONITOR_GAME_AREA_RATIO[1]) monitor['width'] = int(monitor['width'] * MONITOR_GAME_AREA_RATIO[2]) monitor['height'] = int(monitor['height'] * MONITOR_GAME_AREA_RATIO[3])
def __init__(self, window_name=None): if window_name is None: self.hwnd = win32gui.GetDesktopWindow() else: self.hwnd = win32gui.FindWindow(None, window_name) if not self.hwnd: raise Exception('Window not found: {}'.format(window_name)) window_rect = win32gui.GetWindowRect(self.hwnd) self.w = window_rect[2] - window_rect[0] self.h = window_rect[3] - window_rect[1] border_pixels = 0 titlebar_pixels = 0 # self.w = self.w - (border_pixels * 2) # self.h = self.h - titlebar_pixels - border_pixels # self.cropped_x = border_pixels # self.cropped_y = titlebar_pixels self.offset_x = window_rect[0] + self.cropped_x self.offset_y = window_rect[1] + self.cropped_y
def __init__(self, name, path, process_queue, event, lock=None, logger=None): super().__init__(name, path, process_queue, event, logger=logger) with lock: self.process = subprocess.Popen(self.path, stdout=subprocess.PIPE, stderr=subprocess.PIPE) self.pid = self.process.pid try: self.process.wait(timeout=2) except subprocess.TimeoutExpired: if self.process.returncode is None: window = win32gui.GetForegroundWindow() active_window_name = win32gui.GetWindowText(window) rect = win32gui.GetWindowRect(window) if active_window_name == 'Telex': buttons = list(pyautogui.locateAllOnScreen('recieve.png')) for button in buttons: x=button[0]+(button[2]//2) y=button[1]+(button[3]//2) if rect[0] < x < rect[2] and rect[1] < y < rect[3]: pyautogui.click(x=button[0]+(button[2]//2), y=button[1]+(button[3]//2), clicks=2, button='left') time.sleep(1)
def arrange(self): config = [] for row in range(self._table.rowCount()): d = OrderedDict() for col, k in enumerate(self._labels): x = self.get(row, col) if x: d[k] = x if d.get('title'): config.append(d) import re windows = self.all_windows() for d in config: if d['title']: for handle, title in windows.items(): if re.match(d['title'], title): rect = win32gui.GetWindowRect(handle) x = int(d.get('x', rect[0])) y = int(d.get('y', rect[1])) w = int(d.get('w', rect[2] - rect[0])) h = int(d.get('h', rect[3] - rect[1])) win32gui.MoveWindow(handle, x, y, w, h, True)
def __init_window(self): # 画面サイズの取得 screen_x, screen_y = pyautogui.size() # win32guiを使ってウインドウタイトルを探す # Windowのハンドル取得('クラス名','タイトルの一部')で検索クラスがわからなかったらNoneにする parent_handle = win32gui.FindWindow(None, "Mine2000") # ハンドルIDが取れなかったら、mine2000を起動する if parent_handle == 0: cmd = 'C:\Program Files (x86)\mine2000 project\mine2000 ver2.2.1\mine2000.exe' subprocess.Popen(cmd, shell=True) time.sleep(1) parent_handle = win32gui.FindWindow(None, "Mine2000") if parent_handle == 0: sys.exit() # ハンドルが取れたら、ウインドウの左上と右下の座標取得と画面のアクティブ化 if parent_handle > 0: w0, h0, w1, h1 = win32gui.GetWindowRect(parent_handle) apw_x = w1 - w0 apw_y = h1 - h0 # ウィンドウをアクティブに持ってくる win32gui.SetForegroundWindow(parent_handle) time.sleep(0.5) # ウィンドウを画面中央に持ってくる x_pos = int((screen_x - apw_x) / 2) y_pos = int((screen_y - apw_y) / 2) win32gui.MoveWindow(parent_handle, x_pos, y_pos, apw_x, apw_y, True) # スクリーンショット用に座標を調整する return (w0 + 4, h0 + 79, w1 - 4, h1) raise Exception
def GetScreenImg(self): if self.hwnd is None: raise Exception( "HWND is none. HWND not called or invalid window name provided." ) l, t, r, b = win32gui.GetWindowRect(self.hwnd) self.winLeft, self.winTop, self.winRight, self.winBottom = win32gui.GetClientRect( self.hwnd) # Calculate Width of Window w = self.winRight - self.winLeft # Calculate Height of Window h = self.winBottom - self.winTop wDC = win32gui.GetWindowDC(self.hwnd) dcObj = win32ui.CreateDCFromHandle(wDC) cDC = dcObj.CreateCompatibleDC() dataBitMap = win32ui.CreateBitmap() dataBitMap.CreateCompatibleBitmap(dcObj, w, h) cDC.SelectObject(dataBitMap) #First 2 tuples are top-left and bottom-right of destination #Third tuple is the start position in source cDC.BitBlt((0, 0), (w, h), dcObj, (0, 0), win32con.SRCCOPY) #result = windll.user32.PrintWindow(self.hwnd, cDC.GetSafeHdc(),0) bmInfo = dataBitMap.GetInfo() im = np.frombuffer(dataBitMap.GetBitmapBits(True), dtype=np.uint8) # Numpy Implementation #bmStr = dataBitMap.GetBitmapBits(True) #im = Image.frombuffer('RGB',(bmInfo['bmWidth'],bmInfo['bmHeight']),bmStr,'raw','BGRX',0,1) PIL is too slow dcObj.DeleteDC() cDC.DeleteDC() win32gui.ReleaseDC(self.hwnd, wDC) win32gui.DeleteObject(dataBitMap.GetHandle()) #Bitmap has 4 channels like: BGRA. Discard Alpha and flip order to RGB if im.size < 4: return np.zeros((1, 1, 3), dtype=np.uint8) return np.array( im.reshape(bmInfo['bmHeight'], bmInfo['bmWidth'], 4)[:, :, -2::-1])
def win_rect(self) -> tuple: """ 获取窗口的区域(left, top, right, bottom) @property {tuple} """ return win32gui.GetWindowRect(self.handle)
while (True): # gtawin = win32gui.FindWindow(None,"War") name = []; ## Check if correct program is started # while(name != 'Spyder (Python 3.7)'): # time.sleep(0.1); # name=win32gui.GetWindowText(win32gui.GetForegroundWindow()); # print(name) gtawin = win32gui.GetForegroundWindow() if not gtawin: raise Exception('Foxhole not running') # get the bounding box of the window left, top, x2, y2 = win32gui.GetWindowRect(gtawin) widthScreen = x2 - left + 1 heightScreen = y2 - top + 1 win32gui.ShowWindow(gtawin, 1); # win32gui.BringWindowToTop(gtawin); # win32gui.SetForegroundWindow(gtawin); windowName = 'Evaluation´of Cards'; cv2.namedWindow(windowName, cv2.WINDOW_AUTOSIZE); cv2.moveWindow(windowName, 0, 0); font = cv2.FONT_HERSHEY_SIMPLEX with mss.mss() as sct: # The screen part to capture imageCounter = imageCounter + 1;
def Height(self): if (False == self.IsOpen()): return None return win32gui.GetWindowRect(self.__hwnd_main)[3] - self.Ypos()
def Rect(self): if (False == self.IsOpen()): return None return win32gui.GetWindowRect(self.__hwnd_main)