def run(self): if not os.path.exists('temp'): os.mkdir('temp') else: shutil.rmtree('temp') os.mkdir('temp') self.is_pause_for_save = True self.recording = True self.recordTime = 0 self.image_list = [] t = time.time() imCursor = Image.open('res/cursor.png') self.image_gif_list = [] while self.recording: curX, curY = win32gui.GetCursorPos() if self.area is None: image = ImageGrab.grab() image.paste(imCursor, box=(curX, curY), mask=imCursor) else: image = ImageGrab.grab(self.area) image.paste(imCursor, box=(curX - self.area[0], curY - self.area[1]), mask=imCursor) imageName = os.path.join('temp', '%s.jpg' % int(time.time() * 1e3)) image.save(imageName) self.image_list.append(imageName) self.recordTime = time.time() - t # """生成gif""" # self.image_gif_list.append(imageio.imread(imageName)) fps = len(self.image_list) / self.recordTime self.trigger.emit(self.image_list, fps, self.image_gif_list)
def show_menu(self): if self.MENU is None: self.create_menu() position = win32gui.GetCursorPos() win32gui.SetForegroundWindow(self.HWND) win32gui.TrackPopupMenu(self.MENU, win32con.TPM_LEFTALIGN, position[0], position[1], 0, self.HWND, None) win32gui.PostMessage(self.HWND, win32con.WM_NULL, None, None)
def Click2(x, y): # 鼠标点击 a = win32gui.GetCursorPos() x = int(x) y = int(y) win32api.SetCursorPos((x, y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, x, y, 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, x, y, 0, 0) win32api.SetCursorPos(a) # 模拟键盘输入
def show_menu(self): self.menu = win32gui.CreatePopupMenu() self.create_menu(self.menu, self.menu_options) pos = win32gui.GetCursorPos() win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(self.menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def client_pos(): ''' help to find click pos ''' handle = win32gui.FindWindow(None, '阴阳师-网易游戏') pos = win32gui.GetCursorPos() client_pos = win32gui.ScreenToClient(handle, pos) return client_pos
def WindowFromMouse(): """ 获取鼠标点窗口句柄. :return: 窗口句柄hwnd """ (mx, my) = win32gui.GetCursorPos() return win32gui.WindowFromPoint((mx, my))
def get_pos_report(hwnd): tpanel = get_panel(hwnd) left, top, right, bottom = win32gui.GetWindowRect(tpanel) x = right - left y = bottom - top win32api.SetCursorPos([int(left + project_x + calculate_x + report_x / zoom / 2), int(top + y / 2)]) sleep(2) pos = win32gui.GetCursorPos()
def click(): # get mouse x, y x,y=win32gui.GetCursorPos() # print("%s %s" %(x, y)) win32api.SetCursorPos((x,y)) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN,x,y,0,0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP,x,y,0,0)
def listen(self): while self.isRunning: currSecond = time.time() millis = int(round(currSecond * 1000)) if millis - self.lastMillis >= 10: (x, y) = win32gui.GetCursorPos() self.recordMousePosition(x, y, millis) self.lastMillis = millis
def external_events(self): cx, cy = win32gui.GetCursorPos() for sk in self.skeletons: sk.update_mouse(cx, cy) if cy == 0: self.next_sprite()
def storeUserState(): userDict = {} userMouseX, userMouseY = win32gui.GetCursorPos() currWindowHWND = pywinauto.win32functions.GetForegroundWindow() userDict['userMouseX'] = userMouseX userDict['userMouseY'] = userMouseY userDict['userWindowHWND'] = currWindowHWND # print ('Saving usermouse at X: ' + str(userDict['userMouseX']) + ' Y: ' + str(userDict['userMouseY'])) return userDict
def GetPixelColour(hwnd , i_x, i_y): GetPosWnd(hwnd , i_x, i_y) Active(hwnd) i_x, i_y = win32gui.GetCursorPos() i_desktop_window_id = win32gui.GetDesktopWindow() i_desktop_window_dc = win32gui.GetWindowDC(i_desktop_window_id) long_colour = win32gui.GetPixel(i_desktop_window_dc, i_x, i_y) i_colour = int(long_colour) return (i_colour & 0xff), ((i_colour >> 8) & 0xff), ((i_colour >> 16) & 0xff)
def is_cursor_in_window(self, hwnd): cursor_x, cursor_y = win32gui.GetCursorPos() anchor_x, anchor_y, bottom_right_x, bottom_right_y = self.get_window_location(hwnd) if cursor_x >= anchor_x and cursor_x <= bottom_right_x: if cursor_y >= anchor_y and cursor_y <= bottom_right_y: return True return False
def l_mouse_state(btn_state, pos=win32gui.GetCursorPos()): """ Changes left mouse button state. :param btn_state: True for pressed, False for released :param pos: Click position on screen. Defaults to current mouse position """ win32api.mouse_event( win32con.MOUSEEVENTF_LEFTDOWN if btn_state else win32con.MOUSEEVENTF_LEFTUP, pos[0], pos[1], 0, 0)
def getPixelInfo(self): "Work on Desktop." pos = self.getCurrentCursorPos() if not pos: pos = win32gui.GetCursorPos() color = self.getPixelOnDesktop(*pos) return (pos, color) color = self.getPixel(*pos) return (pos, color)
def show_menu(self): menu = win32gui.CreatePopupMenu() self.create_menu(menu, self.menu_options) pos = win32gui.GetCursorPos() # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def invisible_click_rel(self,handle,pos, button_name="left"): """Click in window coordinates without moving mouse""" #get window info xleft, ytop, xright, ybottom = win32gui.GetWindowRect(handle) xcur,ycur = win32gui.GetCursorPos() ctypes.windll.user32.SetCursorPos(pos[0]+xleft,pos[1]+ytop) self.click((pos[0]+xleft,pos[1]+ytop),button_name) ctypes.windll.user32.SetCursorPos(xcur,ycur)
def getCurrentCursorPos(self): x, y = win32gui.GetCursorPos() dw_right_top_x = self.left + self.width dw_left_bottom_y = self.top + self.height if (self.left < abs(x) < dw_right_top_x) and (self.top < abs(y) < dw_left_bottom_y): # Cursor on window return self.desktopPosToPos(x, y) # Out of window return None
def get_point(self): ret = True while ret: c = msvcrt.getwch() if "c" == c: ret = False (tmpX, tmpY) = win32gui.GetCursorPos() return tmpX, tmpY
def clickOnWindow(hld, pos): cursor_pos = win32gui.GetCursorPos() win32api.SetCursorPos(pos) win32gui.SetForegroundWindow(hld) time.sleep(0.2) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, pos[0], pos[1], 0, 0) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, pos[0], pos[1], 0, 0) win32api.SetCursorPos(cursor_pos)
def eventListener(self): state_capital = win32api.GetKeyState(win32con.VK_CAPITAL) state_control = win32api.GetKeyState(win32con.VK_RCONTROL) state_middle = win32api.GetKeyState( win32con.VK_MBUTTON ) # button up = 0 or 1. Button down = -127 or -128 while self.loop: if state_capital != win32api.GetKeyState(win32con.VK_CAPITAL): state_capital = win32api.GetKeyState(win32con.VK_CAPITAL) if state_capital >= 0: self.upperCaseCheckBox.SetValue(state_capital == 1) if self.middleClickCheckBox.IsChecked(): a = win32api.GetKeyState(win32con.VK_MBUTTON) if a != state_middle: # Button state changed state_middle = a if a < 0: pos = win32gui.GetCursorPos() performClick(pos[0], pos[1]) if self.recording: self.lastClick = pos if self.recordBtn.IsEnabled(): self.OnRecord() a = win32api.GetKeyState(win32con.VK_RCONTROL) if a != state_control: # Button state changed state_control = a if not self.controlHoldingCheckBox.IsChecked(): if a < 0 and self.recordBtn.IsEnabled(): self.OnRecord() continue if a < 0 and not self.recording: time.sleep(0.1) a = win32api.GetKeyState(win32con.VK_RCONTROL) if (a >= 0 and self.recording) or (a < 0 and not self.recording): pos = win32gui.GetCursorPos() if self.recording: self.lastClick = pos if self.recordBtn.IsEnabled(): self.OnRecord() time.sleep(0.1)
def grab_screen(): while True: # Loop until return cursor_current_x, cursor_current_y = 0, 0 # Declare variable for future cursor crop_im = False # Declare boolean for cropping later win_DC = win32gui.GetDC(0) # Get device context cursor_origin_x, cursor_origin_y = win32gui.GetCursorPos( ) # Get cursor position full_image = ImageGrab.grab(bbox=( 0, # Capture entire screen 0, win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1))) while win32api.GetAsyncKeyState( win32con.VK_CONTROL) < 0: # While CTRL key is pressed if not crop_im: crop_im = True # Set crop bool so crop branch is entered after cursor_current_x, cursor_current_y = win32gui.GetCursorPos( ) # Get current cursor position win32gui.MoveToEx( win_DC, cursor_origin_x, cursor_origin_y) # Draw a box with original cursor win32gui.LineTo( win_DC, cursor_origin_x, cursor_current_y) # and current cursor as opposite corners win32gui.MoveToEx(win_DC, cursor_origin_x, cursor_current_y) win32gui.LineTo(win_DC, cursor_current_x, cursor_current_y) win32gui.MoveToEx(win_DC, cursor_current_x, cursor_current_y) win32gui.LineTo(win_DC, cursor_current_x, cursor_origin_y) win32gui.MoveToEx(win_DC, cursor_current_x, cursor_origin_y) win32gui.LineTo(win_DC, cursor_origin_x, cursor_origin_y) if crop_im: # Enter crop branch, crop to box size cropped_image = full_image.crop( (min(cursor_current_x, cursor_origin_x), min(cursor_current_y, cursor_origin_y), max(cursor_current_x, cursor_origin_x), max(cursor_current_y, cursor_origin_y))) return cropped_image # RETURN: cropped image sleep(.01) # Sleep so it doesn't waste resources
def grab_im(): """ 选择区域截图,返回截取的PIL图片 """ while True: cursor_current_x, cursor_current_y = 0, 0 crop_im = False # 获取设备环境 win_DC = win32gui.GetDC(0) # 获取鼠标位置 cursor_origin_x, cursor_origin_y = win32gui.GetCursorPos() # 截取全屏 full_image = ImageGrab.grab(bbox=(0, 0, win32api.GetSystemMetrics(0), win32api.GetSystemMetrics(1))) # 只有当检测到CTRL键被按下时才进行截 while win32api.GetAsyncKeyState(win32con.VK_CONTROL) < 0: if not crop_im: crop_im = True # 获取当前鼠标位置 cursor_current_x, cursor_current_y = win32gui.GetCursorPos() # 绘制矩形框 win32gui.MoveToEx(win_DC, cursor_origin_x, cursor_origin_y) win32gui.LineTo(win_DC, cursor_origin_x, cursor_current_y) win32gui.MoveToEx(win_DC, cursor_origin_x, cursor_current_y) win32gui.LineTo(win_DC, cursor_current_x, cursor_current_y) win32gui.MoveToEx(win_DC, cursor_current_x, cursor_current_y) win32gui.LineTo(win_DC, cursor_current_x, cursor_origin_y) win32gui.MoveToEx(win_DC, cursor_current_x, cursor_origin_y) win32gui.LineTo(win_DC, cursor_origin_x, cursor_origin_y) # 根据矩形框裁剪 if crop_im: cropped_image = full_image.crop( (min(cursor_current_x, cursor_origin_x), min(cursor_current_y, cursor_origin_y), max(cursor_current_x, cursor_origin_x), max(cursor_current_y, cursor_origin_y))) return cropped_image sleep(.01)
def _drag(self, *args): x, y = win32gui.GetCursorPos() x -= self.touch_x y -= self.touch_y if self.window_drag_mode == 'both' or self.window_drag_mode == 'horizontal': Window.left = x if self.window_drag_mode == 'both' or self.window_drag_mode == 'vertical': Window.top = y
def main(): print win32gui.GetCursorPos() #定位 win32api.SetCursorPos([510, 846]) # 鼠标点击 win32api.mouse_event(win32con.MOUSEEVENTF_LEFTDOWN, 0, 0) time.sleep(0.05) win32api.mouse_event(win32con.MOUSEEVENTF_LEFTUP, 0, 0) time.sleep(0.05)
def getmouseinfo(): ForegroundWindowHandle = win32gui.GetForegroundWindow() # Active(ForegroundWindowHandle) print "ForegroundWindowHandle========",ForegroundWindowHandle title = win32gui.GetWindowText(ForegroundWindowHandle) classname = win32gui.GetClassName(ForegroundWindowHandle) (x_, y_, w_, z_) = win32gui.GetWindowRect(ForegroundWindowHandle) (x, y) = win32gui.GetCursorPos() x_insidewindow = x - x_ y_insidewindow = y - y_ return title, classname, x_insidewindow, y_insidewindow
def get_mouse_xy(me): if wx: (x, y) = wx.GetMousePosition() elif X: data = xdisplay.screen().root.query_pointer()._data x = data["root_x"] y = data["root_y"] else: (x, y) = win32gui.GetCursorPos() return (x, y)
def _capture_text(self): MAX_OUTPUT_LEN = 1024 x, y = win32gui.GetCursorPos() hrwnd = self.icall.GetRealWindow(x, y) strtmp = ctypes.create_unicode_buffer('\0' * MAX_OUTPUT_LEN) i = ctypes.c_int(-1) ok = self.icall.GetWord(hrwnd, x, y, strtmp, MAX_OUTPUT_LEN, ctypes.byref(i)) if ok: print u'全部文本:%s' % strtmp.value print u'单词位置:%s' % i.value
def RightMB(self): menu = win32gui.CreatePopupMenu() win32gui.AppendMenu(menu, win32con.MF_STRING, 1023, "Settings") win32gui.AppendMenu(menu, win32con.MF_STRING, 1024, "Exit program") pos = win32gui.GetCursorPos() # See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/menus_0hdi.asp win32gui.SetForegroundWindow(self.hwnd) win32gui.TrackPopupMenu(menu, win32con.TPM_LEFTALIGN, pos[0], pos[1], 0, self.hwnd, None) win32gui.PostMessage(self.hwnd, win32con.WM_NULL, 0, 0)
def get_window_from_cursor(): prevWindow = None curX, curY = win32gui.GetCursorPos() handle = win32gui.WindowFromPoint((curX, curY)) prevWindow = handle window_name = win32gui.GetWindowText(handle) r = get_windowsinfo(window_name) print(r) hightligt_window(handle) screenshot(handle, 'pp.png') refreshWindow(handle)